如何在C#中使用Color[]颜色?
好吧,我发现了这个很酷的代码,我不能使用它。你看。。。我们必须输入一个图像 这我可以做到,但我们也需要输入颜色,我不知道怎么做如何在C#中使用Color[]颜色?,c#,colors,C#,Colors,好吧,我发现了这个很酷的代码,我不能使用它。你看。。。我们必须输入一个图像 这我可以做到,但我们也需要输入颜色,我不知道怎么做 public static Bitmap Colorize(Bitmap Image, Color[] Colors) { if (Colors.Length < 256) return null; Bitmap TempBitmap = new Bitmap(Image.Width, Image.Height); for (
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;
}
公共静态位图着色(位图图像,颜色[]颜色)
{
如果(颜色.长度<256)
返回null;
位图TempBitmap=新位图(Image.Width、Image.Height);
对于(int x=0;x
您需要传入一组颜色对象,如下所示:
Bitmap bitmapToColorize = new Bitmap(@"C:\bitmap.bmp");
Color[] colors = new Color[2];
colors[0] = Color.Blue;
colors[1] = Color.Green;
Colorize(bitmapToColorize, colors);
当然,看看这个方法,看起来需要用至少256种颜色填充颜色数组
我建议您继续阅读。知道这段代码应该做什么会很有趣。@Etienne:这不是很明显吗?它会“着色”。:D但说真的,它看起来像是用不同的调色板将一些图像重绘到一个新的图像上。@Cody:你到底在说什么?@Cody可能它被编辑了,但ColorUsing被定义为int。为什么这个问题会被否决?该方法获取颜色数组和位图图像,并基于现有图像的红色分量为返回的位图选择新颜色。我并不是说这是一个很好的方法,但我看不出这个问题有任何错误。这段代码应该将黑白图像转换为彩色图像。当然,数组应该至少为color[256],所以该方法不会返回null。但这似乎是发问者寻找的答案+1好的,我该怎么做才能不返回null?您需要使用256色的数组。