0

生の RGB24 データ配列を C# でビットマップに変換しようとしていますが、その際に問題が発生しています。

これは対応するコードです:

using System.Runtime.InteropServices;

byte[] frame;
//... code
frame = new byte[1280 * 960]; 

// code to get the frame

System.Runtime.InteropServices.GCHandle pinnedArray =   
      GCHandle.Alloc(frame, GCHandleType.Pinned);
IntPtr pointer = pinnedArray.AddrOfPinnedObject();

Bitmap bmp = new Bitmap(width, height, 3 * width, 
        PixelFormat.Format24bppRgb, pointer);

MemoryStream JPEGStream = new MemoryStream ();
bmp.Save(filepath, System.Drawing.Imaging.ImageFormat.Bmp);**

私は得る

「System.Drawing.dll で 'System.AccessViolationException' 型の未処理の例外が発生しました」

上記のコードで。

ただし、変更した場合:

Bitmap bmp = new Bitmap(width, height, stride, 
      PixelFormat.Format24bppRgb, pointer);

Bitmap bmp = new Bitmap(width/3, height/3, stride, 
      PixelFormat.Format24bppRgb, pointer);

クラッシュせず、全領域の 1/3 をカバーする 3 つの画像を取得します。私が取得する必要があるのは、1280 X 960 の領域空間全体をカバーする単一の画像です。

4

2 に答える 2

2

Format24bppRgbサンプルで事前に割り当てた 1 ピクセルではなく、1 ピクセルが 24 ビット (3 バイト) かかることを意味します。

ピクセルあたりのビット数を考慮して割り当てられたバイト数を変更します (異なるサイズを使用する場合は、パディングを忘れないでください):

frame = new byte[1280 * 960 * 3]; // 24bpp = 3 bytes
于 2013-08-24T02:00:14.470 に答える
0

width-1 と height-1 を試しましたか?

于 2013-10-02T02:42:48.463 に答える