如何将位图转换为int[]?
我正在编写一个程序来在 GPU 上进行一些图像处理。为此,我使用 CUDA.Net,但不幸的是 CUDA 无法识别类型 byte,我能够使用此代码存储像素信息:
BitmapData bData = bmp.LockBits(new Rectangle(new Point(), bmp.Size),
ImageLockMode.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;
我的问题在于以下事实: CUDA 不采用此字节数组,如果将其更改为 int[] 类型,程序将检索 AccessViolationException。
有人有解决这个问题的想法吗?
提前致谢。
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),
ImageLockMode.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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
位图保证是 4 字节的倍数。所以 int[] 会起作用:
但这对于接收者来说并不容易,我建议您使用 PixelFormat.Format32bppRgb 这样一个像素正好是 int 的大小。
A bitmap is guaranteed to be a multiple of 4 bytes. So int[] will work:
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.