Bitmap.LockBits 메서드

정의

Bitmap을 시스템 메모리에 잠급니다.

오버로드

LockBits(Rectangle, ImageLockMode, PixelFormat)

Bitmap을 시스템 메모리에 잠급니다.

LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)

Bitmap을 시스템 메모리에 잠급니다.

LockBits(Rectangle, ImageLockMode, PixelFormat)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

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

매개 변수

rect
Rectangle

잠글 Rectangle의 부분을 지정하는 Bitmap 구조체입니다.

flags
ImageLockMode

ImageLockMode의 액세스 수준(읽기/쓰기)을 지정하는 Bitmap 열거형입니다.

format
PixelFormat

PixelFormat의 데이터 형식을 지정하는 Bitmap 열거형입니다.

반환

이 잠금 작업에 대한 정보가 들어 있는 BitmapData입니다.

예외

PixelFormat이 특정 비트/픽셀 값이 아닌 경우

또는

잘못된 PixelFormat이 비트맵에 대해 전달된 경우

작업이 실패했습니다.

예제

다음 코드 예제를 사용 PixelFormat하는 방법을 보여 줍니다.는 , Height, WidthScan0 속성, LockBitsUnlockBits 메서드; 및 ImageLockMode 열거형입니다. 이 예제는 Windows Forms 함께 사용하도록 설계되었습니다. 이 예제는 모든 픽셀 형식에서 올바르게 작동하는 것이 아니라 메서드를 사용하는 LockBits 방법의 예를 제공하도록 설계되었습니다. 이 예제를 실행하려면 폼에 붙여넣고 메서드를 호출 LockUnlockBitsExample 하고 를 로 PaintEventArgs전달 e 하여 폼의 Paint 이벤트를 처리합니다.

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

설명

메서드를 LockBits 사용하여 시스템 메모리에서 기존 비트맵을 잠그면 프로그래밍 방식으로 변경할 수 있습니다. 메서드는 대규모 변경에 더 나은 성능을 제공하지만 메서드를 LockBits 사용하여 이미지 SetPixel 의 색을 변경할 수 있습니다.

BitmapData 의 특성 Bitmap(예: 크기, 픽셀 형식, 메모리에 있는 픽셀 데이터의 시작 주소 및 각 스캔 줄의 길이)을 지정합니다.

이 메서드를 호출할 때 특정 BPP(픽셀당 비트) 값을 포함하는 열거형의 System.Drawing.Imaging.PixelFormat 멤버를 사용해야 합니다. 와 Gdi 같은 Indexed 값을 사용하면 System.Drawing.Imaging.PixelFormatSystem.ArgumentExceptionthrow됩니다. 또한 비트맵에 대해 잘못된 픽셀 형식을 전달하면 가 System.ArgumentExceptionthrow됩니다.

적용 대상

LockBits(Rectangle, ImageLockMode, PixelFormat, BitmapData)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

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

매개 변수

rect
Rectangle

잠글 Bitmap의 부분을 지정하는 사각형 구조체입니다.

flags
ImageLockMode

ImageLockMode의 액세스 수준(읽기/쓰기)을 지정하는 Bitmap 값 중 하나입니다.

format
PixelFormat

PixelFormat의 데이터 형식을 지정하는 Bitmap 값 중 하나입니다.

bitmapData
BitmapData

잠금 작업에 대한 정보가 들어 있는 BitmapData입니다.

반환

잠금 작업에 대한 정보가 들어 있는 BitmapData입니다.

예외

PixelFormat 값이 특정 비트/픽셀 값이 아닌 경우

또는

잘못된 PixelFormat이 비트맵에 대해 전달된 경우

작업이 실패했습니다.

설명

메서드를 LockBits 사용하여 시스템 메모리에서 기존 비트맵을 잠그면 프로그래밍 방식으로 변경할 수 있습니다. 메서드는 대규모 변경에 더 나은 성능을 제공하지만 메서드를 LockBits 사용하여 이미지 SetPixel 의 색을 변경할 수 있습니다.

이 메서드를 호출할 때 특정 BPP(픽셀당 비트) 값을 포함하는 열거형의 System.Drawing.Imaging.PixelFormat 멤버를 사용해야 합니다. 및 Gdi와 같은 Indexed 값을 사용하면 System.Drawing.Imaging.PixelFormatSystem.ArgumentExceptionthrow됩니다. 또한 비트맵에 대해 잘못된 픽셀 형식을 전달하면 가 System.ArgumentExceptionthrow됩니다.

이 버전의 LockBits 메서드는 값ImageLockMode.UserInputBuffer과 함께 flags 사용됩니다.

적용 대상