The following example creates an
Image object based on a metafile and then draws the image. Next, the code calls the
GdipGetBounds function to get the bounding rectangle for the image. The code makes two attempts to display 75 percent of the image. The first attempt fails because it specifies (0, 0) for the upper-left corner of the source rectangle. The second attempt succeeds because it uses the X and Y data members returned by
GdipGetBounds to specify the upper-left corner of the source rectangle.
C++
VOID Example_GetBounds(HDC hdc)
{
Graphics graphics(hdc);
Image image(L"SampleMetafile2.emf");
graphics.DrawImage(&image, 0, 0);
// Get the bounding rectangle for the image (metafile).
RectF boundsRect;
Unit unit;
image.GetBounds(&boundsRect, &unit);
// Attempt to draw 75 percent of the image.
// Less than 75 percent of the image will be visible because the
// upper-left corner of the image's bounding rectangle is not (0, 0).
RectF dstRect(250.0f, 0.0f, 100.0f, 50.0f);
graphics.DrawImage(
&image,
dstRect, // destination rectangle
0.0f, 0.0f, // upper-left corner of source rectangle
0.75f*boundsRect.Width, // width of source rectangle
boundsRect.Height, // height of source rectangle
UnitPixel);
// Draw 75 percent of the image.
dstRect.Y = 80.0f;
graphics.DrawImage(
&image,
dstRect, // destination rectangle
boundsRect.X, boundsRect.Y, // upper-left corner of source rectangle
0.75f*boundsRect.Width, // width of source rectangle
boundsRect.Height, // height of source rectangle
UnitPixel);
}
PowerBASIC
SUB GDIP_GetImageBounds (BYVAL hdc AS DWORD)
LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL pImage AS DWORD
LOCAL strFileName AS STRING
LOCAL boundsRect AS RECTF
LOCAL unit AS LONG
hStatus = GdipCreateFromHDC(hdc, pGraphics)
' // Create an Image object, and then clone it.
strFileName = UCODE$("climber.emf")
hStatus = GdipLoadImageFromFile(STRPTR(strFileName), pImage)
' // Draw the image.
hStatus = GdipDrawImage(pGraphics, pImage, 0, 0)
' // Get the bounding rectangle for the image (metafile).
hStatus = GdipGetImageBounds(pImage, boundsRect, unit)
' // Attempt to draw 75 percent of the image.
' // Less than 75 percent of the image will be visible because the
' // upper-left corner of the image's bounding rectangle is not (0, 0).
hStatus = GdipDrawImagePointRect(pGraphics, pImage, 250, 0, 100, 50, 0.75 * boundsRect.Width, boundsRect.Height, %UnitPixel)
' // Draw 75 percent of the image.
hStatus = GdipDrawImagePointRect(pGraphics, pImage, 250, 80, boundsRect.x, boundsRect.y, 0.75 * boundsRect.Width, boundsRect.Height, %UnitPixel)
' // Cleanup
IF pImage THEN GdipDisposeImage(pImage)
IF pGraphics THEN GdipDeleteGraphics(pGraphics)
END SUB
(http://www.jose.it-berater.org/captures/GdipGetImageBounds.png)