• Welcome to Jose's Read Only Forum 2023.
 

GDI+: GdipGetRegionScans

Started by José Roca, June 26, 2008, 07:12:37 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca



The following example creates a region from a path and gets a set of rectangles that approximate the region. The code then draws each of the rectangles.

C++


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

   SolidBrush solidBrush(Color(255, 255, 0, 0));
   Pen pen(Color(255, 0, 0, 0));
   GraphicsPath path;
   Matrix matrix;
   RectF* rects = NULL;
   INT count = 0; 

   // Create a region from a path.
   path.AddEllipse(10, 10, 50, 300);
   Region pathRegion(&path);   
   graphics.FillRegion(&solidBrush, &pathRegion);

   // Get the rectangles.
   graphics.GetTransform(&matrix);
   count = pathRegion.GetRegionScansCount(&matrix);
   rects = (RectF*)malloc(count*sizeof(RectF));
   pathRegion.GetRegionScans(&matrix, rects, &count);
   
   // Draw the rectangles.
   for(INT j = 0; j < count; ++j)
      graphics.DrawRectangle(&pen, rects[j]);

   free(rects);
}


PowerBASIC


SUB GDIP_GetRegionScans (BYVAL hdc AS DWORD)

   LOCAL hStatus AS LONG
   LOCAL pGraphics AS DWORD
   LOCAL pPath AS DWORD
   LOCAL pSolidBrush AS DWORD
   LOCAl pPen AS DWORD
   LOCAL pPathRegion AS DWORD
   LOCAL pMatrix AS DWORD
   LOCAL count AS LONG
   LOCAL j AS LONG
   DIM   rects(0) AS RECTF

   hStatus = GdipCreateFromHDC(hdc, pGraphics)
   hStatus = GdipCreateSolidFill(GDIP_ARGB(255, 255, 0, 0), pSolidBrush)
   hStatus = GdipCreatePen1(GDIP_ARGB(255, 0, 0, 0), 1.0, %UnitWorld, pPen)
   hStatus = GdipCreatePath(%FillModeAlternate, pPath)
   hStatus = GdipCreateMatrix(pMatrix)

   ' // Create a region from a path.
   hStatus = GdipAddPathEllipseI(pPath, 10, 10, 50, 300)
   hStatus = GdipCreateRegionPath(pPath, pPathRegion)

   ' // Get the rectangles.
   hStatus = GdipGetWorldTransform(pGraphics, pMatrix)
   hStatus = GdipGetRegionScansCount(pPathRegion, count, pMatrix)
   REDIM rects(count - 1)
   hStatus = GdipGetRegionScans(pPathRegion, rects(0), count, pMatrix)

   ' // Draw the rectangles.
   FOR j = 0 TO count - 1
      hStatus = GdipDrawRectangle(pGraphics, pPen, rects(j).x, rects(j).y, rects(j).Width, rects(j).Height)
   NEXT

   ' // Cleanup
   IF pMatrix THEN GdipDeleteMatrix(pMatrix)
   IF pPen THEN GdipDeletePen(pPen)
   IF pPathRegion THEN GdipDeleteRegion(pPathRegion)
   IF pSolidBrush THEN GdipDeleteBrush(pSolidBrush)
   IF pPath THEN GdipDeletePath(pPath)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB


The following illustration shows the output of the preceding code.



José Roca

#1


The following example creates a region from a path and gets a set of rectangles that approximate the region. The code then draws each of the rectangles.

C++


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

   SolidBrush solidBrush(Color(255, 255, 0, 0));
   Pen pen(Color(255, 0, 0, 0));
   GraphicsPath path;
   Matrix matrix;
   Rect* rects = NULL;
   INT count = 0; 

   // Create a region from a path.
   path.AddEllipse(10, 10, 50, 300);
   Region pathRegion(&path);   
   graphics.FillRegion(&solidBrush, &pathRegion);

   // Get the rectangles.
   graphics.GetTransform(&matrix);
   count = pathRegion.GetRegionScansCount(&matrix);
   rects = (Rect*)malloc(count*sizeof(Rect));
   pathRegion.GetRegionScans(&matrix, rects, &count); 

   // Draw the rectangles.
   for(INT j = 0; j < count; ++j)
      graphics.DrawRectangle(&pen, rects[j]);

   free(rects);
}


PowerBASIC


