다음을 통해 공유


방법: 색 전단

전단 변환에서는 다른 색 구성 요소의 일정 비율로 색 구성 요소를 늘리거나 줄입니다. 예를 들어, 빨강 구성 요소를 파랑 구성 요소 값의 50%만큼 늘리는 변환에서는 색 (0.2, 0.5, 1)이 (0.7, 0.5, 1)로 변환됩니다. 여기서 변환된 빨강 구성 요소의 값은 0.2 + (1/2)(1)의 결과로 0.7이 되었습니다.

예제

다음 예제에서는 ColorBars4.bmp 파일에서 Image 개체를 만듭니다. 그런 다음 코드에서는 위 단락에서 설명한 전단 변환을 이미지의 각 픽셀에 적용합니다.

아래 그림에서 왼쪽은 원래 이미지이고 오른쪽은 기울이기 변환이 적용된 이미지입니다.

색 전단

아래 표에는 전단 변환을 수행하기 전과 변환 후의 막대 네 개에 대한 색 벡터가 나와 있습니다.

원래 설정

기울이기 적용 후 이미지

(0, 0, 1, 1)

(0.5, 0, 1, 1)

(0.5, 1, 0.5, 1)

(0.75, 1, 0.5, 1)

(1, 1, 0, 1)

(1, 1, 0, 1)

(0.4, 0.4, 0.4, 1)

(0.6, 0.4, 0.4, 1)

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

Dim colorMatrixElements As Single()() = _
    {New Single() {1, 0, 0, 0, 0}, _
        New Single() {0, 1, 0, 0, 0}, _
        New Single() {0.5F, 0, 1, 0, 0}, _
        New Single() {0, 0, 0, 1, 0}, _
        New Single() {0, 0, 0, 0, 1}}

Dim colorMatrix As New ColorMatrix(colorMatrixElements)

imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, _
    ColorAdjustType.Bitmap)

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

e.Graphics.DrawImage(image, New Rectangle(150, 10, width, height), 0, 0, _
    width, height, GraphicsUnit.Pixel, imageAttributes)
Image image = new Bitmap("ColorBars.bmp");
ImageAttributes imageAttributes = new ImageAttributes();
int width = image.Width;
int height = image.Height;

float[][] colorMatrixElements = { 
        new float[] {1,  0,  0,  0, 0},
        new float[] {0,  1,  0,  0, 0},
        new float[] {0.5f,  0,  1,  0, 0},
        new float[] {0,  0,  0,  1, 0},
        new float[] {0, 0, 0, 0, 1}};

ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);

imageAttributes.SetColorMatrix(
   colorMatrix,
   ColorMatrixFlag.Default,
   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를 필요로 합니다. ColorBars.bmp를 시스템에서 사용할 수 있는 이미지 이름 및 경로로 바꾸십시오.

참고 항목

참조

ColorMatrix

ImageAttributes

기타 리소스

Windows Forms의 그래픽 및 그리기

이미지 다시 칠하기