我正在尝试将托管位图复制到非托管浮点数组(用于 Opencl.net 包装器的 Cl.CreateImage2D)。不幸的是,我遇到了一个异常,但是如果我将数组长度 (srcIMGBytesSize) 除以 4,我就成功了。我的数组长度有问题吗?图像格式为 Format32bppArgb。我正在使用单声道。
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 );
尝试将数据复制到 float[] 数组时出现以下异常:
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)
谢谢!