Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleManipulate image pixels very quickly using LockBits wrapped in a class in VB .NET
DescriptionThis example shows how to manipulate image pixels very quickly using LockBits wrapped in a class in VB .NET. The BitmapBytesRGB24 class provides routines that prepare a bitmap for pixel manipulation and that copy the results back into the bitmap.
Keywordsimage, pixel, image processing, unmanaged array, LockBits, VB .NET
CategoriesGraphics, VB.NET
 
The class's LockBitmap subroutine calls the bitmap's LockBits method to lock the object's data. This prevents the data from moving while the program is manipulating it. It allocates an array big enough to hold the pixel data and then calls Marshal.Copy to copy the data from the bitmap into this array.

Subroutine UnlockBitmap calls Marshal.Copy to copy the data from the pixel array back into the Bitmap object. It then calls the object's UnlockBits method.

 
Public Class BitmapBytesRGB24
    ' Provide public access to the picture's byte data.
    Public ImageBytes() As Byte
    Public RowSizeBytes As Integer
    Public Const PixelDataSize As Integer = 24

    ' Save a reference to the bitmap.
    Public Sub New(ByVal bm As Bitmap)
        m_Bitmap = bm
    End Sub

    ' A reference to the Bitmap.
    Private m_Bitmap As Bitmap

    ' Bitmap data.
    Private m_BitmapData As BitmapData

    ' Lock the bitmap's data.
    Public Sub LockBitmap()
        ' Lock the bitmap data.
        Dim bounds As Rectangle = New Rectangle( _
            0, 0, m_Bitmap.Width, m_Bitmap.Height)
        m_BitmapData = m_Bitmap.LockBits(bounds, _
            Imaging.ImageLockMode.ReadWrite, _
            Imaging.PixelFormat.Format24bppRgb)
        RowSizeBytes = m_BitmapData.Stride

        ' Allocate room for the data.
        Dim total_size As Integer = m_BitmapData.Stride * _
            m_BitmapData.Height
        ReDim ImageBytes(total_size)

        ' Copy the data into the ImageBytes array.
        Marshal.Copy(m_BitmapData.Scan0, ImageBytes, _
            0, total_size)
    End Sub

    ' Copy the data back into the Bitmap
    ' and release resources.
    Public Sub UnlockBitmap()
        ' Copy the data back into the bitmap.
        Dim total_size As Integer = m_BitmapData.Stride * _
            m_BitmapData.Height
        Marshal.Copy(ImageBytes, 0, _
            m_BitmapData.Scan0, total_size)

        ' Unlock the bitmap.
        m_Bitmap.UnlockBits(m_BitmapData)

        ' Release resources.
        ImageBytes = Nothing
        m_BitmapData = Nothing
    End Sub
End Class
 
The main program makes a BitmapBytesRGB24 object and calls its LockBitmap method. It processes the image's pixels and then calls the object's UnlockBitmap method.
 
' Make a BitmapBytesRGB24 object.
Dim bm_bytes As New BitmapBytesRGB24(bm)

' Lock the bitmap.
bm_bytes.LockBitmap()

pix = 0
For Y = 0 To bm.Height - 1
    For X = 0 To bm.Width - 1
        ' Process the pixel's bytes.
        For k = 0 To 2
            bm_bytes.ImageBytes(pix) = _
                BYTE_255 - bm_bytes.ImageBytes(pix)
            pix += 1
        Next k
    Next X
Next Y

' Unlock the bitmap.
bm_bytes.UnlockBitmap()
 
On my computer, this approach is about 10 times faster than directly modifying the bitmap's pixels. The code demonstrates that approach also so you can compare the two.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated