• Welcome to Jose's Read Only Forum 2023.
 

GDI+: GdipDrawImagePointsRect

Started by José Roca, June 23, 2008, 02:15:08 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca



The following example draws the original source image and then draws a portion of the image in a specified parallelogram.

C++


VOID Example_DrawImage4(HDC hdc)

{

   Graphics graphics(hdc);

   // Create an Image object.
   Image image(L"pattern.png");

   // Draw the original source image.
   graphics.DrawImage(&image, 10, 10);

   // Define the portion of the image to draw.
   REAL srcX = 70.0f;
   REAL srcY = 20.0f;
   REAL srcWidth = 100.0f;
   REAL srcHeight = 100.0f;

   // Create an array of Point objects that specify the destination of the cropped image.
   PointF destPoints[3] = {
   PointF(230.0f, 30.0f),
   PointF(350.0f, 50.0f),
   PointF(275.0f, 120.0f)};

   Point* pdestPoints = destPoints;

   // Create an ImageAttributes object that specifies a recoloring from red to blue.
   ImageAttributes remapAttributes;
   ColorMap redToBlue;
   redToBlue.oldColor = Color(255, 255, 0, 0);
   redToBlue.newColor = Color(255, 0, 0, 255);
   remapAttributes.SetRemapTable(1, &redToBlue);

   // Draw the cropped image.
   graphics.DrawImage(
   &image,
   pdestPoints,
   3,
   srcX,
   srcY,
   srcWidth,
   srcHeight,
   UnitPixel,
   &remapAttributes,
   NULL,
   NULL);
}


PowerBASIC


SUB GDIP_GdipDrawImagePointsRect (BYVAL hdc AS DWORD)

   LOCAL hStatus AS LONG
   LOCAL pGraphics AS DWORD
   LOCAL pImage AS DWORD
   LOCAL strFileName AS STRING
   LOCAL srcx AS SINGLE
   LOCAL srcy AS SINGLE
   LOCAL srcwidth AS SINGLE
   LOCAL srcheight AS SINGLE
   LOCAL pRemapAttributes AS DWORD
   LOCAL redToBlue AS GDIP_COLORMAP
   DIM   destPoints(2) AS POINTF

   hStatus = GdipCreateFromHDC(hdc, pGraphics)

   ' // Create the Image object
   strFileName = UCODE$("pattern.png")
   hStatus = GdipLoadImageFromFile(STRPTR(strFileName), pImage)

   ' // Draw the original source image.
   hStatus = GdipDrawImage(pGraphics, pImage, 10, 10)

   ' // Define the portion of the image to draw.
   srcX = 70.0!
   srcY = 20.0!
   srcwidth = 100.0!
   srcheight = 100.0!

   ' // Create an array of Point structures that specify the destination of the cropped image.
   destPoints(0).x = 230.0! : destPoints(0).y = 30.0!
   destPoints(1).x = 350.0! : destPoints(1).y = 50.0!
   destPoints(2).x = 275.0! : destPoints(2).y = 120.0!

   ' // Create an ImageAttributes object that specifies a recoloring from red to blue.
   hStatus = GdipCreateImageAttributes(pRemapAttributes)
   redToBlue.oldColor = GDIP_ARGB(255, 255, 0, 0)
   redToBlue.newColor = GDIP_ARGB(255, 0, 0, 255)
   hStatus = GdipSetImageAttributesRemapTable(pRemapAttributes, %ColorAdjustTypeDefault, %TRUE, 1, redToBlue)

   ' Draw the cropped image
   hStatus = GdipDrawImagePointsRect(pGraphics, pImage, destPoints(0), 3, srcx, srcy, _
             srcwidth, srcheight, %UnitPixel, pRemapAttributes, %NULL, %NULL)

   ' // Cleanup
   IF pRemapAttributes THEN GdipDisposeImageAttributes(pRemapAttributes)
   IF pImage THEN GdipDisposeImage(pImage)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB