Bitmap.LockBits 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将 Bitmap 锁定到系统内存中。
重载
| LockBits(Rectangle, ImageLockMode, PixelFormat) | 将 Bitmap 锁定到系统内存中。 | 
| LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData) | 将 Bitmap 锁定到系统内存中。 | 
LockBits(Rectangle, ImageLockMode, PixelFormat)
- Source:
- Bitmap.cs
- Source:
- Bitmap.cs
- Source:
- Bitmap.cs
将 Bitmap 锁定到系统内存中。
public:
 System::Drawing::Imaging::BitmapData ^ LockBits(System::Drawing::Rectangle rect, System::Drawing::Imaging::ImageLockMode flags, System::Drawing::Imaging::PixelFormat format);public System.Drawing.Imaging.BitmapData LockBits (System.Drawing.Rectangle rect, System.Drawing.Imaging.ImageLockMode flags, System.Drawing.Imaging.PixelFormat format);member this.LockBits : System.Drawing.Rectangle * System.Drawing.Imaging.ImageLockMode * System.Drawing.Imaging.PixelFormat -> System.Drawing.Imaging.BitmapDataPublic Function LockBits (rect As Rectangle, flags As ImageLockMode, format As PixelFormat) As BitmapData参数
- flags
- ImageLockMode
一个 ImageLockMode 枚举,指定 Bitmap 的访问级别(读/写)。
- format
- PixelFormat
一个 PixelFormat 枚举,指定此 Bitmap 的数据格式。
返回
包含有关此锁定操作信息的 BitmapData。
例外
此操作失败。
示例
下面的代码示例演示如何使用 、、 和 属性、LockBits和 UnlockBits 方法以及 ImageLockMode 枚举。Scan0WidthHeightPixelFormat 此示例旨在与 Windows 窗体 一起使用。 此示例并非旨在正确处理所有像素格式,而是提供如何使用 LockBits 方法的示例。 若要运行此示例,请将其粘贴到窗体中,并通过调用 LockUnlockBitsExample 方法处理窗体的 Paint 事件,并e作为 PaintEventArgs传递。
void LockUnlockBitsExample( PaintEventArgs^ e )
{
   // Create a new bitmap.
   Bitmap^ bmp = gcnew Bitmap( "c:\\fakePhoto.jpg" );
   // Lock the bitmap's bits.  
   Rectangle rect = 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.
   // This code is specific to a bitmap with 24 bits per pixels.
   int bytes = Math::Abs(bmpData->Stride) * bmp->Height;
   array<Byte>^rgbValues = gcnew array<Byte>(bytes);
   // Copy the RGB values into the array.
   System::Runtime::InteropServices::Marshal::Copy( ptr, rgbValues, 0, bytes );
   // Set every third value to 255.  
   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 );
}
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);
    }
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
    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
注解
LockBits使用 方法锁定系统内存中的现有位图,以便以编程方式对其进行更改。 可以使用 方法更改图像 SetPixel 的颜色,尽管 LockBits 该方法为大规模更改提供更好的性能。
BitmapData指定 Bitmap的属性,例如大小、像素格式、内存中像素数据的起始地址以及每个扫描行的长度 (步幅) 。
调用此方法时,应使用枚举的成员 System.Drawing.Imaging.PixelFormat ,该成员包含特定的每像素位 (BPP) 值。 使用 System.Drawing.Imaging.PixelFormat 和 IndexedGdi 等值将引发 System.ArgumentException。 此外,为位图传递不正确的像素格式将引发 System.ArgumentException。
适用于
LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)
- Source:
- Bitmap.cs
- Source:
- Bitmap.cs
- Source:
- Bitmap.cs
将 Bitmap 锁定到系统内存中。
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);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);member this.LockBits : System.Drawing.Rectangle * System.Drawing.Imaging.ImageLockMode * System.Drawing.Imaging.PixelFormat * System.Drawing.Imaging.BitmapData -> System.Drawing.Imaging.BitmapDataPublic Function LockBits (rect As Rectangle, flags As ImageLockMode, format As PixelFormat, bitmapData As BitmapData) As BitmapData参数
- flags
- ImageLockMode
某个 ImageLockMode 值,指定 Bitmap 的访问级别(读/写)。
- format
- PixelFormat
某个 PixelFormat 值,指定 Bitmap 的数据格式。
- bitmapData
- BitmapData
包含锁定操作信息的 BitmapData。
返回
包含锁定操作信息的 BitmapData。
例外
此操作失败。
注解
LockBits使用 方法锁定系统内存中的现有位图,以便以编程方式对其进行更改。 可以使用 方法更改图像 SetPixel 的颜色,尽管 LockBits 该方法为大规模更改提供更好的性能。
调用此方法时,应使用枚举的成员 System.Drawing.Imaging.PixelFormat ,该成员包含特定的每像素位 (BPP) 值。 使用 System.Drawing.Imaging.PixelFormat 值(如 Indexed 和 Gdi)将引发 System.ArgumentException。 此外,为位图传递不正确的像素格式将引发 System.ArgumentException。
此版本的 LockBits 方法旨在与 值ImageLockMode.UserInputBuffer一flags起使用。