how to convert a raw RGB data array to a bitmap in C#
I am trying to convert a raw RGB24 data array into a bitmap in C#, but I
am running into trouble in doing so.
This is the corresponding code:
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);**
I get a
"An unhandled exception of type 'System.AccessViolationException' occurred
in System.Drawing.dll"
with the code above.
However if i change:
Bitmap bmp = new Bitmap(width, height, stride,
PixelFormat.Format24bppRgb, pointer);
to
Bitmap bmp = new Bitmap(width/3, height/3, stride,
PixelFormat.Format24bppRgb, pointer);
I do not crash and get 3 images covering 1/3 of the total area. What I
should be getting is a single image that covers the entire 1280 X 960 area
space.
No comments:
Post a Comment