Interactive PowerBasic Forum

Legacy Software (PBWIN 9.0+/PBCC 5.0+) => Source Code => Graphics and Multimedia => GDI+ (GDI Plus) => Topic started by: José Roca on June 23, 2008, 02:29:20 AM

Title: GDI+: GdipGetImageGraphicsContext
Post by: José Roca on June 23, 2008, 02:29:20 AM


The following example calls the GdipGetImageGraphicsContext function to create a Graphics object that is associated with an Image object. The call to GdipFillEllipse does not paint on the display device; instead, it alters the bitmap of the Image object. The call to GdipDrawImage displays the altered bitmap.

C++


VOID Example_FromImage(HDC hdc)
{
   Graphics graphics(hdc);

   // Create an Image object from a PNG file.
   Image image(L"Mosaic.png");

   // Create a Graphics object that is associated with the image.
   Graphics* imageGraphics = Graphics::FromImage(&image);
   
   // Alter the image.
   SolidBrush brush(Color(255, 0, 0, 255));
   imageGraphics->FillEllipse(&brush, 10, 40, 100, 50);

   // Draw the altered image.
   graphics.DrawImage(&image, 30, 20);
   
   delete imageGraphics;
}


PowerBASIC


SUB GDIP_GetImageGraphicsContext (BYVAL hdc AS DWORD)

   LOCAL hStatus AS LONG
   LOCAL pGraphics AS DWORD
   LOCAL pImage AS DWORD
   LOCAL pImageGraphics AS DWORD
   LOCAL pBrush AS DWORD
   LOCAL strFileName AS STRING

   hStatus = GdipCreateFromHDC(hdc, pGraphics)

   ' // Create an Image object from a PNG file.
   strFileName = UCODE$("pattern.png")
   hStatus = GdipLoadImageFromFile(STRPTR(strFileName), pImage)

   ' // Create a Graphics object that is associated with the image.
   hStatus = GdipGetImageGraphicsContext(pImage, pImageGraphics)

   ' // Alter the image.
   hStatus = GdipCreateSolidFill(GDIP_ARGB(255, 0, 255, 0), pBrush)
   hStatus = GdipFillEllipse(pImageGraphics, pBrush, 10, 40, 100, 50)

   ' // Draw the altered image.
   hStatus = GdipDrawImage(pGraphics, pImage, 30, 20)

   ' // Cleanup
   IF pBrush THEN GdipDeleteBrush(pBrush)
   IF pImage THEN GdipDisposeImage(pImage)
   IF pImageGraphics THEN GdipDeleteGraphics(pImageGraphics)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB


(http://www.jose.it-berater.org/captures/GdipGetImageGraphicsContext.png)