C# 用阴影给图像着色

标签 c# bitmap

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

我从 TibiaME (tibiame.com) 的游戏文件中拍摄了照片,它几乎满足了我的要求。

Hat picture without colors

Hat picture orange Hat picture green

如何为该表格填充颜色?当我尝试替换某种颜色时,它不起作用,因为它在每个地方都不相同。阴影看起来很酷:P

最佳答案

对图像进行着色(着色)的最简单(也是最快)的方法是使用 ColorMatrix .

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

enter image description here

请注意,我已经对发布的图像进行了 Photoshop 处理,使其中心部分周围透明;只使用原来的看起来像这样..:

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

关于C# 用阴影给图像着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51526858/

相关文章:

c# - 页面上的 GridView 不会刷新,即使再次调用 DataBind

c# - 以编程方式绑定(bind)数据源时,组合框选择事件自动触发 C#

c# - 不可空类型

android - 任何算法来检查包含特定颜色的每个像素?在安卓 :)

python从二维数组制作位图数据

c++在位图上绘制图像并保存

android - 如何上传 gif 文件以在 android 中解析为解析文件

c# - 高效地写入日志文件

c# - NLog 控制台应用程序

java - 从 ImageView 获取图像并使用编码位图将此图像发送到其他 fragment