Author Topic: gdi+LoadImageStream :)  (Read 3467 times)

0 Members and 1 Guest are viewing this topic.

Offline Frank Brübach

  • Full Member
  • ***
  • Posts: 109
  • User-Rate: +13/-5
gdi+LoadImageStream :)
« on: November 14, 2009, 12:12:18 AM »
hello all.

I have tried successfully to use three different pictures for saving/loading Image with gdi+. It was a good exercise for me to understand what a stream and a compound file is good for.

by the way: how I can rotate text font and make it transparent? any example here at the forum to study?

code one:
Code: [Select]
' ========================================================================================
' GdipSaveImageToStream example by frank brübach
' ========================================================================================

#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 pIStream3 AS IStream
   LOCAL pImage1 AS DWORD
   LOCAL pImage2 AS DWORD
   LOCAL pImage3 AS DWORD

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

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

   ' // 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)

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


   ' // 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"))

   ' // Get the class identifier for the PNG encoder.
   LOCAL pngClsid1 AS GUID
   pngClsid1 = 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)

 ' // Save image2 as a stream in the compound file.
   hStatus = GdipSaveImageToStream(pImage3, pIStream3, pngClsid1, BYVAL %NULL)

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

   pIStream1 = NOTHING
   pIStream2 = NOTHING
   pIStream3 = NOTHING
   pIStorage = NOTHING

   ' // Shutdown GDI+
   GdiplusShutdown token

   SLEEP 8000

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


code two:
Code: [Select]
' ========================================================================================
' GdipLoadImageFromStream example by Frank Brübach
' ========================================================================================

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


SUB GDIP_LoadImageFromStream (BYVAL hdc AS DWORD)

   LOCAL hr AS LONG
   LOCAL hStatus AS LONG
   LOCAL pGraphics AS DWORD
   LOCAL pImage1 AS DWORD
   LOCAL pImage2 AS DWORD
   LOCAL pImage3 AS DWORD
   LOCAL wszName AS STRING
   LOCAL pIStorage AS IStorage
   LOCAL pIStream1 AS IStream
   LOCAL pIStream2 AS IStream
   LOCAL pIStream3 AS IStream
   hStatus = GdipCreateFromHDC(hdc, pGraphics)
   IF hStatus THEN EXIT SUB

   ' // Open an existing compound file, and get a pointer
   ' // to its IStorage interface.
   wszName = UCODE$("CompoundFile.cmp")
   hr = StgOpenStorage(STRPTR(wszName), NOTHING, _
        %STGM_READ OR %STGM_SHARE_EXCLUSIVE, _
        0, %NULL, pIStorage)
   IF FAILED(hr) THEN
      MSGBOX "StgOpenStorage failure: " & HEX$(hr)
      GdipDeleteGraphics(pGraphics)
      EXIT SUB
   END IF

   ' // Get a pointer to the stream StreamImage1 in the compound file.
   wszName = UCODE$("StreamImage1")
   hr = pIStorage.OpenStream(STRPTR(wszName), 0, %STGM_READ OR %STGM_SHARE_EXCLUSIVE, 0, pIStream1)
   IF FAILED(hr) THEN
      MSGBOX "IStorage.OpenStream failure: " & HEX$(hr)
      GdipDeleteGraphics(pGraphics)
      EXIT SUB
   END IF

   ' // Get a pointer to the stream StreamImage2 in the compound file.
   wszName = UCODE$("StreamImage2")
   hr = pIStorage.OpenStream(STRPTR(wszName), 0, %STGM_READ OR %STGM_SHARE_EXCLUSIVE, 0, pIStream2)
   IF FAILED(hr) THEN
      MSGBOX "IStorage.OpenStream failure: " & HEX$(hr)
      GdipDeleteGraphics(pGraphics)
      EXIT SUB
   END IF

   ' // Get a pointer to the stream StreamImage2 in the compound file.
   wszName = UCODE$("StreamImage3")
   hr = pIStorage.OpenStream(STRPTR(wszName), 0, %STGM_READ OR %STGM_SHARE_EXCLUSIVE, 0, pIStream3)
   IF FAILED(hr) THEN
      MSGBOX "IStorage.OpenStream failure: " & HEX$(hr)
      GdipDeleteGraphics(pGraphics)
      EXIT SUB
   END IF


   ' // Construct a new Image object based on StreamImage1.
   hStatus = GdipLoadImageFromStream(pIStream1, pImage1)
   IF hStatus THEN
      GdipDeleteGraphics(pGraphics)
      EXIT SUB
   END IF

   ' // Draw the image
   hStatus = GdipDrawImage(pGraphics, pImage1, 10, 10)

   ' // Construct a new Image object based on StreamImage2.
   hStatus = GdipLoadImageFromStream(pIStream2, pImage2)
   IF hStatus THEN
      GdipDeleteGraphics(pGraphics)
      EXIT SUB
   END IF

   ' // Draw the image
   hStatus = GdipDrawImage(pGraphics, pImage2, 260, 80)

