방법: 이미지 색 변환
변환은 네 가지 색 구성 요소 중 하나 이상에 값을 추가합니다. 변환을 나타내는 색 행렬 항목은 다음 표에 제공되어 있습니다.
변환할 구성 요소 | 행렬 항목 |
---|---|
빨간색 | [4][0] |
녹색 | [4][1] |
파랑 | [4][2] |
알파 | [4][3] |
예제
다음 예제에서는 ColorBars.bmp 파일에서 Image 개체를 생성합니다. 그런 다음, 코드는 이미지에서 각 픽셀의 빨간색 구성 요소에 0.75를 추가합니다. 원본 이미지는 변환된 이미지와 함께 그려집니다.
다음 그림에서는 왼쪽에 원본 이미지, 오른쪽에 변환된 이미지를 보여줍니다.
다음 표에서는 빨간색 변환 전후에 네 개의 막대에 대한 색 벡터를 나열합니다. 색 구성 요소의 최댓값은 1이므로, 두 번째 행의 빨간색 구성 요소는 변경되지 않습니다. (마찬가지로 색 구성 요소의 최솟값은 0입니다.)
Original | 변역됨 |
---|---|
검정색(0, 0, 0, 1) | (0.75, 0, 0, 1) |
빨간색(1, 0, 0, 1) | (1, 0, 0, 1) |
녹색(0, 1, 0, 1) | (0.75, 1, 0, 1) |
파란색(0, 0, 1, 1) | (0.75, 0, 1, 1) |
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, 0, 1, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {.75f, 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);
Dim image As 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, 0, 1, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0.75F, 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)
' 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)
코드 컴파일
앞의 예제는 Windows forms에서 사용하도록 설계되었으며 PaintEventArgs 이벤트 처리기의 매개 변수인 e
Paint가 필요합니다. ColorBars.bmp
를 시스템에서 유효한 이미지 파일 이름과 경로로 바꿉니다.
참고 항목
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET Desktop feedback
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기