Bitmap.LockBits Methode

Definition

Sperrt einen Bitmap In-Systemspeicher.

Überlädt

Name Beschreibung
LockBits(Rectangle, ImageLockMode, PixelFormat)

Sperrt einen Bitmap In-Systemspeicher.

LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)

Sperrt einen Bitmap In-Systemspeicher.

LockBits(Rectangle, ImageLockMode, PixelFormat)

Sperrt einen Bitmap In-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 Teil der Bitmap zu sperrenden Sperre angibt.

flags
ImageLockMode

Eine ImageLockMode Aufzählung, die die Zugriffsebene (Lese-/Schreibzugriff) für die Bitmap.

format
PixelFormat

Eine PixelFormat Aufzählung, die das Datenformat dieses BitmapTyps angibt.

Gibt zurück

A BitmapData that contains information about this lock operation.

Ausnahmen

Dies PixelFormat ist kein bestimmter Bit-pro-Pixel-Wert.

-oder-

Das falsche PixelFormat Wird für eine Bitmap übergeben.

Dieser Vorgang ist fehlgeschlagen.

Beispiele

Im folgenden Codebeispiel wird veranschaulicht, wie die PixelFormatEigenschaften , Height, WidthScan0 die Methoden und UnlockBits die LockBitsImageLockMode Enumeration verwendet werden. Dieses Beispiel wurde für die Verwendung mit Windows Forms entwickelt. Dieses Beispiel ist nicht so konzipiert, dass es mit allen Pixelformaten ordnungsgemäß funktioniert, sondern um ein Beispiel für die Verwendung der LockBits Methode bereitzustellen. Wenn Sie dieses Beispiel ausführen möchten, fügen Sie es in ein Formular ein, und behandeln Sie das Ereignis des Formulars Paint , indem Sie die LockUnlockBitsExample Methode aufrufen und als übergeben ePaintEventArgs.

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 große Änderungen bietet.

Die BitmapData Specifies the attributes of the Bitmap, such as size, pixel format, the starting address of the pixel data in memory, and length of each scan line (stride).

Beim Aufrufen dieser Methode sollten Sie ein Element der System.Drawing.Imaging.PixelFormat Enumeration verwenden, das einen bestimmten Bit-pro-Pixel-Wert (BPP) enthält. Die Verwendung von Werten wie Indexed z. B. System.Drawing.Imaging.PixelFormat und Gdi löst ein System.ArgumentException. Außerdem löst das Übergeben des falschen Pixelformats für eine Bitmap ein System.ArgumentException.

Gilt für:

LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)

Sperrt einen Bitmap In-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 Rechteckstruktur, die den Teil der Bitmap zu sperrenden Sperre angibt.

flags
ImageLockMode

Einer der ImageLockMode Werte, der die Zugriffsebene (Lese-/Schreibzugriff) für die Bitmap.

format
PixelFormat

Einer der PixelFormat Werte, die das Datenformat der Bitmap.

bitmapData
BitmapData

A BitmapData that contains information about the lock operation.

Gibt zurück

A BitmapData that contains information about the lock operation.

Ausnahmen

PixelFormat wert ist kein bestimmter Bit-pro-Pixel-Wert.

-oder-

Das falsche PixelFormat Wird für eine Bitmap übergeben.

Dieser Vorgang ist fehlgeschlagen.

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 große Änderungen bietet.

Beim Aufrufen dieser Methode sollten Sie ein Element der System.Drawing.Imaging.PixelFormat Enumeration verwenden, das einen bestimmten Bit-pro-Pixel-Wert (BPP) enthält. Die Verwendung von Werten, zIndexed. B. System.Drawing.Imaging.PixelFormat und Gdi, löst eine System.ArgumentException. Außerdem löst das Übergeben des falschen Pixelformats für eine Bitmap ein System.ArgumentException.

Diese Version der LockBits Methode soll mit einem flags Wert von ImageLockMode.UserInputBufferverwendet werden.

Gilt für: