我的场景:
- 我有一张彩色背景图片 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/