如何转换C#代码
bmd是BitmapData
byte* scanline = (byte*)bmd.Scan0 + (y * bmd.Stride);
到VB.NET?
在线C#到VB.net转换器给了我这一行
Dim scanline As Pointer(Of Byte) = CType(bmd.Scan0, Pointer(Of Byte)) + (y * bmd.Stride)
但未定义类型'指针'。在 VB.Net?
我有什么选择?感谢您的建议。
答案 0 :(得分:3)
VB.NET不支持指针。只要VB.NET是你的要求,替代品就会非常慢,Marshal类就是你所拥有的。它不应该是,在您的解决方案中添加C#类库并在Visual Studio中使用其VB.NET代码中的类。
答案 1 :(得分:1)
元帅是这里唯一的方式。我之前已经取得了很大的成功,但这很烦人。
http://msdn.microsoft.com/en-us/library/system.drawing.imaging.bitmapdata.scan0.aspx
' Get the address of the first line.
Dim ptr As IntPtr = bmpData.Scan0
' Declare an array to hold the bytes of the bitmap.
' This code is specific to a bitmap with 24 bits per pixels.
Dim bytes As Integer = Math.Abs(bmpData.Stride) * bmp.Height
Dim rgbValues(bytes - 1) As Byte
' Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)
答案 2 :(得分:0)
这是我得到的确切但一次通过后失败了。关于数组超出界限的东西可能与y * bmd.Stride有关(但我不明白为什么会出现越界错误,因为它应该只复制原始内存字节而不使用数组!)
Public Function findImages(ByVal bmd As BitmapData) As List(Of Point)
Dim results As New List(Of Point)()
foundRects = New List(Of Rectangle)()
For y As Integer = 0 To bmd.Height - 1
'oringinal code
'Dim scanline As Pointer(Of Byte) = CType(bmd.Scan0, Pointer(Of Byte)) + (y * bmd.Stride)
'mess is here
' gets address of the first line
'Dim ptr As IntPtr = bmd.Scan0
'Dim bytes As Integer = (y * bmd.Stride)
'If bytes = 0 Then bytes = bmd.Stride
Dim scanline(bmd.Width * PIXLESIZE) As Byte
'Copy the RGB values into the array.
Runtime.InteropServices.Marshal.Copy(bmd.Scan0, scanline, (y * bmd.Stride), bmd.Width * PIXLESIZE)
' --------------------------------
For x As Integer = 0 To bmd.Width - 1
Dim xo As Integer = x * PIXLESIZE
Dim buff As Byte() = {scanline(xo), scanline(xo + 1), scanline(xo + 2), &HFF}
Dim val As Integer = BitConverter.ToInt32(buff, 0)
' Pixle value from subimage in desktop image
If pixels.ContainsKey(val) AndAlso notFound(x, y) Then
Dim loc As Point = DirectCast(pixels(val), Point)
Dim sx As Integer = x - loc.X
Dim sy As Integer = y - loc.Y
' Subimage occurs in desktop image
If imageThere(bmd, sx, sy) Then
Dim p As New Point(x - loc.X, y - loc.Y)
results.Add(p)
foundRects.Add(New Rectangle(x, y, bmImage.Width, bmImage.Height))
End If
End If
Next
Next
Return results
End Function