• Welcome to Jose's Read Only Forum 2023.
 

GDI+: GdipSaveGraphics

Started by José Roca, June 23, 2008, 10:15:58 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca



The following example sets the world transformation of a Graphics object to a rotation and then saves the state of the Graphics object. Next, the code calls GdipTranslateWorldTransform, and saves the state again. Then the code calls GdipScaleTransform. At that point, the world transformation of the Graphics object is a composite transformation: first rotate, then translate, then scale. The code uses a red pen to draw an ellipse that is transformed by that composite transformation.

The code passes state2, which was returned by the second call to GdipSaveGraphics, to the GdipRestoreGraphics function, and draws the ellipse again using a green pen. The green ellipse is rotated and translated but not scaled. Finally the code passes state1, which was returned by the first call to GdipSaveGraphics, to the GdipRestoreGraphics function, and draws the ellipse again using a blue pen. The blue ellipse is rotated but not translated or scaled.

C++


VOID Example_Save1(HDC hdc)
{
   Graphics graphics(hdc);
   GraphicsState state1, state2;

   graphics.RotateTransform(30.0f);
   state1 = graphics.Save();
   graphics.TranslateTransform(100.0f, 0.0f, MatrixOrderAppend);
   state2 = graphics.Save();
   graphics.ScaleTransform(1.0f, 3.0f, MatrixOrderAppend);
   
   // Draw an ellipse.
   // Three transformations apply: rotate, then translate, then scale.
   Pen redPen(Color(255, 255, 0, 0));
   graphics.DrawEllipse(&redPen, 0, 0, 100, 20);

   // Restore to state2 and draw the ellipse again.
   // Two transformations apply: rotate then translate.
   graphics.Restore(state2);
   Pen greenPen(Color(255, 0, 255, 0));
   graphics.DrawEllipse(&greenPen, 0, 0, 100, 20);

   // Restore to state1 and draw the ellipse again.
   // Only the rotation transformation applies.
   graphics.Restore(state1);
   Pen bluePen(Color(255, 0, 0, 255));
   graphics.DrawEllipse(&bluePen, 0, 0, 100, 20);
}


PowerBASIC


SUB GDIP_Save1 (BYVAL hdc AS DWORD)

   LOCAL hStatus AS LONG
   LOCAL pGraphics AS DWORD
   LOCAL state1 AS LONG
   LOCAL state2 AS LONG
   LOCAL pRedPen AS DWORD
   LOCAL pGreenPen AS DWORD
   LOCAL pBluePen AS DWORD

   hStatus = GdipCreateFromHDC(hdc, pGraphics)

   hStatus = GdipRotateWorldTransform(pGraphics, 30, %MatrixOrderPrepend)
   hStatus = GdipSaveGraphics(pGraphics, state1)
   hStatus = GdipTranslateWorldTransform(pGraphics, 100, 0, %MatrixOrderAppend)
   hStatus = GdipSaveGraphics(pGraphics, state2)
   hStatus = GdipScaleWorldTransform(pGraphics, 1, 3, %MatrixOrderAppend)

   ' // Draw an ellipse.
   ' // Three transformations apply: rotate, then translate, then scale.
   hStatus = GdipCreatePen1(GDIP_ARGB(255, 255, 0, 0), 1, %UnitWorld, pRedPen)
   hStatus = GdipDrawEllipse(pGraphics, pRedPen, 0, 0, 100, 20)
   hStatus = GdipRestoreGraphics(pGraphics, state2)

   ' // Restore to state2 and draw the ellipse again.
   ' // Two transformations apply: rotate then translate.
   hStatus = GdipRestoreGraphics(pGraphics, state2)
   hStatus = GdipCreatePen1(GDIP_ARGB(255, 0, 255, 0), 1, %UnitWorld, pGreenPen)
   hStatus = GdipDrawEllipse(pGraphics, pGreenPen, 0, 0, 100, 20)

   ' // Restore to state1 and draw the ellipse again.
   ' // Only the rotation transformation applies.
   hStatus = GdipRestoreGraphics(pGraphics, state1)
   hStatus = GdipCreatePen1(GDIP_ARGB(255, 0, 0, 255), 1, %UnitWorld, pBluePen)
   hStatus = GdipDrawEllipse(pGraphics, pBluePen, 0, 0, 100, 20)

   ' // Cleanup
   IF pBluePen THEN GdipDeletePen(pBluePen)
   IF pGreenPen THEN GdipDeletePen(pGreenPen)
   IF pRedPen THEN GdipDeletePen(pRedPen)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB




