C# 用C语言将RGB数组转换为图像#
我知道每个像素的rgb值,如何用C#中的这些值创建图片?我见过一些这样的例子:C# 用C语言将RGB数组转换为图像#,c#,image,bitmap,C#,Image,Bitmap,我知道每个像素的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.len
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;
}
公共位图GetDataPicture(int w,int h,字节[]数据)
{
位图pic=新位图(this.width、this.height、System.Drawing.Imaging.PixelFormat.Format32bppArgb);
颜色c;
对于(int i=0;ipublic 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=>{244166,89}
2=>{54,68125}。您的解决方案非常接近工作代码。只需要“调色板”——即一组3个字节的数组,其中每个3个字节的元素包含{R,G,B}值
//palette is a 256x3 table
public static Bitmap GetPictureFromData(int w, int h, byte[] data, byte[][] palette)
{
Bitmap pic = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Color c;
for (int i = 0; i < data.Length; i++)
{
byte[] color_bytes = palette[data[i]];
c = Color.FromArgb(color_bytes[0], color_bytes[1], color_bytes[2]);
pic.SetPixel(i % w, i / w, c);
}
return pic;
}
//调色板是一个256x3表格
公共静态位图GetPictureFromData(int w,int h,字节[]数据,字节[]调色板)
{
位图pic=新位图(w、h、System.Drawing.Imaging.PixelFormat.Format32bppArgb);
颜色c;
for(int i=0;i
这段代码对我有效,但速度非常慢
如果您在内存中创建BMP文件的“image”,然后使用image.FromStream(MemoryStream(“image”)),它的代码会更快,但解决方案更复杂。我会尝试以下代码,它使用256个
颜色的数组作为调色板条目(您必须提前创建和填充):
公共位图GetDataPicture(int w,int h,字节[]数据)
{
位图pic=新位图(w、h、System.Drawing.Imaging.PixelFormat.Format32bppArgb);
对于(int x=0;x
我倾向于迭代像素,而不是数组,因为我发现双循环比单循环和模/除运算更容易读取。你不知道如何创建字节数组??如何创建任何内容的数组。。。sometype[]myname=…上述示例是否可行?您的确切要求是什么?您需要使用每个像素的rgb创建位图图像,或者使用图像创建bytearrray使用每个像素的rgb创建位图图像如何声明调色板变量?@InsideMan实际上这是我代码中的一个错误。“调色板”变量应该是“数据”变量,它的长度为w*h*4字节,包含每个像素的ARGB值。