Copy memory buffer to BitmapData

I am trying to copy the unpacked image to a BitmapData object using copy_mem_image(), however it gives an error saying Access Violation. Where is the problem? My code is as follows:

int check = Processor1->dcraw_process();
libraw_processed_image_t *image = Processor1->dcraw_make_mem_image(&check);
int step;
step = image->width*(24/8)*3;
//create a bitmap with the image size
Bitmap^ pic = gcnew Bitmap(image->width, image->height);				
System::Drawing::Rectangle rect = System::Drawing::Rectangle(0,0,image->width,image->height);
BitmapData^ bmpData = pic->LockBits( rect, ImageLockMode::ReadWrite, PixelFormat::Format24bppRgb );
// Get the address of the first line.
IntPtr bmpPtr = bmpData->Scan0;
Processor1->copy_mem_image(&bmpPtr, step, 0);
// Unlock the bits.
pic->UnlockBits( bmpData );


Your 'step' calculation is

Your 'step' calculation is wrong. copy_mem_image accept step in bytes, for 8-bit images it is width*3.
You calculate step as width*(24/8)*3 so width*9.

-- Alex Tutubalin

Should the 'step', which is

Should the 'step', which is also called stride for bitmap, has to be a multiple of 4?? If width*3 is not multiple of 4, would it gives an error when creating an bitmap object??

copy_mem_image() copies image

copy_mem_image() copies image data without alpha channel (4th byte).

If you need alpha channel in your bitmap, you need to use per-pixel copy in a loop.

-- Alex Tutubalin

Thanks for your quite reply,

Thanks for your quick reply, but even though I changed the 'step' to image->width*3, it still gives me an error at the line of copy_mem_image(&bmpPtr, step, 0). The error is: Unhandled exception at 0x77ea15de in Raw_Tool.exe: 0xC0000005: Access violation. Could you tell me why?

It may be caused by read-only

It may be caused by read-only memory in .Net Bitmap object (I know nothing about Bitmap type).

After you've called dcraw_make_mem_image, the RGB pixels are contained in image->data. You may try to create Bitmap directly from this array without extra copy_mem_image() call.

-- Alex Tutubalin

It seems that for rotated

It seems that for rotated images (.imgdata.sizes.flip&4) you need to use height for stride calculation. And keep in mind that you will receive rotated image with width and height swapped.


dcraw_make_mem_image() returns correct width/height,

-- Alex Tutubalin