Insecure in VB.Net

I need to pass a C # function to VB.NET, but in C # I have something like this:

unsafe { byte* pSmall = (byte*)(void*)smallData.Scan0; byte* pBig = (byte*)(void*)bigData.Scan0; int smallOffset = smallStride - smallBmp.Width * 3; int bigOffset = bigStride - bigBmp.Width * 3; bool matchFound = true; .... } 

I read on some blogs that “unsafe” does not exist in VB.Net. The question is: what can I use instead of unsafe ?

+4
source share
2 answers

The blogs you read are true that there is no way to use unsafe code in VB.NET. It seems you want to manipulate (perhaps read pixel data?) A raster file. In C #, you can use unsafe code to increase performance over the GetPixel method. In VB.NET you can try instead of LockBits .

Here is an example of this page on how to use it.

 Public g_RowSizeBytes As Integer Public g_PixBytes() As Byte Private m_BitmapData As BitmapData ' Lock the bitmap data. Public Sub LockBitmap(ByVal bm As Bitmap) ' Lock the bitmap data. Dim bounds As Rectangle = New Rectangle( _ 0, 0, bm.Width, bm.Height) m_BitmapData = bm.LockBits(bounds, _ Imaging.ImageLockMode.ReadWrite, _ Imaging.PixelFormat.Format24bppRgb) g_RowSizeBytes = m_BitmapData.Stride ' Allocate room for the data. Dim total_size As Integer = m_BitmapData.Stride * _ m_BitmapData.Height ReDim g_PixBytes(total_size) ' Copy the data into the g_PixBytes array. Marshal.Copy(m_BitmapData.Scan0, g_PixBytes, _ 0, total_size) End Sub 

The page also shows how to unlock a bitmap.

+4
source

This is what I think may be a working version:

 Dim pSmall As Pointer(Of Byte) = CType(CType(smallData.Scan0, Pointer(Of System.Void)), Pointer(Of Byte)) Dim pBig As Pointer(Of Byte) = CType(CType(bigData.Scan0, Pointer(Of System.Void)), Pointer(Of Byte)) Dim smallOffset As Integer = smallStride - smallBmp.Width * 3 Dim bigOffset As Integer = bigStride - bigBmp.Width * 3 Dim matchFound As Boolean = True 
0
source

All Articles