The following example creates a
Metafile. The code uses the
Metafile to record a sequence of graphics commands and then saves the recorded commands in a file named
SampleMetafile.emf. Note that the function that creates the metafile (
GdipRecordMetafileFileName) receives a device context handle, and we retrieve a
Graphics handle calling the
GdipGetImageGraphicsContext function passing the metafile handle. The recording stops (and the recorded commands are saved to the file) when the
Graphics handle is deleted with
GdipDeleteGraphics. To display the metafile, a new
Graphics handle is retrieved calling the
GdipCreateFromHDC function and passing the
Graphics and
Metafile handles to the
GdipDrawImage function. Note that the code uses the same
Metafile handle to record and to display (play back) the metafile. A metafile contains its own graphics state, which is defined by the
Graphics object used to record the metafile. Any properties of the
Graphics object (clip region, world transformation, smoothing mode, and the like) that you set while recording the metafile will be stored in the metafile. When you display the metafile, the drawing will be done according to those stored properties.
C++
Metafile metafile(L"SampleMetafile.emf", hdc);
{
Graphics graphics(&metafile);
Pen greenPen(Color(255, 0, 255, 0));
SolidBrush solidBrush(Color(255, 0, 0, 255));
// Add a rectangle and an ellipse to the metafile.
graphics.DrawRectangle(&greenPen, Rect(50, 10, 25, 75));
graphics.DrawEllipse(&greenPen, Rect(100, 10, 25, 75));
// Add an ellipse (drawn with antialiasing) to the metafile.
graphics.SetSmoothingMode(SmoothingModeHighQuality);
graphics.DrawEllipse(&greenPen, Rect(150, 10, 25, 75));
// Add some text (drawn with antialiasing) to the metafile.
FontFamily fontFamily(L"Arial");
Font font(&fontFamily, 24, FontStyleRegular, UnitPixel);
graphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
graphics.RotateTransform(30.0f);
graphics.DrawString(L"Smooth Text", 11, &font,
PointF(50.0f, 50.0f), &solidBrush);
} // End of recording metafile.
// Play back the metafile.
Graphics playbackGraphics(hdc);
playbackGraphics.DrawImage(&metafile, 200, 100);
PowerBASIC
SUB GDIP_RecordMetafile (BYVAL hdc AS DWORD)
LOCAL hStatus AS LONG
LOCAL pMetafile AS DWORD
LOCAL pGraphics AS DWORD
LOCAL pPlaybackGraphics AS DWORD
LOCAL pGreenPen AS DWORD
LOCAL pSolidBrush AS DWORD
LOCAL pFontFamily AS DWORD
LOCAL pFont AS DWORD
LOCAL strFileName AS STRING
LOCAL strDesc AS STRING
LOCAL strFontName AS STRING
LOCAL strText AS STRING
LOCAL rcf AS RECTF
' // Create a Metafile object for recording
strFileName = UCODE$("SampleMetafile.emf")
strDesc = UCODE$("")
hStatus = GdipRecordMetafileFileName(STRPTR(strFileName), hdc, _
%EmfTypeEmfPlusDual, BYVAL %NULL, %MetafileFrameUnitGdi, _
STRPTR(strDesc), pMetafile)
IF pMetaFile THEN
' // Create a Graphics object that is associated with the Metafile object
hStatus = GdipGetImageGraphicsContext(pMetafile, pGraphics)
IF pGraphics THEN
' // Create a pen and a solid brush
hStatus = GdipCreatePen1(GDIP_ARGB(255, 0, 255, 0), 1, %UnitPixel, pGreenPen)
hStatus = GdipCreateSolidFill(GDIP_ARGB(255, 0, 0, 255), pSolidBrush)
' // Add a rectangle and an ellipse to the metafile
hStatus = GdipDrawRectangle(pGraphics, pGreenPen, 50, 10, 25, 75)
hStatus = GdipDrawEllipse(pGraphics, pGreenPen, 100, 10, 25, 75)
' // Add an ellipse (drawn with antialiasing) to the metafile.
hStatus = GdipSetSmoothingMode(pGraphics, %SmoothingModeHighQuality)
hStatus = GdipDrawEllipse(pGraphics, pGreenPen, 150, 10, 25, 75)
' // Add some text (drawn with antialiasing) to the metafile.
strFontName = UCODE$("Arial")
hStatus = GdipCreateFontFamilyFromName(STRPTR(strFontName), %NULL, pFontFamily)
IF pFontFamily THEN
hStatus = GdipCreateFont(pFontFamily, 24, %FontStyleRegular, %UnitPixel, pFont)
GdipDeleteFontFamily(pFontFamily)
END IF
hStatus = GdipSetTextRenderingHint(pGraphics, %TextRenderingHintAntiAlias)
hStatus = GdipRotateWorldTransform(pGraphics, 30.0!, %MatrixOrderPrepend)
strText = UCODE$("Smooth Text")
rcf.x = 50.0! : rcf.y = 50.0!
hStatus = GdipDrawString(pGraphics, STRPTR(strText), LEN(strText) \ 2, pFont, rcf, %NULL, pSolidBrush)
' // Stop recording by deleting the Graphics object
GdipDeleteGraphics(pGraphics)
' // Free resources
IF pFont THEN GdipDeleteFont(pFont)
IF pSolidBrush THEN GdipDeleteBrush(pSolidBrush)
IF pGreenPen THEN GdipDeletePen(pGreenPen)
END IF
END IF
' // Create a Graphics object that is associated with the device context
hStatus = GdipCreateFromHDC(hdc, pPlaybackGraphics)
IF pPlaybackGraphics THEN
' // Display the metafile
hStatus = GdipDrawImage(pPlaybackGraphics, pMetafile, 200, 100)
' // Delete the Graphics object
GdipDeleteGraphics(pPlaybackGraphics)
END IF
' // Dispose the Metafile object
IF pMetafile THEN GdipDisposeImage(pMetafile)
END SUB
(http://www.jose.it-berater.org/captures/GdipRecordMetafileFileName.png)