Bitmap.LockBits Method (Rectangle, ImageLockMode, PixelFormat)


Locks a Bitmap into system memory.

Namespace:   System.Drawing
Assembly:  System.Drawing (in System.Drawing.dll)

<SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags := SecurityPermissionFlag.UnmanagedCode)>
Public Function LockBits (
	rect As Rectangle,
	flags As ImageLockMode,
	format As PixelFormat
) As BitmapData


Type: System.Drawing.Rectangle

A Rectangle structure that specifies the portion of the Bitmap to lock.

Type: System.Drawing.Imaging.ImageLockMode

An ImageLockMode enumeration that specifies the access level (read/write) for the Bitmap.

Type: System.Drawing.Imaging.PixelFormat

A PixelFormat enumeration that specifies the data format of this Bitmap.

Return Value

Type: System.Drawing.Imaging.BitmapData

A BitmapData that contains information about this lock operation.

Exception Condition

The PixelFormat is not a specific bits-per-pixel value.


The incorrect PixelFormat is passed in for a bitmap.


The operation failed.

Use the LockBits method to lock an existing bitmap in system memory so that it can be changed programmatically. You can change the color of an image with the SetPixel method, although the LockBits method offers better performance for large-scale changes.

The BitmapData specifies the attributes of the Bitmap, such as size, pixel format, the starting address of the pixel data in memory, and length of each scan line (stride).

When calling this method, you should use a member of the System.Drawing.Imaging.PixelFormat enumeration that contains a specific bits-per-pixel (BPP) value. Using System.Drawing.Imaging.PixelFormat values such as Indexed and Gdi will throw an System.ArgumentException. Also, passing the incorrect pixel format for a bitmap will throw an System.ArgumentException.

The following code example demonstrates how to use the PixelFormat, Height, Width, and Scan0 properties; the LockBits and UnlockBits methods; and the ImageLockMode enumeration. This example is designed to be used with Windows Forms. This example is not designed to work correctly with all pixel formats, but to provide an example of how to use the LockBits method. To run this example, paste it into a form and handle the form's Paint event by calling the LockUnlockBitsExample method, passing e as PaintEventArgs.

Private Sub LockUnlockBitsExample(ByVal e As PaintEventArgs)

    ' Create a new bitmap.
    Dim bmp As New Bitmap("c:\fakePhoto.jpg")

    ' Lock the bitmap's bits.  
    Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
    Dim bmpData As System.Drawing.Imaging.BitmapData = bmp.LockBits(rect, _
        Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat)

    ' 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)

    ' Set every third value to 255. A 24bpp image will look red.
    For counter As Integer = 2 To rgbValues.Length - 1 Step 3
        rgbValues(counter) = 255

    ' Copy the RGB values back to the bitmap
    System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes)

    ' Unlock the bits.

    ' Draw the modified image.
    e.Graphics.DrawImage(bmp, 0, 150)

End Sub

.NET Framework
Available since 1.1
Return to top