Bitmap.LockBits 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將 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
參數
- flags
- ImageLockMode
ImageLockMode 列舉,指定 Bitmap 的存取層級 (讀取/寫入)。
- format
- PixelFormat
PixelFormat 列舉,指定這個 Bitmap 的資料格式。
傳回
BitmapData,包含這個鎖定作業的相關資訊。
例外狀況
作業失敗。
範例
下列程式碼範例示範如何使用 、、 和 屬性、 LockBits 和 UnlockBits 方法,以及 ImageLockMode 列舉。 Scan0WidthHeightPixelFormat 此範例的設計目的是要與Windows Forms搭配使用。 此範例並非設計來正確處理所有像素格式,而是提供如何使用 方法的 LockBits 範例。 若要執行此範例,請將它貼到表單中,並藉由呼叫 方法來處理表單 Paint 的 事件,並 e
傳遞為 PaintEventArgs 。 LockUnlockBitsExample
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)
將 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
參數
- flags
- ImageLockMode
一個 ImageLockMode 值,指定 Bitmap 的存取層級 (讀取/寫入)。
- format
- PixelFormat
一個 PixelFormat 值,指定 Bitmap 的資料格式。
- bitmapData
- BitmapData
BitmapData,包含鎖定作業的相關資訊。
傳回
BitmapData,包含鎖定作業的相關資訊。
例外狀況
作業失敗。
備註
LockBits使用 方法來鎖定系統記憶體中的現有點陣圖,以便以程式設計方式加以變更。 您可以使用 方法來變更影像 SetPixel 的色彩,不過 LockBits 此方法可為大規模變更提供更佳的效能。
呼叫此方法時,您應該使用列舉的成員 System.Drawing.Imaging.PixelFormat ,其中包含每個圖元的特定位 (BPP) 值。 使用 System.Drawing.Imaging.PixelFormat 和 之類的 IndexedGdi 值會擲回 System.ArgumentException 。 此外,傳遞點陣圖不正確的像素格式將會擲回 System.ArgumentException 。
這個版本的 LockBits 方法旨在與 的值 ImageLockMode.UserInputBuffer 搭配 flags
使用。