다음을 통해 공유


방법: 색 매핑 변경 테이블 사용

매핑 변경은 색 매핑 변경 테이블에 따라 이미지의 색을 변환하는 과정입니다. 색 매핑 변경 테이블은 ColorMap 개체의 배열입니다. 배열의 각 ColorMap 개체에는 OldColor 속성과 NewColor 속성이 있습니다.

GDI+에서는 이미지를 그릴 때 이미지의 각 픽셀을 이전 색의 배열과 비교합니다. 픽셀의 색이 이전 색과 일치하면 상응하는 새로운 색으로 변경됩니다. 이러한 색 변경은 렌더링에만 적용되며, Image 개체나 Bitmap 개체에 저장되어 있는 이미지 자체의 색 값은 그대로입니다.

매핑이 변경된 이미지를 그리려면 ColorMap 개체의 배열을 초기화합니다. 해당 배열을 ImageAttributes 개체의 SetRemapTable 메서드에 전달한 다음, ImageAttributes 개체를 Graphics 개체의 DrawImage 메서드에 전달합니다.

예제

다음 예제에서는 RemapInput.bmp 파일에서 Image 개체를 만듭니다. 이 코드에서는 단일 ColorMap 개체로 구성되는 색 매핑 변경 테이블을 만듭니다. ColorRemap 개체의 OldColor 속성은 빨강이고 NewColor 속성은 파랑입니다. 이 코드에서는 매핑을 변경하기 전에 한 번, 매핑을 변경한 후 한 번 이미지를 그립니다. 매핑 변경에서는 빨강 픽셀을 모두 파랑으로 바꿉니다.

아래 그림에서 왼쪽은 원래 이미지이고 오른쪽은 매핑이 변경된 이미지입니다.

색 매핑 변경

        Dim image As New Bitmap("RemapInput.bmp")
        Dim imageAttributes As New ImageAttributes()
        Dim width As Integer = image.Width
        Dim height As Integer = image.Height
        Dim colorMap As New ColorMap()

        colorMap.OldColor = Color.FromArgb(255, 255, 0, 0) ' opaque red
        colorMap.NewColor = Color.FromArgb(255, 0, 0, 255) ' opaque blue
        Dim remapTable As ColorMap() = {colorMap}

        imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap)

        e.Graphics.DrawImage(image, 10, 10, width, height)

        ' Pass in the destination rectangle (2nd argument), the upper-left corner 
        ' (3rd and 4th arguments), width (5th argument),  and height (6th 
        ' argument) of the source rectangle.
        e.Graphics.DrawImage( _
           image, _
           New Rectangle(150, 10, width, height), _
           0, 0, _
           width, _
           height, _
           GraphicsUnit.Pixel, _
           imageAttributes)

Image image = new Bitmap("RemapInput.bmp");
ImageAttributes imageAttributes = new ImageAttributes();
int width = image.Width;
int height = image.Height;
ColorMap colorMap = new ColorMap();

colorMap.OldColor = Color.FromArgb(255, 255, 0, 0);  // opaque red
colorMap.NewColor = Color.FromArgb(255, 0, 0, 255);  // opaque blue

ColorMap[] remapTable = { colorMap };

imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

e.Graphics.DrawImage(image, 10, 10, width, height);

e.Graphics.DrawImage(
   image,
   new Rectangle(150, 10, width, height),  // destination rectangle 
   0, 0,        // upper-left corner of source rectangle 
   width,       // width of source rectangle
   height,      // height of source rectangle
   GraphicsUnit.Pixel,
   imageAttributes);

코드 컴파일

앞의 예제는 Windows Forms에서 사용해야 하며 Paint 이벤트 처리기의 매개 변수인 PaintEventArgs e를 필요로 합니다.

참고 항목

기타 리소스

이미지 다시 칠하기

이미지, 비트맵 및 메타파일