将内存中的字节复制到 VB.NET 中的数组
不幸的是,我无法在当前项目中诉诸 C#,因此我必须在不使用 unsafe 关键字的情况下解决此问题。
我有一个位图,我需要直接访问像素和通道值。我想超越 Marshal.ReadByte() 和 Marshal.WriteByte() (并且肯定超越 GetPixel 和 SetPixel)。
有没有办法将位图的所有像素数据放入一个同时适用于 32 和 64 位系统的 Byte 数组中?我想要与原始位图完全相同的布局,因此还需要包括每行的填充(如果存在)。
马歇尔似乎没有类似的东西:
byte[] ReadBytes(IntPtr start, int offset, int count)
除非我完全错过了......
非常感谢任何帮助, 大卫
附言。到目前为止,我的所有图像都是 32BppPArgb 像素格式。
unfortunately I cannot resort to C# in my current project, so I'll have to solve this without the unsafe keyword.
I've got a bitmap, and I need to access the pixels and channel values directly. I'd like to go beyond Marshal.ReadByte() and Marshal.WriteByte() (and definitely beyond GetPixel and SetPixel).
Is there a way to put all the pixel data of the bitmap into a Byte array that works on both 32 and 64 bit systems? I want the exact same layout as the original bitmap, so the padding for each row (if it exists) also needs to be included.
Marshal doesn't seem to have something akin to:
byte[] ReadBytes(IntPtr start, int offset, int count)
Unless I totally missed it...
Any help greatly appreciated,
David
ps. So far all my images are in 32BppPArgb pixelformat.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Marshal 确实有一种方法可以完全满足您的要求。请参阅 Marshall.Copy()
另一个方向也有超载
Marshal does have a Method that does exactly what you are asking. See Marshall.Copy()
And there are overloads to go the other direction as well
这样的事情会做吗? (未经测试):
它不会让您直接操作 Drawing.Bitmap 对象中的像素,但它会让您根据问题标题将该位图复制到字节数组。
另一种选择是通过 BinaryFormatter 进行序列化,但我认为这仍然需要您通过 MemoryStream 传递它。
Would something like this do? (untested):
It won't let you manipulate the pixels in a Drawing.Bitmap object directly, but it will let you copy that bitmap to a byte array, as per the question title.
Another option is serialization via the BinaryFormatter, but I think that will still require you to pass it through a MemoryStream.
VB 不提供直接内存访问的方法。您有两种选择:
好吧,还有第三种选择。 VB.Net本身并不支持直接内存访问,但它是可以实现的。它只是丑陋且容易出错。尽管如此,如果您愿意付出努力,您可以尝试使用 这些技术与前面提到的方法相结合。
VB does not offer methods for direct memory access. You have two choices:
Alright, there is a third option. VB.Net does not inherently support direct memory access, but it can be accomplished. It's just ugly and prone to errors. Nonetheless, if you're willing to put in the effort, you can try building a bitmap access library using these techniques combined with the approach referenced previously.
shf301 是正确的,但我想添加一个链接,指向有关快速像素数据访问的全面解释/教程。与其将图像保存到流中并访问内存中的文件,不如锁定位图,将像素数据复制出来,访问它,然后将其复制回来。这种技术的性能非常好。
代码采用 C# 编写,但该方法与语言无关且易于阅读。
http://ilab.ahemm.org/tutBitmap.html
shf301 is right on the money, but I'd like to add a link to a comprehensive explanation/tutorial on fast pixel data access. Rather than saving the image to a stream and accessing a file-in-memory, it would be better to lock the bitmap, copy pixel data out, access it, and copy it back in. The performance of this technique is pretty good.
Code is in c#, but the approach is language-neutral and easy to read.
http://ilab.ahemm.org/tutBitmap.html