Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices

Public Class DirectAccessBitmap
  Public Pixel() As Integer

  Private m_Bitmap As Bitmap
  Private total_size As Integer

  Private m_BitmapData As BitmapData

  Public Sub New(ByVal bm As Bitmap)
    m_Bitmap = bm
    Dim Bounds As Rectangle = New Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height)
    m_BitmapData = m_Bitmap.LockBits(Bounds, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb)
    total_size = m_BitmapData.Width * m_BitmapData.Height
    ReDim Pixel(total_size)
  End Sub

  Public Sub LoadBitmap()
    ' this line copies everything except the last (top) line of the bitmap
    Marshal.Copy(m_BitmapData.Scan0, Pixel, m_BitmapData.Width, total_size - m_BitmapData.Width)
    ' this line copies everything
    ' Marshal.Copy(m_BitmapData.Scan0, Pixel, 0, total_size)
  End Sub

  Public Sub UnloadBitmap()
    Marshal.Copy(Pixel, 0, m_BitmapData.Scan0, total_size)
    m_Bitmap.UnlockBits(m_BitmapData)
    Pixel = Nothing
    m_BitmapData = Nothing
  End Sub

End Class
