使用透明背景更改图像颜色 [英] Change image color with transparent background

查看:277
本文介绍了使用透明背景更改图像颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to load an image with green circle over a transparent background into a bitmap image using c# (System.Drawings).

那是容易的部分.但是,在将圆添加到较大图像之前,我需要先更改圆的颜色,而又不影响周围的透明度.就我而言,我需要将圆圈颜色更改为黄色,并将其添加为太阳.

That's the easy part. However I need to change the color of the circle before adding it to the bigger image, without affecting the transparency of the surrounding. In my case I need to change the circle color to yellow and add it as a sun.

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

I can't use fixed yellow circle image because the desired color is dynamic.

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

So in the code below, how can I change the color of the image before adding it to the bitmap?

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

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

Please note that two things need to be into consideration: Keeping the anti-aliasing of the shape (circle), and the color needs to be picked by user and used as is to overlay the original color of the circle.

已修复:

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

Thanks to @TaW, he provided the correct answer. However with a glitch, here's the final version that worked for me:

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函数如下:

While ChangeToColor function is as follows:

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

推荐答案

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

This will create a new Bitmap with all non-transparent pixels moved strongly toward a new color:

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

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

do make sure not to leak the Bitmaps you create!

请注意,还有其他方法. 此处是该方法的链接使用 ColorMapping .这样一来,就可以将一个颜色范围替换为另一个颜色范围,这样就可以保持渐变效果,就像您在抗锯齿图形中看到的一样..

Note that there are other methods as well. Here is a link to a method that uses ColorMapping. This allows for a range of colors to be replaced by another range, so it can keep gradients like the ones you get in anti-alised graphics..

这篇关于使用透明背景更改图像颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