The following example creates a
GraphicsPath object and adds a closed figure to the
path. The code defines a warp transformation by specifying a source rectangle and an array of four destination points. The source rectangle and destination points are passed to the
GdipWarpPath function. The code draws the path twice: once before it has been warped and once after it has been warped.
C++
VOID WarpExample(HDC hdc)
{
Graphics graphics(hdc);
// Create a path.
PointF points[] ={
PointF(20.0f, 60.0f),
PointF(30.0f, 90.0f),
PointF(15.0f, 110.0f),
PointF(15.0f, 145.0f),
PointF(55.0f, 145.0f),
PointF(55.0f, 110.0f),
PointF(40.0f, 90.0f),
PointF(50.0f, 60.0f)};
GraphicsPath path;
path.AddLines(points, 8);
path.CloseFigure();
// Draw the path before applying a warp transformation.
Pen bluePen(Color(255, 0, 0, 255));
graphics.DrawPath(&bluePen, &path);
// Define a warp transformation, and warp the path.
RectF srcRect(10.0f, 50.0f, 50.0f, 100.0f);
PointF destPts[] = {
PointF(220.0f, 10.0f),
PointF(280.0f, 10.0f),
PointF(100.0f, 150.0f),
PointF(400.0f, 150.0f)};
path.Warp(destPts, 4, srcRect);
// Draw the warped path.
graphics.DrawPath(&bluePen, &path);
// Draw the source rectangle and the destination polygon.
Pen blackPen(Color(255, 0, 0, 0));
graphics.DrawRectangle(&blackPen, srcRect);
graphics.DrawLine(&blackPen, destPts[0], destPts[1]);
graphics.DrawLine(&blackPen, destPts[0], destPts[2]);
graphics.DrawLine(&blackPen, destPts[1], destPts[3]);
graphics.DrawLine(&blackPen, destPts[2], destPts[3]);
}
PowerBASIC
SUB GDIP_WarpPath (BYVAL hdc AS DWORD)
LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL pPath AS DWORD
LOCAL pBlackPen AS DWORD
LOCAL pBluePen AS DWORD
LOCAL srcRect AS RECTF
DIM pts(7) AS POINTF
DIM destPts(3) AS POINTF
hStatus = GdipCreateFromHDC(hdc, pGraphics)
' // Create a path.
hStatus = GdipCreatePath(%FillModeAlternate, pPath)
pts(0).x = 20 : pts(0).y = 60
pts(1).x = 30 : pts(1).y = 90
pts(2).x = 15 : pts(2).y = 110
pts(3).x = 15 : pts(3).y = 145
pts(4).x = 55 : pts(4).y = 145
pts(5).x = 55 : pts(5).y = 110
pts(6).x = 40 : pts(6).y = 90
pts(7).x = 50 : pts(7).y = 60
hStatus = GdipAddPathLine2(pPath, pts(0), 8)
hStatus = GdipClosePathFigure(pPath)
' // Draw the path before applying a warp transformation.
hStatus = GdipCreatePen1(GDIP_ARGB(255, 0, 0, 255), 1, %UnitWorld, pBluePen)
hStatus = GdipDrawPath(pGraphics, pBluePen, pPath)
' // Define a warp transformation, and warp the path.
srcRect.x = 10 : srcRect.y = 50 : srcRect.Width = 50 : srcRect.Height = 100
destPts(0).x = 220 : destPts(0).y = 10
destPts(1).x = 280 : destPts(1).y = 10
destPts(2).x = 100 : destPts(2).y = 150
destPts(3).x = 400 : destPts(3).y = 150
hStatus = GdipWarpPath(pPath, %NULL, destPts(0), 4, srcRect.x, srcRect.y, srcRect.Width, srcRect.Height, %WarpModePerspective, FlatnessDefault)
' // Draw the warped path.
hStatus = GdipDrawPath(pGraphics, pBluePen, pPath)
' // Draw the source rectangle and the destination polygon.
hStatus = GdipCreatePen1(GDIP_ARGB(255, 0, 0, 0), 1, %UnitWorld, pBlackPen)
hStatus = GdipDrawRectangle(pGraphics, pBlackPen, srcRect.x, srcRect.y, srcRect.Width, srcRect.Height)
hStatus = GdipDrawLine(pGraphics, pBlackPen, destpts(0).x, destPts(0).y, destPts(1).x, destPts(1).y)
hStatus = GdipDrawLine(pGraphics, pBlackPen, destpts(0).x, destPts(0).y, destPts(2).x, destPts(2).y)
hStatus = GdipDrawLine(pGraphics, pBlackPen, destpts(1).x, destPts(1).y, destPts(3).x, destPts(3).y)
hStatus = GdipDrawLine(pGraphics, pBlackPen, destpts(2).x, destPts(2).y, destPts(3).x, destPts(3).y)
' // Cleanup
IF pBluePen THEN GdipDeletePen(pBluePen)
IF pBlackPen THEN GdipDeletePen(pBlackPen)
IF pPath THEN GdipDeletePath(pPath)
IF pGraphics THEN GdipDeleteGraphics(pGraphics)
END SUB
(http://www.jose.it-berater.org/captures/GdipWarpPath.png)