How to convert a bitmap to int[]?
I'm writing a program to do some image processing on the GPU. For this I'm using CUDA.Net, but unfortunaly the CUDA doesn't recognize the type byte, in which I was able to store the pixels information using this code:
BitmapData bData = bmp.LockBits(new Rectangle(new Point(), bmp.Size),
ImageLock开发者_JS百科Mode.ReadOnly,
PixelFormat.Format24bppRgb);
// number of bytes in the bitmap
byteCount = bData.Stride * (bmp.Height);
byte[] bmpBytes = new byte[byteCount];
Marshal.Copy(bData.Scan0, bmpBytes, 0, byteCount);
bmp.UnlockBits(bData);
return bmpBytes;
My problem resides in the fact that CUDA doesn't take this byte array, and if change it to a type int[] the program retrieves an AccessViolationException.
Does someone has any thoughts to resolve this problem?
Thanks in advance.
A bitmap is guaranteed to be a multiple of 4 bytes. So int[] will work:
int[] bytes = new int[byteCount/4];
Marshal.Copy(bData.Scan0, bytes, 0, byteCount/4);
That doesn't make it easy for the receiver though, I'd suggest you use PixelFormat.Format32bppRgb so one pixel is exactly the size of an int.
精彩评论