我知道每个像素的 rgb 值,如何在 C# 中根据这些值创建图片?我见过一些这样的例子:
public Bitmap GetDataPicture(int w, int h, byte[] data)
{
Bitmap pic = new Bitmap(this.width, this.height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Color c;
for (int i = 0; i < data.length; i++)
{
c = Color.FromArgb(data[i]);
pic.SetPixel(i%w, i/w, c);
}
return pic;
}
但它不起作用。
我有一个像这样的二维数组:
1 3 1 2 4 1 3 ...
2 3 4 2 4 1 3 ...
4 3 1 2 4 1 3 ...
...
每个数字对应一个rgb值,例如1 => {244,166,89}
2=>{54,68,125}。
我会尝试下面的代码,它使用 256 的数组Color
调色板条目(您必须提前创建并填写):
public Bitmap GetDataPicture(int w, int h, byte[] data)
{
Bitmap pic = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
int arrayIndex = y * w + x;
Color c = Color.FromArgb(
data[arrayIndex],
data[arrayIndex + 1],
data[arrayIndex + 2],
data[arrayIndex + 3]
);
pic.SetPixel(x, y, c);
}
}
return pic;
}
我倾向于迭代像素,而不是数组,因为我发现读取双循环比读取单循环和模/除运算更容易。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)