Bitmap.LockBits Methode

Definition

Sperrt eine Bitmap im Systemspeicher.

Überlädt

LockBits(Rectangle, ImageLockMode, PixelFormat)

Sperrt eine Bitmap im Systemspeicher.

LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)

Sperrt eine Bitmap im Systemspeicher.

LockBits(Rectangle, ImageLockMode, PixelFormat)

Sperrt eine Bitmap im Systemspeicher.

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

Parameter

rect
Rectangle

Eine Rectangle-Struktur, die den zu zeichnenden Teil der Bitmap angibt.

flags
ImageLockMode

Eine ImageLockMode-Enumeration, die die Zugriffsebene (Lesen/Schreiben) für die Bitmap angibt.

format
PixelFormat

Eine PixelFormat-Enumeration, die das Datenformat dieser Bitmap angibt.

Gibt zurück

Ein BitmapData mit den Informationen über den Sperrvorgang.

Ausnahmen

PixelFormat ist kein in Bit pro Pixel angegebener Wert.

- oder -

Für eine Bitmap wurde ein falsches PixelFormat übergeben.

Fehler beim Vorgang.

Beispiele

Im folgenden Codebeispiel wird veranschaulicht, wie die PixelFormatEigenschaften , Height, Widthund Scan0 , die LockBits Methoden und UnlockBits und und die ImageLockMode -Enumeration verwendet werden. Dieses Beispiel ist für die Verwendung mit Windows Forms konzipiert. Dieses Beispiel ist nicht so konzipiert, dass es ordnungsgemäß mit allen Pixelformaten funktioniert, sondern als Beispiel für die Verwendung der LockBits -Methode. Um dieses Beispiel auszuführen, fügen Sie es in ein Formular ein, und behandeln Sie das Ereignis des FormularsPaint, indem Sie die LockUnlockBitsExample -Methode aufrufen und als PaintEventArgsübergebene.

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

Hinweise

Verwenden Sie die LockBits -Methode, um eine vorhandene Bitmap im Systemspeicher zu sperren, damit sie programmgesteuert geändert werden kann. Sie können die Farbe eines Bilds mit der SetPixel -Methode ändern, obwohl die LockBits -Methode eine bessere Leistung für umfangreiche Änderungen bietet.

BitmapData Gibt die Attribute von Bitmapan, z. B. Größe, Pixelformat, die Startadresse der Pixeldaten im Arbeitsspeicher und länge jeder Scanzeile (Stride).

Wenn Sie diese Methode aufrufen, sollten Sie einen Member der Enumeration verwenden, der System.Drawing.Imaging.PixelFormat einen bestimmten BPP-Wert (Bits-per-Pixel) enthält. Die Verwendung von System.Drawing.Imaging.PixelFormat Werten wie Indexed und Gdi löst eine aus System.ArgumentException. Außerdem löst das Übergeben des falschen Pixelformats für eine Bitmap einen aus System.ArgumentException.

Gilt für:

LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)

Sperrt eine Bitmap im Systemspeicher.

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

Parameter

rect
Rectangle

Eine Rectangle-Struktur, die den zu sperrenden Bereich der Bitmap angibt.

flags
ImageLockMode

Einer der ImageLockMode-Werte, der die Zugriffsebene (Lesen/Schreiben) für die Bitmap angibt.

format
PixelFormat

Einer der PixelFormat-Werte, der das Datenformat der Bitmap angibt.

bitmapData
BitmapData

Ein BitmapData mit den Informationen über den Sperrvorgang.

Gibt zurück

Ein BitmapData mit den Informationen über den Sperrvorgang.

Ausnahmen

Der PixelFormat-Wert ist kein in Bit pro Pixel angegebener Wert.

- oder -

Für eine Bitmap wurde ein falsches PixelFormat übergeben.

Fehler beim Vorgang.

Hinweise

Verwenden Sie die LockBits -Methode, um eine vorhandene Bitmap im Systemspeicher zu sperren, damit sie programmgesteuert geändert werden kann. Sie können die Farbe eines Bilds mit der SetPixel -Methode ändern, obwohl die LockBits -Methode eine bessere Leistung für umfangreiche Änderungen bietet.

Wenn Sie diese Methode aufrufen, sollten Sie einen Member der Enumeration verwenden, der System.Drawing.Imaging.PixelFormat einen bestimmten BPP-Wert (Bits-per-Pixel) enthält. Wenn Sie System.Drawing.Imaging.PixelFormat Werte wie Indexed und Gdiverwenden, wird eine System.ArgumentExceptionausgelöst. Außerdem löst das Übergeben des falschen Pixelformats für eine Bitmap einen aus System.ArgumentException.

Diese Version der LockBits -Methode ist für die Verwendung mit dem flags Wert vorgesehen ImageLockMode.UserInputBuffer.

Gilt für: