• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

GDI+: GdipImageSelectActiveFrame

Started by José Roca, June 23, 2008, 02:56:22 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca

 
Copying Individual Frames from a Multiple-Frame Image



The following example retrieves the individual frames from a multiple-frame TIFF file. The code constructs an Image object from the multiple-frame TIFF file. To retrieve the individual frames (pages), the code calls the GdipImageSelectActiveFrame function.

The first argument passed to the GdipImageSelectActiveFrame function is the address of the image. The second argument is the address of a globally unique identifier (GUID) that specifies the dimension in which the frames were previously added to the multiple-frame TIFF file. The third argument is the zero-based index of the desired page.

C++


GUID   pageGuid = FrameDimensionPage;
CLSID  encoderClsid;
Image  multi(L"Multiframe.tif");

// Get the CLSID of the PNG encoder.
GetEncoderClsid(L"image/png", &encoderClsid);

// Display and save the first page (index 0).
multi.SelectActiveFrame(&pageGuid, 0);
graphics.DrawImage(&multi, 10, 10);
multi.Save(L"Page0.png", &encoderClsid, NULL);

// Display and save the second page.
multi.SelectActiveFrame(&pageGuid, 1);
graphics.DrawImage(&multi, 200, 10);
multi.Save(L"Page1.png", &encoderClsid, NULL);

// Display and save the third page.
multi.SelectActiveFrame(&pageGuid, 2);
graphics.DrawImage(&multi, 10, 150);
multi.Save(L"Page2.png", &encoderClsid, NULL);

// Display and save the fourth page.
multi.SelectActiveFrame(&pageGuid, 3);
graphics.DrawImage(&multi, 200, 150);
multi.Save(L"Page3.png", &encoderClsid, NULL);


PowerBASIC

The main function relies on the helper function GdiPlusGetEncoderClsid to retrieve the guid of the TIFF encoder.


#COMPILE EXE
#DIM ALL
#INCLUDE "GDIPLUS.INC"
#INCLUDE "GDIPUTILS.INC"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL hStatus AS LONG
   LOCAL token AS DWORD
   LOCAL StartupInput AS GdiplusStartupInput
   LOCAL EncoderClsid AS GUID
   LOCAL pageGuid AS GUID
   LOCAL pMulti AS DWORD
   LOCAL strFileName AS STRING

   ' // Initialize GDI+
   StartupInput.GdiplusVersion = 1
   hStatus = GdiplusStartup(token, StartupInput, BYVAL %NULL)
   IF hStatus THEN
      PRINT "Error initializing GDI+"
      EXIT FUNCTION
   END IF

   ' // Create the image object.
   strFileName = UCODE$("Multiframe.tif")
   hStatus = GdipLoadImageFromFile(STRPTR(strFileName), pMulti)

   ' // Get the CLSID of the TIFF encoder.
   EncoderClsid = GUID$(GdiPlusGetEncoderClsid("image/tiff"))

   pageGuid = $FrameDimensionPage

   ' // Save the first page (index 0).
   hStatus = GdipImageSelectActiveFrame(pMulti, pageGuid, 0)
   strFileName = UCODE$("Page0.png")
   hStatus = GdipSaveImageToFile(pMulti, STRPTR(strFileName), EncoderClsid, BYVAL %NULL)
   IF hStatus = %StatusOk THEN PRINT "Page 0 saved successfully"

   ' // Save the second page (index 1).
   hStatus = GdipImageSelectActiveFrame(pMulti, pageGuid, 1)
   strFileName = UCODE$("Page1.png")
   hStatus = GdipSaveImageToFile(pMulti, STRPTR(strFileName), EncoderClsid, BYVAL %NULL)
   IF hStatus = %StatusOk THEN PRINT "Page 1 saved successfully"

   ' // Save the third page (index 2).
   hStatus = GdipImageSelectActiveFrame(pMulti, pageGuid, 2)
   strFileName = UCODE$("Page2.png")
   hStatus = GdipSaveImageToFile(pMulti, STRPTR(strFileName), EncoderClsid, BYVAL %NULL)
   IF hStatus = %StatusOk THEN PRINT "Page 2 saved successfully"

   ' // Cleanup
   IF pMulti THEN GdipDisposeImage(pMulti)

   ' // Shutdown GDI+
   GdiplusShutdown token

   WAITKEY$

END FUNCTION
' ========================================================================================