c# - 使用透明背景更改图像颜色

标签 c# gdi+ system.drawing

我需要使用 C# (System.Drawings) 将透明背景上带有绿色圆圈的图像加载到位图图像中。

这是简单的部分。但是我需要在将圆圈添加到更大的图像之前更改圆圈的颜色,而不影响周围的透明度。在我的例子中,我需要将圆圈颜色更改为黄色并将其添加为太阳。

我不能使用固定的黄色圆圈图像,因为所需的颜色是动态的。

那么在下面的代码中,如何在将图像添加到位图之前更改图像的颜色?

Image i = Image.FromFile(greenCircleFile);
Bitmap b = new Bitmap(500, 500);

using(Graphics g = Graphics.FromImage(b))
{
    //--> Here I need to change the color of the green circle to yellow
    //afterwards I can add it to the bitmap image
    g.DrawImage(i, 0, 0, 500, 500);
}

请注意,需要考虑两件事:保持形状(圆形)的抗锯齿效果,以及颜色需要由用户选择并按原样使用以覆盖圆形的原始颜色。

固定:

感谢@TaW,他提供了正确答案。但是有一个小故障,这是对我有用的最终版本:

Image i = Image.FromFile(greenCircleFile);
Bitmap b = new Bitmap(500, 500);

using(Graphics g = Graphics.FromImage(b))
{
    //Here I need to change the color of the green circle to yellow
    i = ChangeToColor(b, Color.Gold)
    //afterwards I can add it to the bitmap image
    g.DrawImage(i, 0, 0, 500, 500);
}

ChangeToColor函数如下:

Bitmap ChangeToColor(Bitmap bmp, Color c)
{
    Bitmap bmp2 = new Bitmap(bmp.Width, bmp.Height);
    using (Graphics g = Graphics.FromImage(bmp2))
    {
        float tr = c.R / 255f;
        float tg = c.G / 255f;
        float tb = c.B / 255f;

        ColorMatrix colorMatrix = new ColorMatrix(new float[][]
          {
            new float[] {0, 0, 0, 0, 0},
            new float[] {0, 0, 0, 0, 0},
            new float[] {0, 0, 0, 0, 0},
            new float[] {0, 0, 0, 1, 0},
            new float[] {tr, tg, tb, 0, 1}
          });

        ImageAttributes attributes = new ImageAttributes();
        attributes.SetColorMatrix(colorMatrix);

        g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height),
            0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);
    }
    return bmp2;
}

最佳答案

这将创建一个新的位图,其中所有非透明像素都强烈地向新颜色移动:

    Bitmap ChangeToColor(Bitmap bmp, Color c)
    {
        Bitmap bmp2 = new Bitmap(bmp.Width, bmp.Height);
        using (Graphics g = Graphics.FromImage(bmp2))
        {
            float tr = c.R / 255f;
            float tg = c.G / 255f;
            float tb = c.B / 255f;

            ColorMatrix colorMatrix = new ColorMatrix(new float[][]
              {
                 new float[] {0, 0, 0, 0, 0},
                 new float[] {0, 0, 0, 0, 0},
                 new float[] {0, 0, 0, 0, 0},
                 new float[] {0, 0, 0, 1, 0},
                 new float[] {tr, tg, tb, 0, 1}  // kudos to OP!
              });

            ImageAttributes attributes = new ImageAttributes();
            attributes.SetColorMatrix(colorMatrix);

            g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height),
                0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);
        }
        return bmp2;
    }

请确保不要泄露您创建的位图!

请注意,还有其他方法。 Here是指向使用 ColorMapping 的方法的链接.这允许将一系列颜色替换为另一个范围,因此它可以保持渐变,就像您在抗锯齿图形中获得的那样。

关于c# - 使用透明背景更改图像颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41777110/

相关文章:

C# 泛型未按预期工作

c# - 使用 gRPC 共享非常大的文件

c# - 使用 C# 的二维图形

c++ - 从图像中提取缩略图的最快方法是什么?

c# - 绘制矩形时出现这种奇怪异常的原因是什么?如何确定?

c# - Image.Save 不将图像数据保存到文件

c# - 在 SQLIte 中使用哪种数据类型来存储 TimeTaken 字段?

c# - Entity Framework 核心 .Include() 问题

c# - 如何等待onPaint完成绘画(C#)

c# - 获取 EXIF 方向标签,旋转到正确的方向,处理图像并以正确的方向保存图像