Bitmap.LockBits Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Blocca un oggetto Bitmap nella memoria di sistema.
Overload
| Nome | Descrizione |
|---|---|
| LockBits(Rectangle, ImageLockMode, PixelFormat) |
Blocca un oggetto Bitmap nella memoria di sistema. |
| LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData) |
Blocca un oggetto Bitmap nella memoria di sistema. |
LockBits(Rectangle, ImageLockMode, PixelFormat)
Blocca un oggetto Bitmap nella memoria di sistema.
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
Parametri
- flags
- ImageLockMode
Enumerazione ImageLockMode che specifica il livello di accesso (lettura/scrittura) per .Bitmap
- format
- PixelFormat
Enumerazione PixelFormat che specifica il formato di dati di questo Bitmapoggetto .
Valori restituiti
Oggetto BitmapData che contiene informazioni su questa operazione di blocco.
Eccezioni
non PixelFormat è un valore specifico per bit per pixel.
oppure
L'oggetto non corretto PixelFormat viene passato per una bitmap.
Operazione non riuscita.
Esempio
Nell'esempio di codice seguente viene illustrato come utilizzare le PixelFormatproprietà , WidthHeight, e , Scan0 i LockBits metodi e UnlockBits e l'enumerazione ImageLockMode . Questo esempio è progettato per essere usato con Windows Form. Questo esempio non è progettato per funzionare correttamente con tutti i formati pixel, ma per fornire un esempio di come usare il LockBits metodo . Per eseguire questo esempio, incollarlo in un modulo e gestire l'evento del Paint modulo chiamando il LockUnlockBitsExample metodo , passando e come 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
Commenti
Usare il LockBits metodo per bloccare una bitmap esistente nella memoria di sistema in modo che possa essere modificata a livello di codice. È possibile modificare il colore di un'immagine con il SetPixel metodo , anche se il LockBits metodo offre prestazioni migliori per le modifiche su larga scala.
BitmapData Specifica gli attributi dell'oggetto Bitmap, ad esempio dimensioni, formato pixel, indirizzo iniziale dei dati pixel in memoria e lunghezza di ogni riga di analisi (stride).
Quando si chiama questo metodo, è consigliabile usare un membro dell'enumerazione System.Drawing.Imaging.PixelFormat che contiene un valore BPP (Bits-Per-Pixel) specifico. L'uso di System.Drawing.Imaging.PixelFormat valori come Indexed e Gdi genererà un'eccezione System.ArgumentException. Inoltre, passando il formato di pixel non corretto per una bitmap genererà un oggetto System.ArgumentException.
Si applica a
LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)
Blocca un oggetto Bitmap nella memoria di sistema.
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
Parametri
- flags
- ImageLockMode
Uno dei valori che specifica il livello di ImageLockMode accesso (lettura/scrittura) per .Bitmap
- format
- PixelFormat
Uno dei PixelFormat valori che specifica il formato di dati dell'oggetto Bitmap.
- bitmapData
- BitmapData
Oggetto BitmapData contenente informazioni sull'operazione di blocco.
Valori restituiti
Oggetto BitmapData contenente informazioni sull'operazione di blocco.
Eccezioni
PixelFormat value non è un valore specifico per bit per pixel.
oppure
L'oggetto non corretto PixelFormat viene passato per una bitmap.
Operazione non riuscita.
Commenti
Usare il LockBits metodo per bloccare una bitmap esistente nella memoria di sistema in modo che possa essere modificata a livello di codice. È possibile modificare il colore di un'immagine con il SetPixel metodo , anche se il LockBits metodo offre prestazioni migliori per le modifiche su larga scala.
Quando si chiama questo metodo, è consigliabile usare un membro dell'enumerazione System.Drawing.Imaging.PixelFormat che contiene un valore BPP (Bits-Per-Pixel) specifico. L'uso di System.Drawing.Imaging.PixelFormat valori, ad esempio Indexed e Gdi, genererà un'eccezione System.ArgumentException. Inoltre, passando il formato di pixel non corretto per una bitmap genererà un oggetto System.ArgumentException.
Questa versione del LockBits metodo deve essere usata con il flags valore .ImageLockMode.UserInputBuffer