[英]Byte array or matrix to BitMap
I am currently having the following problem: I want to convert a byte array that comes from a file with the following configuration: 我目前遇到以下问题:我想转换来自具有以下配置的文件的字节数组:
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.
So what I want to do is convert these bytes into a bitmap without having to set pixel by pixel with bitmap.setPixel
because it takes too long. 所以我想做的是将这些字节转换为位图,而不必使用
bitmap.setPixel
逐像素设置,因为它需要太长时间。
Any suggestions? 有什么建议么? Thanks in advance!
提前致谢!
If you have the byte[]
of the pixels, and the width and height, then you can use BitmapData
to write the bytes to the bitmap since you also know the format. 如果你有像素的
byte[]
,以及宽度和高度,那么你可以使用BitmapData
将字节写入位图,因为你也知道格式。 Here's an example: 这是一个例子:
//Your actual bytes
byte[] bytes = {255, 0, 0, 0, 0, 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, 0, data.Scan0, bytes.Length);
bitmap.UnlockBits(data);
This is a very fast operation. 这是一个非常快速的操作。
You will need to import these three namespaces at the top of your C# file, at minimum: 您至少需要在C#文件的顶部导入这三个命名空间:
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.