ImageLockMode-Enumeration
Gibt Flags an, die an den Flagsparameter der LockBits-Methode übergeben werden. Die LockBits-Methode sperrt einen Bereich eines Bildes, sodass Pixeldaten gelesen oder geschrieben werden können.
Namespace: System.Drawing.Imaging
Assembly: System.Drawing (in system.drawing.dll)
Syntax
'Declaration
Public Enumeration ImageLockMode
'Usage
Dim instance As ImageLockMode
public enum ImageLockMode
public enum class ImageLockMode
public enum ImageLockMode
public enum ImageLockMode
Member
Membername | Beschreibung | |
---|---|---|
![]() |
ReadOnly | Gibt an, dass ein Bereich des Bildes zum Lesen gesperrt ist. |
![]() |
ReadWrite | Gibt an, dass ein Bereich des Bildes zum Lesen oder Schreiben gesperrt ist. |
UserInputBuffer | Gibt an, dass der für das Lesen und Schreiben von Pixeldaten verwendete Puffer vom Benutzer reserviert ist. Wenn dieses Flag festgelegt ist, fungiert der flags-Parameter der LockBits-Methode als Eingabeparameter und möglicherweise als Ausgabeparameter. Wenn dieses Flag gelöscht wurde, fungiert der flags-Parameter ausschließlich als Ausgabeparameter. | |
![]() |
WriteOnly | Gibt an, dass ein Bereich des Bildes zum Schreiben gesperrt ist. |
Beispiel
Im folgenden Codebeispiel wird die Verwendung der PixelFormat-Eigenschaft, der Height-Eigenschaft, der Width-Eigenschaft und der Scan0-Eigenschaft sowie der LockBits-Methode, der UnlockBits-Methode und der ImageLockMode-Enumeration veranschaulicht. Dieses Beispiel ist für die Verwendung mit Windows Forms vorgesehen. Fügen Sie es zum Ausführen dieses Beispiels in ein Formular ein, behandeln Sie das Paint-Ereignis des Formulars, indem Sie die LockUnlockBitsExample
-Methode aufrufen, wobei Sie e als PaintEventArgs übergeben.
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 = bmp.Width * bmp.Height * 3
Dim rgbValues(bytes - 1) As Byte
' Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)
' Set every red value to 255.
For counter As Integer = 0 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
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.
// This code is specific to a bitmap with 24 bits per pixels.
int bytes = bmp.Width * bmp.Height * 3;
byte[] rgbValues = new byte[bytes];
// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
// Set every red value to 255.
for (int counter = 0; 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);
}
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 = bmp->Width * bmp->Height * 3;
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 red value to 255.
for ( int counter = 0; 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.get_Width(), bmp.get_Height());
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect,
System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.get_PixelFormat());
// Get the address of the first line.
IntPtr ptr = bmpData.get_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 = bmp.get_Width() * bmp.get_Height() * 3;
ubyte rgbValues[] = new ubyte[bytes];
// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
// Set every red value to 255.
for (int counter = 0; counter < rgbValues.get_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.get_Graphics().DrawImage(bmp, 0, 150);
} //LockUnlockBitsExample
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0