将内存中的字节复制到 VB.NET 中的数组

发布于 2024-08-11 12:04:45 字数 460 浏览 4 评论 0原文

不幸的是,我无法在当前项目中诉诸 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云淡风轻 2024-08-18 12:04:45

Marshal 确实有一种方法可以完全满足您的要求。请参阅 Marshall.Copy()

public static void Copy(
    IntPtr source,
    byte[] destination,
    int startIndex,
    int length
   )

从非托管内存复制数据
指向托管 8 位无符号指针
整数数组。

另一个方向也有超载

Marshal does have a Method that does exactly what you are asking. See Marshall.Copy()

public static void Copy(
    IntPtr source,
    byte[] destination,
    int startIndex,
    int length
   )

Copies data from an unmanaged memory
pointer to a managed 8-bit unsigned
integer array.

And there are overloads to go the other direction as well

生来就爱笑 2024-08-18 12:04:45

这样的事情会做吗? (未经测试):

Public Shared Function BytesFromBitmap(ByVal Image As Drawing.Bitmap) As Byte()
   Using buffer As New IO.MemoryStream()
        image.Save(result, Drawing.Imaging.ImageFormat.Bmp)

        Using rdr As New IO.BinaryReader(buffer)
            Return rdr.ReadBytes(buffer.Length)
        End Using
    End Using
End Function

它不会让您直接操作 Drawing.Bitmap 对象中的像素,但它会让您根据问题标题将该位图复制到字节数组。

另一种选择是通过 BinaryFormatter 进行序列化,但我认为这仍然需要您通过 MemoryStream 传递它。

Would something like this do? (untested):

Public Shared Function BytesFromBitmap(ByVal Image As Drawing.Bitmap) As Byte()
   Using buffer As New IO.MemoryStream()
        image.Save(result, Drawing.Imaging.ImageFormat.Bmp)

        Using rdr As New IO.BinaryReader(buffer)
            Return rdr.ReadBytes(buffer.Length)
        End Using
    End Using
End Function

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.

美羊羊 2024-08-18 12:04:45

VB 不提供直接内存访问的方法。您有两种选择:

  1. 使用 Marshal 类
  2. 编写一个小型的不安全 C#(或 C++/CLI)库,仅处理这些操作并从 VB 代码中引用它。

好吧,还有第三种选择。 VB.Net本身并不支持直接内存访问,但它是可以实现的。它只是丑陋且容易出错。尽管如此,如果您愿意付出努力,您可以尝试使用 这些技术与前面提到的方法相结合。

VB does not offer methods for direct memory access. You have two choices:

  1. Use the Marshal class
  2. Write a small unsafe C# (or C++/CLI) library that handles only these operations and reference it from your VB code.

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.

檐上三寸雪 2024-08-18 12:04:45

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文