• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

GDI+: GdipLoadImageFromStream

Started by José Roca, January 25, 2009, 01:51:26 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca



The following example creates two Image objects. Each Image object is based on a stream that is part of a compound file. For an example of creating a compound file, see GdipSaveImageToStream. The code calls StgOpenStorage to open the compound file and get a pointer to its IStorage interface. Then the code calls IStorage.OpenStream to get a pointer to an IStream interface that represents one of the streams in the compound file. The code constructs an Image object based on that IStream pointer and then calls GdipDrawImage to display the image on the screen. The code uses a similar process to construct an Image object based on a second stream that is part of the same compound file.


' ========================================================================================
' GdipLoadImageFromStream
' ========================================================================================

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

' ========================================================================================
' The following example creates two Image objects. Each Image object is based on a stream
' that is part of a compound file. For an example of creating a compound file, see
' GdipSaveImageToStream. The code calls StgOpenStorage to open the compound file and get a
' pointer to its IStorage interface. Then the code calls IStorage.OpenStream to get a
' pointer to an IStream interface that represents one of the streams in the compound file.
' The code constructs an Image object based on that IStream pointer and then calls
' GdripDrawImage to display the image on the screen. The code uses a similar process to
' construct an Image object based on a second stream that is part of the same compound file.
' ========================================================================================
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 wszName AS STRING
   LOCAL pIStorage AS IStorage
   LOCAL pIStream1 AS IStream
   LOCAL pIStream2 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

   ' // 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, 200, 10)

   ' // Cleanup
   IF pImage1 THEN GdipDisposeImage(pImage1)
   IF pImage2 THEN GdipDisposeImage(pImage2)
   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 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 = "GdipLoadImageFromStream"

   ' 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.55   ' 55% of the client screen width
   nHeight = (((rc.nBottom - rc.nTop)) + 2) * 0.50   ' 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
' ========================================================================================

' ========================================================================================
' 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
         EndPaint(hWnd, ps)

      CASE %WM_DESTROY
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

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

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