代码之家  ›  专栏  ›  技术社区  ›  SSpoke

C byte*到vb.net bitmapdata scanline

  •  1
  • SSpoke  · 技术社区  · 14 年前

    如何转换C代码

    BMD是位图数据

     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 回复  |  直到 14 年前
        1
  •  1
  •   Tom    14 年前

    元帅是这里唯一的路。我以前做过很多成功的事,但这是在涂油。

    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
  •  3
  •   Hans Passant    14 年前

    VB.NET中不支持指针。只要vb.net是您的需求,marshal类就是您所能得到的,那么其他的选择就非常缓慢。不必如此,在您的解决方案中添加一个C类库,并在您的vb.net代码中使用它的类在Visual Studio中得到了很好的支持。

        3
  •  0
  •   SSpoke    14 年前

    这是我的确切信息,但一次通过就失败了。关于数组越界的一些信息,可能是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