ImageLockMode Enumeration
.NET Framework 3.0
Specifies flags that are passed to the flags parameter of the LockBits method. The LockBits method locks a portion of an image so that you can read or write the pixel data.
Namespace: System.Drawing.Imaging
Assembly: System.Drawing (in system.drawing.dll)
Assembly: System.Drawing (in system.drawing.dll)
| Member name | Description | |
|---|---|---|
![]() | ReadOnly | Specifies that a portion of the image is locked for reading. |
![]() | ReadWrite | Specifies that a portion of the image is locked for reading or writing. |
| UserInputBuffer | Specifies that the buffer used for reading or writing pixel data is allocated by the user. If this flag is set, the flags parameter of the LockBits method serves as an input parameter (and possibly as an output parameter). If this flag is cleared, then the flags parameter serves only as an output parameter. | |
![]() | WriteOnly | Specifies that a portion of the image is locked for writing. |
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. 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 = bmp.Width * bmp.Height * 3 Dim rgbValues(bytes - 1) As Byte ' Copy the RGB values into the array. System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes) ' Set every red value to 255. For counter As Integer = 2 To rgbValues.Length - 1 Step 3 rgbValues(counter) = 255 Next ' Copy the RGB values back to the bitmap System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes) ' Unlock the bits. bmp.UnlockBits(bmpData) ' Draw the modified image. e.Graphics.DrawImage(bmp, 0, 150) End Sub
private void LockUnlockBitsExample(PaintEventArgs e)
{
// Create a new bitmap.
Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.get_Width(), bmp.get_Height());
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect,
System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.get_PixelFormat());
// Get the address of the first line.
IntPtr ptr = bmpData.get_Scan0();
// Declare an array to hold the bytes of the bitmap.
// This code is specific to a bitmap with 24 bits per pixels.
int bytes = bmp.get_Width() * bmp.get_Height() * 3;
ubyte rgbValues[] = new ubyte[bytes];
// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
// Set every red value to 255.
for (int counter = 2; counter < rgbValues.get_Length(); counter+=3) {
rgbValues[counter] = 255;
}
// Copy the RGB values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
// Unlock the bits.
bmp.UnlockBits(bmpData);
// Draw the modified image.
e.get_Graphics().DrawImage(bmp, 0, 150);
} //LockUnlockBitsExample
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Community Additions
ADD
Show:
