Bitmap.LockBits Metoda

Definicja

Blokuje pamięć systemową Bitmap .

Przeciążenia

LockBits(Rectangle, ImageLockMode, PixelFormat)

Źródło:
Bitmap.cs
Źródło:
Bitmap.cs
Źródło:
Bitmap.cs
Źródło:
Bitmap.cs
Źródło:
Bitmap.cs
Ź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

Struktura Rectangle określająca część do Bitmap zablokowania.

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 blokowania.

Wyjątki

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

— lub —

PixelFormat Niepoprawny element jest przekazywany dla mapy bitowej.

Operacja nie powiodła się.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać PixelFormatwłaściwości HeightWidth , Scan0, LockBitsi UnlockBits , oraz ImageLockMode wyliczenia. Ten przykład został zaprojektowany do użycia z formularzami systemu Windows. Ten przykład nie jest przeznaczony do poprawnego działania ze wszystkimi formatami pikseli, ale w celu udostępnienia przykładu LockBits użycia metody . Aby uruchomić ten przykład, wklej go w formularzu i obsłuż zdarzenie formularza Paint , wywołując LockUnlockBitsExample metodę , 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

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

Określa BitmapData atrybuty Bitmap, takie jak rozmiar, format pikseli, adres początkowy 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 System.Drawing.Imaging.PixelFormat wyliczenia zawierającego określoną wartość bitów na piksel (BPP). Użycie System.Drawing.Imaging.PixelFormat wartości takich 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
Źródło:
Bitmap.cs
Źródło:
Bitmap.cs
Ź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ęść Bitmap obiektu do zablokowania.

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 blokowania.

Zwraca

Element BitmapData zawierający informacje o operacji blokowania.

Wyjątki

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

— lub —

PixelFormat Niepoprawny element jest przekazywany dla mapy bitowej.

Operacja nie powiodła się.

Uwagi

LockBits Użyj metody , aby zablokować istniejącą mapę bitową w pamięci systemowej, aby można było ją zmienić programowo. Kolor obrazu można zmienić za SetPixel pomocą 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 System.Drawing.Imaging.PixelFormat wyliczenia zawierającego określoną wartość bitów na piksel (BPP). Użycie System.Drawing.Imaging.PixelFormat wartości, takich jak Indexed i Gdi, spowoduje zgłoszenie elementu 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