6

現在、次の問題があります: 次の構成のファイルからのバイト配列を変換したい:

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時間がかかりすぎるので、ピクセルごとに設定することなく、これらのバイトをビットマップに変換することです。

助言がありますか?前もって感謝します!

4

1 に答える 1

11

byte[]ピクセル数と幅と高さがある場合は、フォーマットもわかっているため、ビットマップにバイトを書き込むために使用できますBitmapData。次に例を示します。

//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);

これは非常に高速な操作です。

少なくとも、C# ファイルの先頭に次の 3 つの名前空間をインポートする必要があります。

using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
于 2012-06-07T14:35:53.153 に答える