The following example draws the original source image and then draws a portion of the image in a specified rectangle.
C++
VOID Example_DrawImage5(HDC hdc)
{
Graphics graphics(hdc);
// Create an Image object.
Image image(L"pattern.png");
// Draw the original source image.
graphics.DrawImage(&image, 10, 10);
// Define the portion of the image to draw.
int srcX = 70;
int srcY = 20;
int srcWidth = 100;
int srcHeight = 100;
// Create a Rect object that specifies the destination of the image.
Rect destRect(200, 10, image.GetWidth(), image.GetHeight());
// Create an ImageAttributes object that specifies a recoloring from red to blue.
ImageAttributes remapAttributes;
ColorMap redToBlue;
redToBlue.oldColor = Color(255, 255, 0, 0);
redToBlue.newColor = Color(255, 0, 0, 255);
remapAttributes.SetRemapTable(1, &redToBlue);
// Draw the resized image.
graphics.DrawImage(
&image,
destRect,
srcX,
srcY,
srcWidth,
srcHeight,
UnitPixel,
&remapAttributes,
NULL,
NULL);
}
PowerBASIC
SUB GDIP_GdipDrawImageRectRect (BYVAL hdc AS DWORD)
LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL pImage AS DWORD
LOCAL strFileName AS STRING
LOCAL nWidth AS DWORD
LOCAL nHeight AS DWORD
LOCAL srcx AS SINGLE
LOCAL srcy AS SINGLE
LOCAL srcwidth AS SINGLE
LOCAL srcheight AS SINGLE
LOCAL pRemapAttributes AS DWORD
LOCAL redToBlue AS GDIP_COLORMAP
DIM destPoints(2) AS POINTF
hStatus = GdipCreateFromHDC(hdc, pGraphics)
' // Create the Image object
strFileName = UCODE$("pattern.png")
hStatus = GdipLoadImageFromFile(STRPTR(strFileName), pImage)
' // Draw the original source image.
hStatus = GdipDrawImage(pGraphics, pImage, 10, 10)
' // Define the portion of the image to draw.
srcX = 70.0!
srcY = 20.0!
srcwidth = 100.0!
srcheight = 100.0!
' // Get the width and height of the image.
hStatus = GdipGetImageWidth(pImage, nWidth)
hStatus = GdipGetImageHeight(pImage, nHeight)
' // Create an ImageAttributes object that specifies a recoloring from red to blue.
hStatus = GdipCreateImageAttributes(pRemapAttributes)
redToBlue.oldColor = GDIP_ARGB(255, 255, 0, 0)
redToBlue.newColor = GDIP_ARGB(255, 0, 0, 255)
hStatus = GdipSetImageAttributesRemapTable(pRemapAttributes, %ColorAdjustTypeDefault, %TRUE, 1, redToBlue)
' Draw the cropped image
hStatus = GdipDrawImageRectRect(pGraphics, pImage, 200, 10, nWidth, nHeight, srcx, srcy, _
srcwidth, srcheight, %UnitPixel, pRemapAttributes, %NULL, %NULL)
' // Cleanup
IF pRemapAttributes THEN GdipDisposeImageAttributes(pRemapAttributes)
IF pImage THEN GdipDisposeImage(pImage)
IF pGraphics THEN GdipDeleteGraphics(pGraphics)
END SUB
(http://www.jose.it-berater.org/captures/GdipDrawImageRectRect.png)