Прочетете на английски Редактиране

Споделяне чрез


Bitmap.LockBits Method

Definition

Locks a Bitmap into system memory.

Overloads

LockBits(Rectangle, ImageLockMode, PixelFormat)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

Locks a Bitmap into system memory.

C#
public System.Drawing.Imaging.BitmapData LockBits(System.Drawing.Rectangle rect, System.Drawing.Imaging.ImageLockMode flags, System.Drawing.Imaging.PixelFormat format);

Parameters

rect
Rectangle

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

flags
ImageLockMode

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

format
PixelFormat

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

Returns

A BitmapData that contains information about this lock operation.

Exceptions

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

-or-

The incorrect PixelFormat is passed in for a bitmap.

The operation failed.

Examples

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.

C#
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.Width, bmp.Height);
        System.Drawing.Imaging.BitmapData bmpData =
            bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
            bmp.PixelFormat);

        // Get the address of the first line.
        IntPtr ptr = bmpData.Scan0;

        // Declare an array to hold the bytes of the bitmap.
        int bytes  = Math.Abs(bmpData.Stride) * bmp.Height;
        byte[] rgbValues = new byte[bytes];

        // Copy the RGB values into the array.
        System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

        // Set every third value to 255. A 24bpp bitmap will look red.  
        for (int counter = 2; counter < rgbValues.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.Graphics.DrawImage(bmp, 0, 150);
    }

Remarks

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.

Applies to

.NET 10 (package-provided) и други версии
Продукт Версии
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

Locks a Bitmap into system memory.

C#
public System.Drawing.Imaging.BitmapData LockBits(System.Drawing.Rectangle rect, System.Drawing.Imaging.ImageLockMode flags, System.Drawing.Imaging.PixelFormat format, System.Drawing.Imaging.BitmapData bitmapData);

Parameters

rect
Rectangle

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

flags
ImageLockMode

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

format
PixelFormat

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

bitmapData
BitmapData

A BitmapData that contains information about the lock operation.

Returns

A BitmapData that contains information about the lock operation.

Exceptions

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

-or-

The incorrect PixelFormat is passed in for a bitmap.

The operation failed.

Remarks

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.

Applies to

.NET 10 (package-provided) и други версии
Продукт Версии
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10