2010-11-23 84 views
1

我怎麼轉換C#代碼C#字節*到VB.NET的BitmapData掃描線

骨密度的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?

我的選擇是什麼?感謝您的建議。

回答

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) 
+0

我該如何解決我的問題?我得到這個atm昏暗的掃描線(bmd.Width * PIXLESIZE)作爲字節 Runtime.InteropServices.Marshal.Copy(bmd.Scan0,scanline,(y * bmd.Stride),bmd.Width * PIXLESIZE),但它失敗後通過。 – SSpoke 2010-11-24 16:34:17

3

VB.NET不支持指針。只要VB.NET是你的要求,選擇是不愉快的慢,元帥類是你所有。它不應該是這樣的,在您的解決方案中添加C#類庫並在您的VB.NET代碼中使用它的類在Visual Studio中得到很好的支持。

+0

非常有趣,因爲我看到它的方式,.NET框架是一個巨大的系統,它包含了很多實踐,如Delphi.NET/C#/VB ..所以他們應該都具有相同的功能..我想VB.NET嘗試儘可能裸露以表示是BASIC語言。雖然在引擎蓋下所有的語言都做相同的代碼?也許我在這裏錯了..但我安裝了一個用於所有語言的.NET框架= P – SSpoke 2010-11-24 19:00:57

+0

支持編寫不安全的代碼在移植到.NET的語言中並不常見。例如,F#,IronPython,IronRuby,JScript都沒有它。等等。向C#團隊購買一支雪茄。 – 2010-11-24 19:12:29

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