Change image color with transparent background

好久不见. 提交于 2019-12-05 14:05:56
TaW

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!

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..

Here's my solution you just need to create a new Control

then inherit the Picturebox check this out.

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

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!