Bitmap.LockBits メソッド

定義

Bitmap をシステム メモリにロックします。

オーバーロード

LockBits(Rectangle, ImageLockMode, PixelFormat)

Bitmap をシステム メモリにロックします。

LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)

Bitmap をシステム メモリにロックします。

LockBits(Rectangle, ImageLockMode, PixelFormat)

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

パラメーター

rect
Rectangle

Bitmap のロックする部分を指定する Rectangle 構造体。

flags
ImageLockMode

Bitmap のアクセス レベル (読み取り/書き込み) を指定する ImageLockMode 列挙体。

format
PixelFormat

この Bitmap のデータ形式を指定する PixelFormat 列挙体。

戻り値

このロック処理に関する情報を格納している BitmapData

例外

PixelFormat はピクセルあたりのビット数の特定の値ではありません。

- または -

ビットマップに対して不正な PixelFormat が渡されました。

操作が失敗しました。

次のコード例では、、Height、、Widthおよび Scan0 の各プロパティ、LockBitsおよび UnlockBits メソッド、および 列挙体を使用PixelFormatする方法をImageLockMode示します。 この例は、Windows フォームで使用するように設計されています。 この例は、すべてのピクセル形式で正しく動作するようには設計されていませんが、 メソッドの使用方法の例を LockBits 示します。 この例を実行するには、フォームに貼り付け、 メソッドを呼び出してフォームのPaintイベントをLockUnlockBitsExample処理し、 を としてPaintEventArgseします。

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、 がSystem.ArgumentExceptionスローされます。IndexedGdi また、ビットマップの正しくないピクセル形式を渡すと、 System.ArgumentExceptionがスローされます。

適用対象

LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)

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

パラメーター

rect
Rectangle

Bitmap のロックする部分を指定する四角形構造体。

flags
ImageLockMode

Bitmap のアクセス レベル (読み取り/書き込み) を指定する ImageLockMode 値の 1 つ。

format
PixelFormat

Bitmap のデータ形式を指定する PixelFormat 値の 1 つ。

bitmapData
BitmapData

ロック処理に関する情報を格納している BitmapData

戻り値

ロック処理に関する情報を格納している BitmapData

例外

PixelFormat 値はピクセルあたりのビット数の特定の値ではありません。

- または -

ビットマップに対して不正な PixelFormat が渡されました。

操作が失敗しました。

注釈

メソッドを LockBits 使用して、システム メモリ内の既存のビットマップをロックして、プログラムで変更できるようにします。 メソッドを使用してイメージ SetPixel の色を変更できますが、 LockBits メソッドは大規模な変更に対してパフォーマンスを向上させます。

このメソッドを呼び出すときは、特定の System.Drawing.Imaging.PixelFormat ピクセルあたりのビット数 (BPP) 値を含む列挙体のメンバーを使用する必要があります。 や GdiなどのIndexed値を使用するとSystem.Drawing.Imaging.PixelFormatSystem.ArgumentExceptionがスローされます。 また、ビットマップの正しくないピクセル形式を渡すと、 System.ArgumentExceptionがスローされます。

このバージョンの LockBits メソッドは、 の値ImageLockMode.UserInputBufferと共にflags使用することを目的としています。

適用対象