C# Color an image with shadows

2020-04-17 05:59发布

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 took pictures from gamefiles of TibiaME (tibiame.com), which does pretty much what i want to to.

Hat picture without colors

Hat picture orange Hat picture green

How can I Fill this form with color? When I try to replace a certain color, it does not work, since it's not the same everyhwere. The shadows look pretty cool :P

标签: c# bitmap
2条回答
老娘就宠你
2楼-- · 2020-04-17 06:11

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:

enter image description here

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

enter image description here

((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<Bitmap> TintImages(Bitmap bmp0, List<Color> colors )
{
    List<Bitmap> tinted = new List<Bitmap>();
    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;
}
查看更多
家丑人穷心不美
3楼-- · 2020-04-17 06:27

You could iterate over each pixel of the bitmap and make a color shift in the direction you want. When I say colorshoft I mean a you have to adapt the RGB values of each pixel.

A simple shift to red could look like this:

for (int Xcount = 0; Xcount < myBitmap.Width; Xcount++)
{
    for (int Ycount = 0; Ycount < myBitmap.Height; Ycount++)
    {
        //get color of the pixel
        Color pixelColor = myBitmap.GetPixel(Xcount, Ycount);
        byte red = pixelColor.R;
        byte green = pixelColor.G;
        byte blue = pixelColor.B;

        //make shift and prevent overflow
        if (red < 205)
            red += 50;
        else
            red = 255;

        //set color of the pixel
        myBitmap.SetPixel(Xcount, Ycount, Color.FromRgb(red, green, blue));
    }
}

Keep in mind this is just a simple example and may not result in what you expected. You can read more about the RGB colorspace here: RGB color model and here you find a RGB Color Codes Chart

查看更多
登录 后发表回答