C # байт * в VB.NET BitmapData scanline - PullRequest
1 голос
/ 23 ноября 2010

как конвертировать код 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)

но тип 'Pointer' не определен. в VB.Net?

Какие у меня варианты? Спасибо за совет.

Ответы [ 3 ]

3 голосов
/ 23 ноября 2010

Указатели не поддерживаются в VB.NET. Альтернативы являются неприятно медленными, пока VB.NET - ваше требование, класс Marshal - это все, что у вас есть. Этого не должно быть, добавление библиотеки классов C # в ваше решение и использование ее классов в коде VB.NET очень хорошо поддерживается в Visual Studio.

1 голос
/ 23 ноября 2010

Маршал - единственный путь сюда. Я делал это раньше с большим успехом, но это раздражало.

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)
0 голосов
/ 24 ноября 2010

Вот то, что я получил точно, но это не удалось после одного прохода.Что-то в том, что массив выходит за пределы, вероятно, что-то с 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
...