Bitmap.LockBitsとは? わかりやすく解説

Bitmap.LockBits メソッド (Rectangle, ImageLockMode, PixelFormat, BitmapData)

メモ : このメソッドは、.NET Framework version 2.0 で新しく追加されたものです。

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

名前空間: System.Drawing
アセンブリ: System.Drawing (system.drawing.dll 内)
構文構文

Public Function LockBits ( _
    rect As Rectangle, _
    flags As ImageLockMode, _
    format As PixelFormat, _
    bitmapData As BitmapData _
) As BitmapData
Dim instance As Bitmap
Dim rect As Rectangle
Dim flags As ImageLockMode
Dim format As PixelFormat
Dim bitmapData As BitmapData
Dim returnValue As BitmapData

returnValue = instance.LockBits(rect, flags, format, bitmapData)
public BitmapData LockBits (
    Rectangle rect,
    ImageLockMode flags,
    PixelFormat format,
    BitmapData bitmapData
)
public:
BitmapData^ LockBits (
    Rectangle rect, 
    ImageLockMode flags, 
    PixelFormat format, 
    BitmapData^ bitmapData
)
public BitmapData LockBits (
    Rectangle rect, 
    ImageLockMode flags, 
    PixelFormat format, 
    BitmapData bitmapData
)
public function LockBits (
    rect : Rectangle, 
    flags : ImageLockMode, 
    format : PixelFormat, 
    bitmapData : BitmapData
) : BitmapData

パラメータ

rect

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

flags

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

format

Bitmap のデータ形式を示す PixelFormat 値の 1 つ。

bitmapData

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

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

例外例外
例外の種類条件

ArgumentException

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

または

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

Exception

操作に失敗しました。

解説解説

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

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Bitmap.LockBits メソッド (Rectangle, ImageLockMode, PixelFormat)

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

名前空間: System.Drawing
アセンブリ: System.Drawing (system.drawing.dll 内)
構文構文

Public Function LockBits ( _
    rect As Rectangle, _
    flags As ImageLockMode, _
    format As PixelFormat _
) As BitmapData
Dim instance As Bitmap
Dim rect As Rectangle
Dim flags As ImageLockMode
Dim format As PixelFormat
Dim returnValue As BitmapData

returnValue = instance.LockBits(rect, flags, format)
public BitmapData LockBits (
    Rectangle rect,
    ImageLockMode flags,
    PixelFormat format
)
public:
BitmapData^ LockBits (
    Rectangle rect, 
    ImageLockMode flags, 
    PixelFormat format
)
public BitmapData LockBits (
    Rectangle rect, 
    ImageLockMode flags, 
    PixelFormat format
)
public function LockBits (
    rect : Rectangle, 
    flags : ImageLockMode, 
    format : PixelFormat
) : BitmapData

パラメータ

rect

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

flags

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

format

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

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

例外例外
例外の種類条件

ArgumentException

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

または

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

Exception

操作に失敗しました。

解説解説

BitmapData では、サイズ、ピクセル形式、メモリ内におけるピクセル データの開始アドレス、各スキャン ライン (ストライド) の長さなど、Bitmap の属性を指定します。

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

使用例使用例

PixelFormat、Height、Width、Scan0 の各プロパティ、LockBits メソッドと UnlockBits メソッド、および ImageLockMode 列挙体の使用方法を次のコード例に示します。この例は、Windows フォームでの使用を意図してデザインされています。この例を実行するには、コードをフォームに貼り付け、PaintEventArgs の e を渡して LockUnlockBitsExample メソッドを呼び出すことで、フォームの Paint イベントを処理します。

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
 = bmp.Width * bmp.Height * 3
    Dim rgbValues(bytes - 1) As Byte

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

    ' Set every red value to 255.  
    For counter As Integer
 = 0 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
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.
    // This code is specific to a bitmap with 24 bits per pixels.
    int bytes = bmp.Width * bmp.Height * 3;
    byte[] rgbValues = new byte[bytes];

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

    // Set every red value to 255.  
    for (int counter = 0; 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);

}
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 = bmp->Width * bmp->Height * 3;
   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 red value to 255.  
   for ( int counter = 0; 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.get_Width(), bmp.get_Height());
    System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, 
        System.Drawing.Imaging.ImageLockMode.ReadWrite, 
        bmp.get_PixelFormat());

    // Get the address of the first line.
    IntPtr ptr = bmpData.get_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 = bmp.get_Width() * bmp.get_Height() * 3;
    ubyte rgbValues[] = new ubyte[bytes];

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

    // Set every red value to 255.  
    for (int counter = 0; counter < rgbValues.get_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.get_Graphics().DrawImage(bmp, 0, 150);
} //LockUnlockBitsExample
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Bitmap.LockBits メソッド

Bitmap をシステム メモリにロックします。
オーバーロードの一覧オーバーロードの一覧

名前 説明
Bitmap.LockBits (Rectangle, ImageLockMode, PixelFormat) Bitmap をシステム メモリにロックします。

.NET Compact Framework によってサポートされています。

Bitmap.LockBits (Rectangle, ImageLockMode, PixelFormat, BitmapData) Bitmap をシステム メモリにロックします。
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「Bitmap.LockBits」の関連用語

Bitmap.LockBitsのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Bitmap.LockBitsのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS