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

标签 c# transparency gdi jpeg

我的场景:

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

相关文章:

c# - 如何将字符串转换为本地日期时间?

c# - 为什么 ClearType 部分不能与透明窗口一起使用?

c# - 使用多个监视器获取整个屏幕的设备上下文

c# - 如何控制winform mschart图例文本对齐c#?

c# - 在 ASP.NET Core Web API Controller 中使用 C# 7 元组

c# - 有没有办法在 C# 中复制或制作 XmlNodeList 的另一个副本?

android - 是否可以在 Android 中设置 View 的透明度?

java - Java 中的透明度重叠

gdi+ - 如何在gdi/gdi+中绘制柔和的线条

c++ - 如何用字体计算多字符串的宽度和高度?