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 multiple images' pixels very quickly using LockBits wrapped in a class in VB .NET
DescriptionThis example shows how to manipulate multiple images' 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
 
This example shows how a program can use the BitmapBytesRGB24 class to manipulate more than one image at a time.

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 four BitmapBytesRGB24 objects, calls subroutine ProcessImageLockBits for each, and then calls each object's UnlockBitmap method.
 
Dim bm1 As New Bitmap(picHidden.Image)
Dim bm_bytes1 As New BitmapBytesRGB24(bm1)
Dim bm2 As New Bitmap(picHidden.Image)
Dim bm_bytes2 As New BitmapBytesRGB24(bm2)
Dim bm3 As New Bitmap(picHidden.Image)
Dim bm_bytes3 As New BitmapBytesRGB24(bm3)
Dim bm4 As New Bitmap(picHidden.Image)
Dim bm_bytes4 As New BitmapBytesRGB24(bm4)

ProcessImageLockBits(bm_bytes1, bm1.Width, bm1.Height)
ProcessImageLockBits(bm_bytes2, bm2.Width, bm2.Height)
ProcessImageLockBits(bm_bytes3, bm3.Width, bm3.Height)
ProcessImageLockBits(bm_bytes4, bm4.Width, bm4.Height)

bm_bytes1.UnlockBitmap()
picVisible1.Image = bm1
bm_bytes2.UnlockBitmap()
picVisible2.Image = bm2
bm_bytes3.UnlockBitmap()
picVisible3.Image = bm3
bm_bytes4.UnlockBitmap()
picVisible4.Image = bm4
 
Subroutine ProcessImageLockBits calls the BitmapBytesRGB24 object's LockBitmap method and modifies the image's pixel data.
 
Private Sub ProcessImageLockBits(ByVal bm_bytes As _
    BitmapBytesRGB24, ByVal wid As Integer, ByVal hgt As _
    Integer)
    Const BYTE_255 As Byte = 255
    Dim X, Y, k As Integer
    Dim pix As Integer

    ' Lock the bitmap.
    bm_bytes.LockBitmap()

    pix = 0
    For Y = 0 To hgt - 1
        For X = 0 To wid - 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
End Sub
 
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