C# Color an image with shadows

前端 未结 2 1513
刺人心
刺人心 2021-01-22 06:51

I am currently creating sort of a game with C# and am trying to create outfits for the players. I would like to make cloth design and let players chose the colors.

I too

2条回答
  •  粉色の甜心
    2021-01-22 07:45

    The simplest (and fastest) way to color (tint) an image is to use a ColorMatrix.

    Here is the result of using nine colors to tint the original:

    Note that I have photoshopped the posted image bo be transparent around the center part; using just the original looks like this..:

    ((The glitch in the lower right is in the original..))

    Here is a function the returns a list of tinted version of an image, one for each color in a list..:

    List TintImages(Bitmap bmp0, List colors )
    {
        List tinted = new List();
        Size sz = bmp0.Size;
        float f = 256f;
        for (int i = 0; i < colors.Count; i++)
        {
            float r = colors[i].R / f;
            float g = colors[i].G / f;
            float b = colors[i].B / f;
    
            float[][] colorMatrixElements = {
                new float[] {r,  0,  0,  0, 0},        // red scaling factor of 
                new float[] {0,  g,  0,  0, 0},        // green scaling factor 
                new float[] {0,  0,  b,  0, 0},        // blue scaling factor 
                new float[] {0,  0,  0,  1, 0},        // alpha scaling factor 
                new float[] {0, 0, 0, 0, 1}};          // no further translations
            ImageAttributes imageAttributes = new ImageAttributes();
            ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
            imageAttributes.SetColorMatrix(
                colorMatrix,
                ColorMatrixFlag.Default,
                ColorAdjustType.Bitmap);
            Bitmap bmp = new Bitmap(sz.Width, sz.Height);
            using (Graphics gr = Graphics.FromImage(bmp))
            {
                gr.DrawImage(bmp0, new Rectangle(0, 0, sz.Width, sz.Height),
                0, 0, sz.Width, sz.Height, GraphicsUnit.Pixel, imageAttributes);
                tinted.Add(bmp);
            }
        }
        return tinted;
    }
    

提交回复
热议问题