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

C# 使用透明背景更改图像颜色,c#,gdi+,system.drawing,C#,Gdi+,System.drawing,我需要使用c#(System.Drawings)将透明背景上带有绿色圆圈的图像加载到位图图像中 这是最简单的部分。但是,在将圆添加到更大的图像之前,我需要更改圆的颜色,而不影响周围的透明度。在我的例子中,我需要将圆圈颜色更改为黄色,并将其添加为太阳 我不能使用固定的黄色圆圈图像,因为所需的颜色是动态的 因此,在下面的代码中,如何在将图像添加到位图之前更改图像的颜色 Image i = Image.FromFile(greenCircleFile); Bitmap b = new Bitmap(5

我需要使用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;
}

这是我的解决方案,您只需要创建一个新控件

然后继承Picturebox来检查这个

public partial class UICirclePicture : PictureBox
{
    [Browsable(false)]
    public int Depth { get; set; }
    [Browsable(false)]
    public SprikiwikiUI Ui
    {
        get { return SprikiwikiUI.Instance; }
    }
    [Browsable(false)]
    public MouseState MouseState { get; set; }

    public UICirclePicture()
    {


        BackColor = Ui.GetApplicationBackgroundColor();
        SizeMode = PictureBoxSizeMode.StretchImage;

    }


    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);
        using (var gp = new GraphicsPath())
        {
            gp.AddEllipse(new Rectangle(0, 0, this.Width - 1, this.Height - 1));
            this.Region = new Region(gp);
        }
    }

}

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

    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;
    }
请确保不要泄漏您创建的位图


请注意,还有其他方法。是指向使用的方法的链接。这允许一个颜色范围被另一个颜色范围替换,因此它可以保持渐变,就像在反序列化图形中一样。

添加了一个示例代码。@Lamar谢谢。投票被撤回:)或者!OP希望绘制光栅图像而不是矢量形状。另外,在
OnResize
中绘图是相当不常用的,我没有用户界面或控件。我只需要编辑一个图像。@Lamar,我明白了,我帮不上忙。。对不起,谢谢!我喜欢听你的推荐信。然而,我将“Color.Gold”应用于红色图像,但它产生了另一种红色。我了解RGB,但不了解矩阵中的其他WA阵列。非常感谢!!我已经用颜色测试了我的版本,但你的代码确实正确。我已经更新了我的答案。我尝试了这个方法,但它似乎将整个图像替换为一种颜色,而不是仅仅替换指定的颜色。