【问题标题】:How do I use Color[] Colors in C#? [closed]如何在 C# 中使用 Color[] 颜色? [关闭]
【发布时间】:2011-07-03 03:17:51
【问题描述】:

好的,我找到了这个很酷的代码,但我不能使用它。你看...我们必须输入一个图像 我可以做到,但我们还需要输入我不知道该怎么做的颜色...

public static Bitmap Colorize(Bitmap Image, Color[] Colors)
{
    if (Colors.Length < 256)
       return null;
    Bitmap TempBitmap = new Bitmap(Image.Width, Image.Height);
    for (int x = 0; x < Image.Width; ++x)
    {
        for (int y = 0; y < Image.Height; ++y)
        {
           int ColorUsing = Image.GetPixel(x, y).R;
            TempBitmap.SetPixel(x, y, Colors[ColorUsing]);
        }
    }
    return TempBitmap;
}

【问题讨论】:

  • 知道这段代码应该做什么会很有趣。
  • @Etienne:这不是很明显吗?它执行“着色”。 :D 但说真的,它看起来像是使用不同的调色板将一些图像重绘到新的图像上。
  • @Cody:你到底在说什么?
  • @Cody 可能已被编辑,但 ColorUsing 被定义为 int。为什么这个问题会被否决?该方法采用颜色数组和位图图像,并根据现有图像的红色分量为返回的位图选择新颜色。我并不是说这是一个很好的方法,但我认为这个问题没有任何问题。
  • 此代码应该将黑白图像转换为彩色图像。

标签: c# colors


【解决方案1】:

需要传入一个Color对象数组,如下:

        Bitmap bitmapToColorize = new Bitmap(@"C:\bitmap.bmp");
        Color[] colors = new Color[2];
        colors[0] = Color.Blue;
        colors[1] = Color.Green;

        Colorize(bitmapToColorize, colors);

当然,看方法,貌似需要用至少256种颜色填充Color数组。

我建议你阅读arrays

【讨论】:

  • 当然,数组至少应该是 Color[256],所以该方法不会返回 null。但这似乎是提问者正在寻找的答案。 +1
  • 好的,我该怎么做才能让事情不返回 null ?
  • 你需要使用256色的数组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
  • 1970-01-01
  • 2014-09-27
  • 1970-01-01
  • 1970-01-01
  • 2022-10-25
  • 2022-07-11
相关资源
最近更新 更多