Bitmap.LockBits 方法

定義

Bitmap 鎖定在系統記憶體內。Locks a Bitmap into system memory.

多載

LockBits(Rectangle, ImageLockMode, PixelFormat)

Bitmap 鎖定在系統記憶體內。Locks a Bitmap into system memory.

LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)

Bitmap 鎖定在系統記憶體內。Locks a Bitmap into system memory.

LockBits(Rectangle, ImageLockMode, PixelFormat)

Bitmap 鎖定在系統記憶體內。Locks a Bitmap into system memory.

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.BitmapData
Public Function LockBits (rect As Rectangle, flags As ImageLockMode, format As PixelFormat) As BitmapData

參數

rect
Rectangle

Rectangle 結構,指定要鎖定的 Bitmap 部分。A Rectangle structure that specifies the portion of the Bitmap to lock.

flags
ImageLockMode

ImageLockMode 列舉,指定 Bitmap 的存取層級 (讀取/寫入)。An ImageLockMode enumeration that specifies the access level (read/write) for the Bitmap.

format
PixelFormat

PixelFormat 列舉,指定這個 Bitmap 的資料格式。A PixelFormat enumeration that specifies the data format of this Bitmap.

傳回

BitmapData,包含這個鎖定作業的相關資訊。A BitmapData that contains information about this lock operation.

例外狀況

PixelFormat 並不是代表每一個像素含有幾個位元的有效值。The PixelFormat is not a specific bits-per-pixel value.

-或--or- 傳入點陣圖的 PixelFormat 不正確。The incorrect PixelFormat is passed in for a bitmap.

作業失敗。The operation failed.

範例

下列程式碼範例示範如何使用 PixelFormatHeightWidthScan0 屬性;LockBits 和 @no__t 5 方法;和 ImageLockMode 列舉。The following code example demonstrates how to use the PixelFormat, Height, Width, and Scan0 properties; the LockBits and UnlockBits methods; and the ImageLockMode enumeration. 這個範例是設計來與 Windows Forms 搭配使用。This example is designed to be used with Windows Forms. 這個範例的設計目的並不是要搭配所有像素格式正常運作,而是提供如何使用 LockBits 方法的範例。This example is not designed to work correctly with all pixel formats, but to provide an example of how to use the LockBits method. 若要執行此範例,請將它貼到表單中,並藉由呼叫 LockUnlockBitsExample 方法來處理表單的 Paint 事件,並將 e 傳遞為 PaintEventArgsTo run this example, paste it into a form and handle the form's Paint event by calling the LockUnlockBitsExample method, passing e as 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 方法來鎖定系統記憶體中的現有點陣圖,以便以程式設計方式進行變更。Use the LockBits method to lock an existing bitmap in system memory so that it can be changed programmatically. 雖然 @no__t 1 方法為大規模變更提供較佳的效能,但您可以使用 SetPixel 方法來變更影像的色彩。You can change the color of an image with the SetPixel method, although the LockBits method offers better performance for large-scale changes.

@No__t-0 會指定 Bitmap 的屬性,例如大小、像素格式、記憶體中圖元資料的起始位址,以及每個掃描線(stride)的長度。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).

呼叫這個方法時,您應該使用 System.Drawing.Imaging.PixelFormat 列舉的成員,其中包含特定的每一像素位元數(BPP)值。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. 使用 @no__t 0 值(例如 IndexedGdi)將會擲回 System.ArgumentExceptionUsing System.Drawing.Imaging.PixelFormat values such as Indexed and Gdi will throw an System.ArgumentException. 此外,為點陣圖傳遞不正確的像素格式將會擲回 System.ArgumentExceptionAlso, passing the incorrect pixel format for a bitmap will throw an System.ArgumentException.

LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)

Bitmap 鎖定在系統記憶體內。Locks a Bitmap into system memory.

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.BitmapData

參數

rect
Rectangle

矩形結構,指定要鎖定的 Bitmap 部分。A rectangle structure that specifies the portion of the Bitmap to lock.

flags
ImageLockMode

一個 ImageLockMode 值,指定 Bitmap 的存取層級 (讀取/寫入)。One of the ImageLockMode values that specifies the access level (read/write) for the Bitmap.

format
PixelFormat

一個 PixelFormat 值,指定 Bitmap 的資料格式。One of the PixelFormat values that specifies the data format of the Bitmap.

bitmapData
BitmapData

BitmapData,包含鎖定作業的相關資訊。A BitmapData that contains information about the lock operation.

傳回

BitmapData,包含鎖定作業的相關資訊。A BitmapData that contains information about the lock operation.

例外狀況

PixelFormat 並不是代表每一個像素含有幾個位元的有效值。PixelFormat value is not a specific bits-per-pixel value.

-或--or- 傳入點陣圖的 PixelFormat 不正確。The incorrect PixelFormat is passed in for a bitmap.

作業失敗。The operation failed.

備註

您可以使用 LockBits 方法來鎖定系統記憶體中的現有點陣圖,以便以程式設計方式進行變更。Use the LockBits method to lock an existing bitmap in system memory so that it can be changed programmatically. 雖然 @no__t 1 方法為大規模變更提供較佳的效能,但您可以使用 SetPixel 方法來變更影像的色彩。You can change the color of an image with the SetPixel method, although the LockBits method offers better performance for large-scale changes.

呼叫這個方法時,您應該使用 System.Drawing.Imaging.PixelFormat 列舉的成員,其中包含特定的每一像素位元數(BPP)值。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. 使用 @no__t 0 值(例如 IndexedGdi)將會擲回 System.ArgumentExceptionUsing System.Drawing.Imaging.PixelFormat values, such as Indexed and Gdi, will throw an System.ArgumentException. 此外,為點陣圖傳遞不正確的像素格式將會擲回 System.ArgumentExceptionAlso, passing the incorrect pixel format for a bitmap will throw an System.ArgumentException.

這一版的 LockBits 方法適用于 ImageLockMode.UserInputBufferflags 值。This version of the LockBits method is intended to be used with a flags value of ImageLockMode.UserInputBuffer.

適用於