BitmapData 類別

定義

指定點陣圖影像的屬性 (Attribute)。Specifies the attributes of a bitmap image. BitmapData 類別是由 LockBits 類別的 UnlockBits(BitmapData)Bitmap 方法所使用。The BitmapData class is used by the LockBits and UnlockBits(BitmapData) methods of the Bitmap class. 無法被繼承。Not inheritable.

public ref class BitmapData sealed
public sealed class BitmapData
type BitmapData = class
Public NotInheritable Class BitmapData
繼承
BitmapData

範例

下列程式碼範例示範如何搭配使用BitmapData類別LockBits與和UnlockBits方法。The following code example demonstrates how to use the BitmapData class with the LockBits and UnlockBits methods. 這個範例是設計來與 Windows Forms 搭配使用。This example is designed to be used with Windows Forms. 若要執行此範例, 請將其貼入表單中, 並Paint藉由LockUnlockBitsExample呼叫方法 (傳遞ePaintEventArgs) 來處理表單的事件。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.

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

建構函式

BitmapData()

初始化 BitmapData 類別的新執行個體。Initializes a new instance of the BitmapData class.

屬性

Height

取得或設定 Bitmap 物件的像素高度。Gets or sets the pixel height of the Bitmap object. 有時也稱為掃描線 (Scan Line) 數目。Also sometimes referred to as the number of scan lines.

PixelFormat

取得或設定傳回這個 Bitmap 物件的 BitmapData 物件中像素資訊格式。Gets or sets the format of the pixel information in the Bitmap object that returned this BitmapData object.

Reserved

保留的。Reserved. 請勿使用。Do not use.

Scan0

取得或設定點陣圖中第一個像素資料的位址。Gets or sets the address of the first pixel data in the bitmap. 這也可以視為點陣圖中第一條掃描線。This can also be thought of as the first scan line in the bitmap.

Stride

取得或設定 Bitmap 物件的分散寬度 (也稱為掃描寬度)。Gets or sets the stride width (also called scan width) of the Bitmap object.

Width

取得或設定 Bitmap 物件的像素寬度。Gets or sets the pixel width of the Bitmap object. 這也可以視為一條掃描線中的像素數目。This can also be thought of as the number of pixels in one scan line.

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於