José Roca

#1
 
The following example sets the world transformation of a Graphics object to a rotation and then saves the state of the Graphics object. Next, the code calls GdipTranslateWorldTransform, and saves the state again. Then the code calls GdipScaleWorldTransform. At that point, the world transformation of the Graphics object is a composite transformation: first rotate, then translate, then scale. The code uses a red pen to draw an ellipse that is transformed by that composite transformation.

The code passes state1, which was returned by the first call to GdipSaveGraphics, to the GdipRestoreGraphics method, and draws the ellipse again using a green pen. The green ellipse is rotated but not translated or scaled.

Next the code attempts to restore the state identified by state2. The attempt fails because the call GdipRestoreGraphics(pGraphics, state1) removed the information blocks identified by both state1 and state2 from the stack.


SUB GDIP_Save2 (BYVAL hdc AS DWORD)

   LOCAL hStatus AS LONG
   LOCAL pGraphics AS DWORD
   LOCAL state1 AS LONG
   LOCAL state2 AS LONG
   LOCAL pRedPen AS DWORD
   LOCAL pGreenPen AS DWORD
   LOCAL pBluePen AS DWORD

   hStatus = GdipCreateFromHDC(hdc, pGraphics)

   hStatus = GdipRotateWorldTransform(pGraphics, 30, %MatrixOrderPrepend)
   hStatus = GdipSaveGraphics(pGraphics, state1)
   hStatus = GdipTranslateWorldTransform(pGraphics, 100, 0, %MatrixOrderAppend)
   hStatus = GdipSaveGraphics(pGraphics, state2)
   hStatus = GdipScaleWorldTransform(pGraphics, 1, 3, %MatrixOrderAppend)

   ' // Draw an ellipse.
   ' // Three transformations apply: rotate, then translate, then scale.
   hStatus = GdipCreatePen1(GDIP_ARGB(255, 255, 0, 0), 1, %UnitWorld, pRedPen)
   hStatus = GdipDrawEllipse(pGraphics, pRedPen, 0, 0, 100, 20)

   ' // Restore to state1 and draw the ellipse again.
   ' // Only the rotation transformation applies.
   hStatus = GdipRestoreGraphics(pGraphics, state1)
   hStatus = GdipCreatePen1(GDIP_ARGB(255, 0, 255, 0), 1, %UnitWorld, pGreenPen)
   hStatus = GdipDrawEllipse(pGraphics, pGreenPen, 0, 0, 100, 20)

   ' // The information block identified by state2 has been lost.
   ' // The following call to Restore has no effect because
   ' // GdipRestoreGraphics(pGraphics, state1) removed from the stack the
   ' // information blocks identified by state1 and state2.
   hStatus = GdipRestoreGraphics(pGraphics, state2)

   ' // The Graphics object is still in the state identified by state1.
   ' // The following code draws a blue ellipse on top of the previously
   ' // drawn green ellipse.
   hStatus = GdipCreatePen1(GDIP_ARGB(255, 0, 0, 255), 1, %UnitWorld, pBluePen)
   hStatus = GdipDrawEllipse(pGraphics, pBluePen, 0, 0, 100, 20)

   ' // Cleanup
   IF pBluePen THEN GdipDeletePen(pBluePen)
   IF pGreenPen THEN GdipDeletePen(pGreenPen)
   IF pRedPen THEN GdipDeletePen(pRedPen)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB