Byte数组或矩阵位图 [英] Byte array or matrix to BitMap

查看:140
本文介绍了Byte数组或矩阵位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有以下问题:我想转换来自文件具有以下配置的字节数组:

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.

所以,我想要做的是这些字节转换成位图,而不必因为时间太长设置像素的像素与 bitmap.setPixel

有什么建议?在此先感谢!

Any suggestions? Thanks in advance!

推荐答案

如果您有字节[] 像素,宽度和高度,那么你就可以使用的BitmapData 来的字节写入位图,因为你也知道的格式。这里有一个例子:

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. 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.

您将需要在你的C#文件的顶部导入这三个名字空间,以最低的:

You will need to import these three namespaces at the top of your C# file, at minimum:

using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

这篇关于Byte数组或矩阵位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