ImageLockMode 枚举

定义

指定传递给 LockBits 方法的标志参数的标志。 LockBits 方法可锁定图像的某一部分,以便读取或写入像素数据。

public enum class ImageLockMode
public enum ImageLockMode
type ImageLockMode = 
Public Enum ImageLockMode
继承
ImageLockMode

字段

ReadOnly 1

指定锁定图像的一部分以便读取。

ReadWrite 3

指定锁定图像的一部分以便读取或写入。

UserInputBuffer 4

指定由用户分配读取或写入像素数据时使用的缓冲区。 如果设置此标志,那么 LockBits 方法的 flags 参数将充当输入参数(也可能充当输出参数)。 如果清除此标志,那么 flags 参数仅充当输出参数。

WriteOnly 2

指定锁定图像的一部分以便写入。

示例

下面的代码示例演示如何使用 、、 和 属性、LockBitsUnlockBits 方法以及 ImageLockMode 枚举。Scan0WidthHeightPixelFormat 此示例旨在与 Windows 窗体 一起使用。 若要运行此示例,请将其粘贴到窗体中,并通过调用 LockUnlockBitsExample 方法处理窗体的 Paint 事件,并e作为 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

适用于