首页 > 解决方案 > 如何使用 C# 比较两个图像并获得透明背景图像?

问题描述

在此示例中,使用 C# 如何将白色图像与黑色图像进行比较以获得透明背景图像?

在此处输入图像描述

这里的问题是在这段代码中,将白色图片与黑色图片进行比较,并返回背景灰色图片。

var image = new Bitmap(this.pictureBox1.Image.Width, this.pictureBox1.Image.Height);
var rect = new Rectangle(0, 0, this.pictureBox1.Image.Width, this.pictureBox1.Image.Height);

Graphics graphics = Graphics.FromImage(image);
graphics.DrawImage(this.pictureBox1.Image, 0, 0);

var waterMarkImage = new Bitmap(this.pictureBox2.Image.Width, this.pbox_2resim.Image.Height);
for (int y = 0; y < waterMarkImage.Height; y++)
{
    for (int x = 0; x < waterMarkImage.Width; x++)
    {
        var color = (this.pictureBox2.Image as Bitmap).GetPixel(x, y);
        color = Color.FromArgb(50, color.R, color.G, color.B);
        waterMarkImage.SetPixel(x, y, color);
    }
}

graphics.DrawImage(waterMarkImage, rect);

this.pictureBox3.Image = image;

标签: c#imagemerge

解决方案


推荐阅读