且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何创建从字节[]在C#中的bmp文件

更新时间:2023-02-08 18:31:54

如果数组实际上包含位图文件,那么你可以字节保存为一个文件:

If the array actually contains a bitmap file, then you can just save the bytes as a file:

File.WriteAllBytes(fileName, imageData);

如果该数组只包含原始像素数据,可以使用数据创建一个位图对象:

If the array contains only raw pixel data, you can create a Bitmap object using the data:

unsafe {
   fixed (byte* ptr = imageData) {
      using (Bitmap image = new Bitmap(width, height, stride, PixelFormat.Format24bppRgb, new IntPtr(ptr))) {
         image.Save(fileName);
      }
   }
}

步幅值是扫描线之间的字节数。如果在扫描行之间没有空白,它的宽* 3 为和24bpp格式。

The stride value is the number of bytes between the scan lines. If there is no padding between the scan lines, it's width * 3 for a 24bpp format.

此方法使用阵列中的数据而不在存储器中的整个图象的另一副本(这就是为什么它需要的步幅值)。

This method uses the data in the array without creating another copy of the entire image in memory (which is why it needs the stride value).

如果位图数据被存储颠倒的阵列中,在步幅值应该是负的,和指针应该在存储器中的最后的扫描线的开始( PTR +步速*(高 - 1)。

If the bitmap data is stored upside down in the array, the stride value should be negative, and the pointer should be the start of the last scan line in memory (ptr + stride * (height - 1)).