Bitmap.LockBits Method (Rectangle, ImageLockMode, PixelFormat, BitmapData)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Locks a Bitmap into system memory

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

[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public BitmapData LockBits(
	Rectangle rect,
	ImageLockMode flags,
	PixelFormat format,
	BitmapData bitmapData
)

Parameters

rect
Type: System.Drawing.Rectangle

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

flags
Type: System.Drawing.Imaging.ImageLockMode

One of the ImageLockMode values that specifies the access level (read/write) for the Bitmap.

format
Type: System.Drawing.Imaging.PixelFormat

One of the PixelFormat values that specifies the data format of the Bitmap.

bitmapData
Type: System.Drawing.Imaging.BitmapData

A BitmapData that contains information about the lock operation.

Return Value

Type: System.Drawing.Imaging.BitmapData

A BitmapData that contains information about the lock operation.

Exception Condition
ArgumentException

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

-or-

The incorrect PixelFormat is passed in for a bitmap.

Exception

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.

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.

This version of the LockBits method is intended to be used with a flags value of ImageLockMode.UserInputBuffer.

.NET Framework
Available since 2.0
Return to top
Show: