C#用阴影为图像着色

时间:2018-07-25 20:05:05

标签: c# bitmap

我目前正在使用C#创建某种游戏,并且正在尝试为玩家创建装备。我想进行布料设计,让玩家选择颜色。

我从TibiaME(tibiame.com)的游戏文件中拍摄了照片,该照片确实做了我想做的事情。

Hat picture without colors

Hat picture orange Hat picture green

如何用颜色填充此表单?当我尝试更换某种颜色时,它不起作用,因为每种颜色都不相同。阴影看起来很酷:P

2 个答案:

答案 0 :(得分:2)

最简单(也是最快)的为图像着色(着色)的方法是使用ColorMatrix

这是使用九种颜色对原始色彩进行着色的结果:

enter image description here

请注意,我已经对所发布的图像进行了照片处理,并且在中心部分周围是透明的;只使用原始的样子..:

enter image description here

((右下角的毛刺在原稿中。)

这是一个函数,用于返回图像的着色版本列表,列表中每种颜色对应一个。.

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

答案 1 :(得分:1)

您可以遍历位图的每个像素,并沿所需方向进行颜色偏移。当我说colorshoft时,我的意思是您必须调整每个像素的RGB值。

向红色的简单转变可能看起来像这样:

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

请记住,这只是一个简单的示例,可能无法达到您的预期。 您可以在RGB color model上阅读有关RGB色彩空间的更多信息,并在这里找到RGB Color Codes Chart