SUB GDIP_GetRegionScansI (BYVAL hdc AS DWORD)

   LOCAL hStatus AS LONG
   LOCAL pGraphics AS DWORD
   LOCAL pPath AS DWORD
   LOCAL pSolidBrush AS DWORD
   LOCAl pPen AS DWORD
   LOCAL pPathRegion AS DWORD
   LOCAL pMatrix AS DWORD
   LOCAL count AS LONG
   LOCAL j AS LONG
   DIM   rects(0) AS RECTL

   hStatus = GdipCreateFromHDC(hdc, pGraphics)
   hStatus = GdipCreateSolidFill(GDIP_ARGB(255, 255, 0, 0), pSolidBrush)
   hStatus = GdipCreatePen1(GDIP_ARGB(255, 0, 0, 0), 1.0, %UnitWorld, pPen)
   hStatus = GdipCreatePath(%FillModeAlternate, pPath)
   hStatus = GdipCreateMatrix(pMatrix)

   ' // Create a region from a path.
   hStatus = GdipAddPathEllipseI(pPath, 10, 10, 50, 300)
   hStatus = GdipCreateRegionPath(pPath, pPathRegion)

   ' // Get the rectangles.
   hStatus = GdipGetWorldTransform(pGraphics, pMatrix)
   hStatus = GdipGetRegionScansCount(pPathRegion, count, pMatrix)
   REDIM rects(count - 1)
   hStatus = GdipGetRegionScansI(pPathRegion, rects(0), count, pMatrix)

   ' // Draw the rectangles.
   FOR j = 0 TO count - 1
      hStatus = GdipDrawRectangleI(pGraphics, pPen, rects(j).nLeft, rects(j).nTop, rects(j).nRight, rects(j).nBottom)
   NEXT

   ' // Cleanup
   IF pMatrix THEN GdipDeleteMatrix(pMatrix)
   IF pPen THEN GdipDeletePen(pPen)
   IF pPathRegion THEN GdipDeleteRegion(pPathRegion)
   IF pSolidBrush THEN GdipDeleteBrush(pSolidBrush)
   IF pPath THEN GdipDeletePath(pPath)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB


The following illustration shows the output of the preceding code.



José Roca

#2


The following example creates a region from a path and gets a set of rectangles that approximate the region. The code then draws each of the rectangles.

PowerBASIC


SUB GDIP_GetRegionScans (BYVAL hdc AS DWORD)

   LOCAL hStatus AS LONG
   LOCAL pGraphics AS DWORD
   LOCAL pPath AS DWORD
   LOCAL pSolidBrush AS DWORD
   LOCAl pPen AS DWORD
   LOCAL pPathRegion AS DWORD
   LOCAL pMatrix AS DWORD
   LOCAL count AS LONG
   DIM   rects(0) AS RECTF

   hStatus = GdipCreateFromHDC(hdc, pGraphics)
   hStatus = GdipCreateSolidFill(GDIP_ARGB(255, 255, 0, 0), pSolidBrush)
   hStatus = GdipCreatePen1(GDIP_ARGB(255, 0, 0, 0), 1.0, %UnitWorld, pPen)
   hStatus = GdipCreatePath(%FillModeAlternate, pPath)
   hStatus = GdipCreateMatrix(pMatrix)

   ' // Create a region from a path.
   hStatus = GdipAddPathEllipseI(pPath, 10, 10, 50, 300)
   hStatus = GdipCreateRegionPath(pPath, pPathRegion)

   ' // Get the rectangles.
   hStatus = GdipGetWorldTransform(pGraphics, pMatrix)
   hStatus = GdipGetRegionScansCount(pPathRegion, count, pMatrix)
   REDIM rects(count - 1)
   hStatus = GdipGetRegionScans(pPathRegion, rects(0), count, pMatrix)

   ' // Draw the rectangles.
   hStatus = GdipDrawRectangles(pGraphics, pPen, rects(0), count)

   ' // Cleanup
   IF pMatrix THEN GdipDeleteMatrix(pMatrix)
   IF pPen THEN GdipDeletePen(pPen)
   IF pPathRegion THEN GdipDeleteRegion(pPathRegion)
   IF pSolidBrush THEN GdipDeleteBrush(pSolidBrush)
   IF pPath THEN GdipDeletePath(pPath)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB


The following illustration shows the output of the preceding code.