• Welcome to Jose's Read Only Forum 2023.
 

GDI+: GdipSaveImageToStream

Started by José Roca, January 25, 2009, 01:54:34 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca



The following example creates two Image objects: one constructed from a JPEG file and one constructed from a PNG file. The code creates a compound file with two streams and saves the two images to those streams.


' ========================================================================================
' GdipSaveImageToStream
' ========================================================================================

' ========================================================================================
' The following example creates two Image objects: one constructed from a JPEG file and
' one constructed from a PNG file. The code creates a compound file with two streams and
' saves the two images to those streams.
' ========================================================================================

' SED_PBCC
#COMPILE EXE
#DIM ALL
#INCLUDE ONCE "GDIPLUS.INC"
#INCLUDE ONCE "GDIPUTILS.INC"

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

   LOCAL hr AS LONG
   LOCAL hStatus AS LONG
   LOCAL token AS DWORD
   LOCAL StartupInput AS GdiplusStartupInput
   LOCAL strFileName AS STRING
   LOCAL wszName AS STRING
   LOCAL pIStorage AS IStorage
   LOCAL pIStream1 AS IStream
   LOCAL pIStream2 AS IStream
   LOCAL pImage1 AS DWORD
   LOCAL pImage2 AS DWORD

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

   ' // Create two Image objects from existing files.
   strFileName = UCODE$("Iron.jpg")
   hStatus = GdipLoadImageFromFile(STRPTR(strFileName), pImage1)
   strFileName = UCODE$("House.png")
   hStatus = GdipLoadImageFromFile(STRPTR(strFileName), pImage2)

   ' // Create a compound file object, and get
   ' // a pointer to its IStorage interface.
   wszName = UCODE$("CompoundFile.cmp")
   hr = StgCreateDocFile(STRPTR(wszName), _
        %STGM_READWRITE OR %STGM_CREATE OR %STGM_SHARE_EXCLUSIVE, _
        %NULL, pIStorage)

   ' // Create a stream in the compound file.
   wszName = UCODE$("StreamImage1")
   hr = pIStorage.CreateStream(STRPTR(wszName), _
        %STGM_READWRITE OR %STGM_SHARE_EXCLUSIVE, _
        0, 0, pIStream1)

   ' // Create a second stream in the compound file.
   wszName = UCODE$("StreamImage2")
   hr = pIStorage.CreateStream(STRPTR(wszName), _
        %STGM_READWRITE OR %STGM_SHARE_EXCLUSIVE, _
        0, 0, pIStream2)

   ' // Get the class identifier for the JPEG encoder.
   LOCAL jpgClsid AS GUID
   jpgClsid = GUID$(GdiPlusGetEncoderClsid("image/jpeg"))

   ' // Get the class identifier for the PNG encoder.
   LOCAL pngClsid AS GUID
   pngClsid = GUID$(GdiPlusGetEncoderClsid("image/png"))

   ' // Save image1 as a stream in the compound file.
   hStatus = GdipSaveImageToStream(pImage1, pIStream1, jpgClsid, BYVAL %NULL)

   ' // Save image2 as a stream in the compound file.
   hStatus = GdipSaveImageToStream(pImage2, pIStream2, pngClsid, BYVAL %NULL)

   ' // Cleanup
   IF pImage1 THEN GdipDisposeImage(pImage1)
   IF pImage2 THEN GdipDisposeImage(pImage2)

   pIStream1 = NOTHING
   pIStream2 = NOTHING
   pIStorage = NOTHING

   ' // Shutdown GDI+
   GdiplusShutdown token

   WAITKEY$

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