' // Construct a new Image object based on StreamImage3.
   hStatus = GdipLoadImageFromStream(pIStream3, pImage3)
   IF hStatus THEN
      GdipDeleteGraphics(pGraphics)
      EXIT SUB
   END IF

   ' // Draw the image
   hStatus = GdipDrawImage(pGraphics, pImage3, 60, 180)

   ' // Cleanup
   IF pImage1 THEN GdipDisposeImage(pImage1)
   IF pImage2 THEN GdipDisposeImage(pImage2)
   IF pImage3 THEN GdipDisposeImage(pImage3)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WINMAIN (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS ASCIIZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   LOCAL hr AS LONG
   LOCAL hWndMain AS DWORD
   LOCAL hCtl AS DWORD
   LOCAL hFont AS DWORD
   LOCAL wcex AS WndClassEx
   LOCAL szClassName AS ASCIIZ * 80
   LOCAL rc AS RECT
   LOCAL szCaption AS ASCIIZ * 255
   LOCAL nLeft AS LONG
   LOCAL nTop AS LONG
   LOCAL nWidth AS LONG
   LOCAL nHeight AS LONG
   LOCAL token AS DWORD
   LOCAL hDlg AS DWORD
   LOCAL StartupInput AS GdiplusStartupInput

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

   hFont = GetStockObject(%ANSI_VAR_FONT)

   ' Register the window class
   szClassName        = "MyClassName"
   wcex.cbSize        = SIZEOF(wcex)
   wcex.style         = %CS_HREDRAW OR %CS_VREDRAW
   wcex.lpfnWndProc   = CODEPTR(WndProc)
   wcex.cbClsExtra    = 0
   wcex.cbWndExtra    = 0
   wcex.hInstance     = hInstance
   wcex.hCursor       = LoadCursor (%NULL, BYVAL %IDC_ARROW)
   wcex.hbrBackground = GetStockObject(%WHITE_BRUSH)
   wcex.lpszMenuName  = %NULL
   wcex.lpszClassName = VARPTR(szClassName)
   wcex.hIcon         = LoadIcon (%NULL, BYVAL %IDI_APPLICATION) ' Sample, if resource icon: LoadIcon(hInst, "APPICON")
   wcex.hIconSm       = LoadIcon (%NULL, BYVAL %IDI_APPLICATION) ' Remember to set small icon too..
   RegisterClassEx wcex

   ' Window caption
   szCaption = "Frankos_GdipLoadImageFromStream ok :)"

   ' Retrieve the nSize of the working area
   SystemParametersInfo %SPI_GETWORKAREA, 0, BYVAL VARPTR(rc), 0

   ' Calculate the position and nSize of the window
   nWidth  = (((rc.nRight - rc.nLeft)) + 2) * 0.65   ' 55% of the client screen width
   nHeight = (((rc.nBottom - rc.nTop)) + 2) * 0.60   ' 50% of the client screen height
   nLeft   = ((rc.nRight - rc.nLeft) \ 2) - nWidth \ 2
   nTop    = ((rc.nBottom - rc.nTop) \ 2) - (nHeight \ 2)

   ' Create a window using the registered class
   hWndMain = CreateWindowEx(%WS_EX_CONTROLPARENT, _           ' extended style
                             szClassName, _                    ' window class name
                             szCaption, _                      ' window caption
                             %WS_OVERLAPPEDWINDOW OR _
                             %WS_CLIPCHILDREN, _               ' window style
                             nLeft, _                          ' initial x position
                             nTop, _                           ' initial y position
                             nWidth, _                         ' initial x nSize
                             nHeight, _                        ' initial y nSize
                             %NULL, _                          ' parent window handle
                             0, _                              ' window menu handle
                             hInstance, _                      ' program instance handle
                             BYVAL %NULL)                      ' creation parameters

   ' Show the window
   ShowWindow hWndMain, nCmdShow
   UpdateWindow hWndMain

   ' Message handler loop
   LOCAL Msg AS tagMsg
   WHILE GetMessage(Msg, %NULL, 0, 0)
      IF ISFALSE IsDialogMessage(hWndMain, Msg) THEN
         TranslateMessage Msg
         DispatchMessage Msg
      END IF
   WEND

   ' Shutdown GDI+
   GdiplusShutdown token

   FUNCTION = msg.wParam

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

SUB GDIP_DrawString (BYVAL hdc AS DWORD)

   LOCAL hStatus AS LONG
   LOCAL pGraphics AS DWORD
   LOCAL pFontFamily AS DWORD
   LOCAL pFont AS DWORD
   LOCAL pSolidBrush AS DWORD
   LOCAL strFontName AS STRING
   LOCAL strText AS STRING
   LOCAL rcf AS RECTF

   hStatus = GdipCreateFromHDC(hdc, pGraphics)

   ' // Create the font
   strFontName = UCODE$("Comic Sans MS")
   hStatus = GdipCreateFontFamilyFromName(STRPTR(strFontName), %NULL, pFontFamily)
   IF hStatus = %StatusOk AND pFontFamily <> %NULL THEN
      hStatus = GdipCreateFont(pFontFamily, 24, %FontStyleRegular, %UnitPoint, pFont)
      GdipDeleteFontFamily(pFontFamily)
   END IF

   ' // Create a solid brush
   hStatus = GdipCreateSolidFill(GDIP_ARGB(255, 255, 0, 128), pSolidBrush)

   ' // Draw a string
   rcf.x = 60.0! : rcf.y = 90.0!
   strText = UCODE$("..the next winter will come!")
   hStatus = GdipDrawString(pGraphics, STRPTR(strText), LEN(strText) \ 2, pFont, rcf, %NULL, pSolidBrush)

   ' // Cleanup
   IF pFont THEN GdipDeleteFont(pFont)
   IF pSolidBrush THEN GdipDeleteBrush(pSolidBrush)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB

' ========================================================================================
' Main Window procedure
' ========================================================================================
FUNCTION WndProc (BYVAL hWnd AS DWORD, BYVAL wMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   LOCAL hDC AS DWORD
   LOCAL ps AS PAINTSTRUCT

   SELECT CASE wMsg

      CASE %WM_COMMAND
         SELECT CASE LOWRD(wParam)
            CASE %IDCANCEL
               IF HIWRD(wParam) = %BN_CLICKED THEN
                  SendMessage hWnd, %WM_CLOSE, 0, 0
                  FUNCTION = 0
                  EXIT FUNCTION
               END IF
         END SELECT

      CASE %WM_SYSCOMMAND
         ' Capture this message and send a WM_CLOSE message
         IF (wParam AND &HFFF0) = %SC_CLOSE THEN
            SendMessage hWnd, %WM_CLOSE, 0, 0
            EXIT FUNCTION
         END IF

      CASE %WM_PAINT
         hDC = BeginPaint(hWnd, ps)
         GDIP_LoadImageFromStream hDC
         GDIP_DrawString hDC
         EndPaint(hWnd, ps)
        
      CASE %WM_DESTROY
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   FUNCTION = DefWindowProc(hWnd, wMsg, wParam, lParam)

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



good night, best regards, frank
(zip file attached)


Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2485
  • User-Rate: +204/-0
Re: gdi+LoadImageStream :)
« Reply #1 on: November 14, 2009, 01:17:32 AM »
 
Quote
by the way: how I can rotate text font and make it transparent? any example here at the forum to study?

In the following example:

http://www.jose.it-berater.org/smfforum/index.php?topic=1918.0

the text is rotated using GdipRotateWorldTransform.