GdipCreateFromHDC2function
Creates a **Graphics** object that is associated with a specified device context and a specified device handle.
Syntax
FUNCTION GdipCreateFromHDC2 (BYVAL hdc AS HDC, BYVAL hDevice AS HANDLE, BYVAL graphics AS GpGraphics PTR PTR) AS GpStatus
Parameters
| Name | Description | |
|---|---|---|
hdc | [in] Handle to a device context that will be associated with the new Graphics object. | |
hdevice | [in] Handle to a device that will be associated with the new Graphics object (e.g., printer or display surface). | |
graphics | [out] Pointer to a GpGraphics object variable that receives a pointer to the new created Graphics object. |
Description
Creates a Graphics object that is associated with a specified device context and a specified device handle.
Remarks
When you use this function to create a Graphics object, make sure that the Graphics object is deleted before the device context is released.
Example
' ======================================================================================== ' This example demonstrates how to use GdipCreateFromHDC2 with a device handle, ' and how to create a bitmap from a graphics object for off-screen rendering. ' ======================================================================================== SUB Example_CreateFromHDC2 (BYVAL hdc AS HDC)
DIM hStatus AS LONG DIM graphics AS GpGraphics PTR
DIM hPrinter AS HANDLE DIM printerName AS WSTRING * 128 = "Microsoft Print to PDF" IF OpenPrinter(printerName, @hPrinter, NULL) = FALSE THEN EXIT SUB
' // Create graphics object using HDC and device handle hStatus = GdipCreateFromHDC2(hdc, hPrinter, @graphics)
' // Get the DPI scaling ratios DIM dpiX AS SINGLE hStatus = GdipGetDpiX(graphics, @dpiX) DIM rxRatio AS SINGLE = dpiX / 96 DIM dpiY AS SINGLE hStatus = GdipGetDpiY(graphics, @dpiY) Dim ryRatio AS SINGLE = dpiY / 96 ' // Set the scale transform hStatus = GdipScaleWorldTransform(graphics, rxRatio, ryRatio, MatrixOrderPrepend)
' // Create a bitmap from the graphics object DIM bitmap AS GpBitmap PTR hStatus = GdipCreateBitmapFromGraphics(300, 200, graphics, @bitmap)
' // Create a graphics object from the bitmap DIM bmpGraphics AS GpGraphics PTR hStatus = GdipGetImageGraphicsContext(bitmap, @bmpGraphics)
' // Draw something on the bitmap DIM brush AS GpBrush PTR hStatus = GdipCreateSolidFill(ARGB_GREEN, @brush) hStatus = GdipFillRectangle(bmpGraphics, brush, 50, 50, 200, 100)
' // Save the bitmap as a .png DIM clsid AS CLSID = AfxGdipGetEncoderClsid("image/png") ' Helper function to get PNG encoder GdipSaveImageToFile(bitmap, "output.png", @clsid, NULL)
' // Close the printer object IF hPrinter THEN ClosePrinter(hPrinter)
' // Draw message on the window DIM fontFamily AS GpFontFamily PTR, font AS GpFont PTR hStatus = GdipCreateFontFamilyFromName("Arial", NULL, @fontFamily) IF hStatus = StatusOk THEN
hStatus = GdipCreateFont(fontFamily, AfxGdipPointsToPixels(12, TRUE), FontStyleRegular, UnitPoint, @font)
GdipDeleteFontFamily(fontFamily)
END IF
DIM textBrush AS GpBrush PTR hStatus = GdipCreateSolidFill(ARGB_BLACK, @textBrush)
DIM rcf AS GpRectF rcf.x = 50 : rcf.y = 80 : rcf.Width = 350 : rcf.Height = 90 DIM wszText AS WSTRING * 128 = "Image saved to output.png" hStatus = GdipDrawString(graphics, wszText, LEN(wszText), font, @rcf, NULL, textBrush)
' // Cleanup IF brush THEN GdipDeleteBrush(brush) IF bmpGraphics THEN GdipDeleteGraphics(bmpGraphics) IF bitmap THEN GdipDisposeImage(bitmap) IF graphics THEN GdipDeleteGraphics(graphics)
END SUB ' ========================================================================================
Reference
- Defined in AfxNova/AfxGdiPlus.bi:1745
- Documented in Graphics/GdiPlus Flat Api/GdiPlusGraphics.md
- Topic: Graphics Functions