Bitmap.LockBits 方法

定義

Bitmap 鎖定在系統記憶體內。

多載

LockBits(Rectangle, ImageLockMode, PixelFormat)

來源:
Bitmap.cs
來源:
Bitmap.cs
來源:
Bitmap.cs

Bitmap 鎖定在系統記憶體內。

public System.Drawing.Imaging.BitmapData LockBits (System.Drawing.Rectangle rect, System.Drawing.Imaging.ImageLockMode flags, System.Drawing.Imaging.PixelFormat format);

參數

rect
Rectangle

Rectangle 結構,指定要鎖定的 Bitmap 部分。

flags
ImageLockMode

ImageLockMode 列舉,指定 Bitmap 的存取層級 (讀取/寫入)。

format
PixelFormat

PixelFormat 列舉,指定這個 Bitmap 的資料格式。

傳回

BitmapData,包含這個鎖定作業的相關資訊。

例外狀況

PixelFormat 並不是代表每一個像素含有幾個位元的有效值。

-或-

傳入點陣圖的 PixelFormat 不正確。

作業失敗。

範例

下列程式代碼範例示範如何使用、、 和屬性、LockBitsUnlockBits 方法,以及 ImageLockMode 列舉。Scan0WidthHeightPixelFormat 此範例的設計目的是要與 Windows Forms 搭配使用。 此範例並非設計來正確處理所有像素格式,而是提供如何使用 方法的 LockBits 範例。 若要執行此範例,請將它貼到表單中,並藉由呼叫 方法來處理表單Paint的事件,並e傳遞為 PaintEventArgsLockUnlockBitsExample

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);
    }

備註

LockBits使用 方法來鎖定系統記憶體中的現有點陣圖,以便以程序設計方式加以變更。 您可以使用 方法來變更影像 SetPixel 的色彩,不過 LockBits 此方法可為大規模變更提供更佳的效能。

BitmapData指定的屬性Bitmap,例如大小、圖元格式、記憶體中像素數據的起始位址,以及每個掃描線的長度 () 。

呼叫此方法時,您應該使用列舉的成員 System.Drawing.Imaging.PixelFormat ,其中包含每個圖元的特定位 (BPP) 值。 使用 System.Drawing.Imaging.PixelFormat 與之類的IndexedGdi值會擲回 System.ArgumentException。 此外,傳遞點陣圖不正確的圖格式會擲回 System.ArgumentException

適用於

.NET Framework 4.8.1 及其他版本
產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)

來源:
Bitmap.cs
來源:
Bitmap.cs
來源:
Bitmap.cs

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);

參數

rect
Rectangle

矩形結構,指定要鎖定的 Bitmap 部分。

flags
ImageLockMode

一個 ImageLockMode 值,指定 Bitmap 的存取層級 (讀取/寫入)。

format
PixelFormat

一個 PixelFormat 值,指定 Bitmap 的資料格式。

bitmapData
BitmapData

BitmapData,包含鎖定作業的相關資訊。

傳回

BitmapData,包含鎖定作業的相關資訊。

例外狀況

PixelFormat 並不是代表每一個像素含有幾個位元的有效值。

-或-

傳入點陣圖的 PixelFormat 不正確。

作業失敗。

備註

LockBits使用 方法來鎖定系統記憶體中的現有點陣圖,以便以程序設計方式加以變更。 您可以使用 方法來變更影像 SetPixel 的色彩,不過 LockBits 此方法可為大規模變更提供更佳的效能。

呼叫此方法時,您應該使用列舉的成員 System.Drawing.Imaging.PixelFormat ,其中包含每個圖元的特定位 (BPP) 值。 使用 System.Drawing.Imaging.PixelFormat 與之類的IndexedGdi值會擲回 System.ArgumentException。 此外,傳遞點陣圖不正確的圖格式會擲回 System.ArgumentException

這個版本的 LockBits 方法旨在與的值ImageLockMode.UserInputBuffer搭配flags使用。

適用於

.NET Framework 4.8.1 及其他版本
產品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9