GDI+ 이미지 자르기 및 크기 조정

Graphics 클래스는 여러 DrawImage 메서드를 제공하며, 그 중 일부는 이미지를 자르고 크기 조정하는 데 사용할 수 있는 원본 및 대상 사각형 매개 변수가 있습니다.

다음 예제에서는 파일 Apple.gif Image 개체를 생성합니다. 코드는 전체 사과 이미지를 원래 크기로 그립니다. 그런 다음, 코드는 Graphics 개체의 DrawImage 메서드를 호출하여 원래 사과 이미지보다 큰 대상 사각형에 사과 이미지의 일부를 그립니다.

DrawImage 메서드는 세 번째, 네 번째, 다섯 번째 및 여섯 번째 인수로 지정된 원본 사각형을 확인하여 그릴 사과 부분을 결정합니다. 이 경우 사과는 너비의 75%, 높이의 75%로 자릅니다.

DrawImage 메서드는 잘린 사과를 그릴 위치와 두 번째 인수로 지정된 대상 직사각형을 확인하여 잘린 사과를 만드는 정도를 결정합니다. 이 경우 대상 사각형은 원래 이미지보다 30% 더 넓고 30% 더 큽니다.

Image image(L"Apple.gif");
UINT width = image.GetWidth();
UINT height = image.GetHeight();
// Make the destination rectangle 30 percent wider and
// 30 percent taller than the original image.
// Put the upper-left corner of the destination
// rectangle at (150, 20).
Rect destinationRect(150, 20, 1.3 * width, 1.3 * height);
// Draw the image unaltered with its upper-left corner at (0, 0).
graphics.DrawImage(&image, 0, 0);
// Draw a portion of the image. Scale that portion of the image
// so that it fills the destination rectangle.
graphics.DrawImage(
   &image,
   destinationRect,
   0, 0,              // upper-left corner of source rectangle
   0.75 * width,      // width of source rectangle
   0.75 * height,     // height of source rectangle
   UnitPixel);

다음 그림에서는 원래 사과와 잘린 스케일링된 사과를 보여 줍니다.

원래 사과의 확대 부분을 보여주는 그림