gpt4 book ai didi

c# - 在 C# 中将 JPG 与 GDI 合并

转载 作者:行者123 更新时间:2023-11-30 22:49:36 25 4
gpt4 key购买 nike

我的场景:

  • 我有一张彩色背景图片 JPG。
  • 我有一张白底黑字 JPG。
  • 两张图片大小相同(高和宽)

我想在彩色背景图像上叠加黑色文本和白色背景的图像,即白色背景变得透明以看到其下方的彩色背景。

我如何在 C# 中使用 GDI 执行此操作?

谢谢!

最佳答案

感谢GalacticCowboy我能够想出这个解决方案:

using (Bitmap background = (Bitmap)Bitmap.FromFile(backgroundPath))
{
using (Bitmap foreground = (Bitmap)Bitmap.FromFile(foregroundPath))
{
// check if heights and widths are the same
if (background.Height == foreground.Height & background.Width == foreground.Width)
{
using (Bitmap mergedImage = new Bitmap(background.Width, background.Height))
{
for (int x = 0; x < mergedImage.Width; x++)
{
for (int y = 0; y < mergedImage.Height; y++)
{
Color backgroundPixel = background.GetPixel(x, y);
Color foregroundPixel = foreground.GetPixel(x, y);
Color mergedPixel = Color.FromArgb(backgroundPixel.ToArgb() & foregroundPixel.ToArgb());
mergedImage.SetPixel(x, y, mergedPixel);
}
}
mergedImage.Save("filepath");
}

}
}
}

像魅力一样工作。谢谢!

关于c# - 在 C# 中将 JPG 与 GDI 合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1071374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com