I'm trying to copy a managed Bitmap to an unmanaged float array (for usage with Opencl.net wrapper's Cl.CreateImage2D). Unfortunately I'm getting an exception, however if I divide the array length (srcIMGBytesSize) by 4, I'm succeeding. Is there a problem with my array's length? The image format is Format32bppArgb. I'm using mono.
System.Drawing.Bitmap bmpImage = new System.Drawing.Bitmap(inputImage);
bitmapData = bmpImage.LockBits( new Rectangle(0, 0, bmpImage.Width, bmpImage.Height), ImageLockMode.ReadOnly, inputImage.PixelFormat);
IntPtr srcBmpPtr = bitmapData.Scan0;
int bitsPerPixel = Image.GetPixelFormatSize( inputImage.PixelFormat );
srcIMGBytesSize = bitmapData.Stride * bitmapData.Height;
float[] srcImage2DData = new float[srcIMGBytesSize];
Marshal.Copy(srcBmpPtr, srcImage2DData, 0, srcIMGBytesSize); //Exception at this line
bmpImage.UnlockBits( bitmapData );
I'm getting the following exception when trying to copy the data into float[] array:
System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
at System.Runtime.InteropServices.Marshal.CopyToManaged(IntPtr source, Object destination, Int32 startIndex, Int32 length)
at System.Runtime.InteropServices.Marshal.Copy(IntPtr source, Single[] destination, Int32 startIndex, Int32 length)
Thank you!