在C#中将JPG与GDI合并

时间:2022-02-08 21:18:05

My scenario:

  • I have one color background image JPG.
  • 我有一个彩色背景图像JPG。

  • I have one black text on white background JPG.
  • 我在白色背景JPG上有一个黑色文本。

  • Both images are the same size (height and width)
  • 两个图像大小相同(高度和宽度)

I want to overlay the image with black text and white background over the color background image, i.e. the white background becomes transparent to see the color background beneath it.

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

How can I do this with GDI in C#?

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

Thanks!

4 个解决方案

#1


Thanks to GalacticCowboy I was able to come up with this solution:

感谢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");
               }

          }
     }
}

Works like a charm. Thanks!

奇迹般有效。谢谢!

#2


If the images are the same size, iterate over them and "AND" the colors for each pixel. For the white pixels, you should get the color of the other image, and for the black ones you should get black.

如果图像大小相同,则迭代它们并“对”每个像素的“颜色”。对于白色像素,您应该获得另一个图像的颜色,而对于黑色像素,您应该获得黑色。

If they're not the same size, scale first.

如果它们的尺寸不同,请先缩放。

I'm making this up off the top of my head, but something like:

我正在把它放在我的头顶,但是类似于:

Color destColor = Color.FromArgb(pixel1.ToArgb() & pixel2.ToArgb());

#3


There exist easier and faster way. You should use ImageAttributes when you draw image that must be partially visible.

存在更容易和更快捷的方式。绘制必须部分可见的图像时,应使用ImageAttributes。

Image BackImage = Image.FromFile(backgroundPath);
using (Graphics g = Graphics.FromImage(BackImage))
{
    using (ForeImage = Image.FromFile(foregroundPath))
    {   
        ImageAttributes imageAttr = new ImageAttributes();
        imageAttr.SetColorKey(Color.FromArgb(245, 245, 245), Color.FromArgb(255, 255, 255),
            ColorAdjustType.Default);
        g.DrawImage(ForeImage, new Rectangle(0, 0, BackImage.Width, BackImage.Height),
            0, 0, BackImage.Width, BackImage.Height, GraphicsUnit.Pixel, imageAttr);
    }
}

SetColorKey method will make color from specified range transparent, so you can make your white bitmap pixels transparent, including all pixels that are affected to jpeg compression artefacts.

SetColorKey方法将使指定范围内的颜色透明,因此您可以使白色位图像素透明,包括受jpeg压缩伪影影响的所有像素。

#4


check out this article. it give you code for making a specified color as the transparent color in your image http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/

看看这篇文章。它为您提供了将指定颜色作为图像中透明色的代码http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/

#1


Thanks to GalacticCowboy I was able to come up with this solution:

感谢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");
               }

          }
     }
}

Works like a charm. Thanks!

奇迹般有效。谢谢!

#2


If the images are the same size, iterate over them and "AND" the colors for each pixel. For the white pixels, you should get the color of the other image, and for the black ones you should get black.

如果图像大小相同,则迭代它们并“对”每个像素的“颜色”。对于白色像素,您应该获得另一个图像的颜色,而对于黑色像素,您应该获得黑色。

If they're not the same size, scale first.

如果它们的尺寸不同,请先缩放。

I'm making this up off the top of my head, but something like:

我正在把它放在我的头顶,但是类似于:

Color destColor = Color.FromArgb(pixel1.ToArgb() & pixel2.ToArgb());

#3


There exist easier and faster way. You should use ImageAttributes when you draw image that must be partially visible.

存在更容易和更快捷的方式。绘制必须部分可见的图像时,应使用ImageAttributes。

Image BackImage = Image.FromFile(backgroundPath);
using (Graphics g = Graphics.FromImage(BackImage))
{
    using (ForeImage = Image.FromFile(foregroundPath))
    {   
        ImageAttributes imageAttr = new ImageAttributes();
        imageAttr.SetColorKey(Color.FromArgb(245, 245, 245), Color.FromArgb(255, 255, 255),
            ColorAdjustType.Default);
        g.DrawImage(ForeImage, new Rectangle(0, 0, BackImage.Width, BackImage.Height),
            0, 0, BackImage.Width, BackImage.Height, GraphicsUnit.Pixel, imageAttr);
    }
}

SetColorKey method will make color from specified range transparent, so you can make your white bitmap pixels transparent, including all pixels that are affected to jpeg compression artefacts.

SetColorKey方法将使指定范围内的颜色透明,因此您可以使白色位图像素透明,包括受jpeg压缩伪影影响的所有像素。

#4


check out this article. it give you code for making a specified color as the transparent color in your image http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/

看看这篇文章。它为您提供了将指定颜色作为图像中透明色的代码http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/