image processing - how to convert a raw RGB data array to a bitmap in C# -


i trying convert raw rgb24 data array bitmap in c#, running trouble in doing so.

this corresponding code:

using system.runtime.interopservices;  byte[] frame; //... code frame = new byte[1280 * 960];   // code 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 a

"an unhandled exception of type 'system.accessviolationexception' occurred in system.drawing.dll"

with code above.

however if 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 not crash , 3 images covering 1/3 of total area. should getting single image covers entire 1280 x 960 area space.

format24bpprgb means 1 pixel takes 24 bits (3 bytes), not 1 pre-allocate in sample.

change amount of bytes allocated account bits-per-pixel (in bytes, if using different sizes don't forget padding):

frame = new byte[1280 * 960 * 3]; // 24bpp = 3 bytes 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -