C#用阴影为图像着色 [英] C# Color an image with shadows

查看:134
本文介绍了C#用阴影为图像着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

I took pictures from gamefiles of TibiaME (tibiame.com), which does pretty much what i want to to.

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

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

推荐答案

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

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

这篇关于C#用阴影为图像着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