我的情况:


我有一个彩色背景图像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");
               }

          }
     }
}


奇迹般有效。谢谢!

07-25 22:47
查看更多