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

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.

ExceptionCondition
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

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft