C# Color an image with shadows

前端 未结 2 1514
刺人心
刺人心 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:40

    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

    0 讨论(0)
  • 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<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;
    }
    
    0 讨论(0)
提交回复
热议问题