我目前遇到以下问题:我想转换来自具有以下配置的文件的字节数组:
Byte1: R color of pixel 0,0. Byte2: G color of pixel 0,0. Byte3: B color of pixel 0,0. Byte4: R color of pixel 0,1. ... ByteN: R color of pixel n,n.@H_301_4@所以我想做的是将这些字节转换为位图,而不必使用bitmap.setPixel逐像素设置,因为它需要太长时间. @H_301_4@有什么建议?提前致谢!
解决方法
如果你有像素的byte [],以及宽度和高度,那么你可以使用BitmapData将字节写入位图,因为你也知道格式.这是一个例子:
//Your actual bytes byte[] bytes = {255,255}; var width = 2; var height = 1; //Make sure to clean up resources var bitmap = new Bitmap(width,height); var data = bitmap.LockBits(new Rectangle(Point.Empty,bitmap.Size),ImageLockMode.WriteOnly,PixelFormat.Format24bppRgb); Marshal.Copy(bytes,data.Scan0,bytes.Length); bitmap.UnlockBits(data);@H_301_4@这是一个非常快速的操作. @H_301_4@您至少需要在C#文件的顶部导入这三个命名空间:
using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices;