Bitmap.LockBits Metoda

Definicja

Blokuje pamięć systemową Bitmap .

Przeciążenia

LockBits(Rectangle, ImageLockMode, PixelFormat)

Blokuje pamięć systemową Bitmap .

LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)

Blokuje pamięć systemową Bitmap .

LockBits(Rectangle, ImageLockMode, PixelFormat)

Źródło:
Bitmap.cs
Źródło:
Bitmap.cs
Źródło:
Bitmap.cs

Blokuje pamięć systemową 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

Parametry

rect
Rectangle

Rectangle Struktura określająca część blokadyBitmap.

flags
ImageLockMode

Wyliczenie ImageLockMode określające poziom dostępu (odczyt/zapis) dla elementu Bitmap.

format
PixelFormat

Wyliczenie PixelFormat określające format danych tego Bitmapelementu .

Zwraca

Element BitmapData zawierający informacje o tej operacji blokady.

Wyjątki

Nie PixelFormat jest to określona wartość bitów na piksel.

-lub-

Niepoprawne PixelFormat jest przekazywane dla mapy bitowej.

Operacja nie powiodła się.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać PixelFormatwłaściwości , Height, Widthi Scan0 , LockBitsUnlockBits i oraz ImageLockMode wyliczenia. Ten przykład jest przeznaczony do użycia z Windows Forms. W tym przykładzie nie zaprojektowano poprawnej pracy ze wszystkimi formatami pikseli, ale aby przedstawić przykład użycia LockBits metody . Aby uruchomić ten przykład, wklej go w formularzu i obsłuż zdarzenie formularza Paint , wywołując metodę LockUnlockBitsExample , przekazując e jako PaintEventArgs.

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

Uwagi

Użyj metody , LockBits aby zablokować istniejącą mapę bitową w pamięci systemowej, aby można było ją zmienić programowo. Kolor obrazu można zmienić za pomocą SetPixel metody , chociaż LockBits metoda zapewnia lepszą wydajność w przypadku zmian na dużą skalę.

Określa BitmapData atrybuty Bitmap, takie jak rozmiar, format pikseli, początkowy adres danych pikseli w pamięci i długość każdego wiersza skanowania (krok).

Podczas wywoływania tej metody należy użyć elementu członkowskiego wyliczenia zawierającego System.Drawing.Imaging.PixelFormat określoną wartość bitów na piksel (BPP). Użycie System.Drawing.Imaging.PixelFormat takich wartości jak Indexed i Gdi spowoduje zgłoszenie elementu System.ArgumentException. Ponadto przekazanie niepoprawnego formatu pikseli dla mapy bitowej spowoduje wyrzucenie elementu System.ArgumentException.

Dotyczy

LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)

Źródło:
Bitmap.cs
Źródło:
Bitmap.cs
Źródło:
Bitmap.cs

Blokuje pamięć systemową 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

Parametry

rect
Rectangle

Struktura prostokąta określająca część blokady Bitmap .

flags
ImageLockMode

ImageLockMode Jedna z wartości określających poziom dostępu (odczyt/zapis) dla elementu Bitmap.

format
PixelFormat

PixelFormat Jedna z wartości określających format danych elementu Bitmap.

bitmapData
BitmapData

Element BitmapData zawierający informacje o operacji blokady.

Zwraca

Element BitmapData zawierający informacje o operacji blokady.

Wyjątki

PixelFormat wartość nie jest określoną wartością bitów na piksel.

-lub-

Niepoprawne PixelFormat jest przekazywane dla mapy bitowej.

Operacja nie powiodła się.

Uwagi

Użyj metody , LockBits aby zablokować istniejącą mapę bitową w pamięci systemowej, aby można było ją zmienić programowo. Kolor obrazu można zmienić za pomocą SetPixel metody , chociaż LockBits metoda zapewnia lepszą wydajność w przypadku zmian na dużą skalę.

Podczas wywoływania tej metody należy użyć elementu członkowskiego wyliczenia zawierającego System.Drawing.Imaging.PixelFormat określoną wartość bitów na piksel (BPP). Użycie System.Drawing.Imaging.PixelFormat wartości, takich jak Indexed i Gdi, spowoduje wyrzucenie wartości System.ArgumentException. Ponadto przekazanie niepoprawnego formatu pikseli dla mapy bitowej spowoduje wyrzucenie elementu System.ArgumentException.

Ta wersja LockBits metody ma być używana z wartością flagsImageLockMode.UserInputBuffer.

Dotyczy