在 C# 中将 JPG 与 GDI 合并

发布于 2024-07-26 03:03:23 字数 209 浏览 2 评论 0原文

我的场景:

  • 我有一张彩色背景图像 JPG。
  • 我有一张白底黑字 JPG。
  • 两个图像的尺寸相同(高度和宽度),

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

我如何在 C# 中使用 GDI 来做到这一点?

谢谢!

My scenario:

  • I have one color background image JPG.
  • I have one black text on white background 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#?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

负佳期 2024-08-02 03:03:23

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

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

          }
     }
}

奇迹般有效。 谢谢!

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

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!

温折酒 2024-08-02 03:03:23

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

如果它们大小不同,请先缩放。

我是凭空想象出来的,但类似这样的:

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

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());
这个俗人 2024-08-02 03:03:23

存在更简单、更快的方法。 当您绘制必须部分可见的图像时,应该使用 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 方法将使指定范围内的颜色透明,因此您可以使白色位图像素透明,包括受 jpeg 压缩伪影影响的所有像素。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文