Announcement

Collapse
No announcement yet.

Read/Using/Writing Image Formats Other Than BMP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Read/Using/Writing Image Formats Other Than BMP

    Barry's post made me think about what my minimum graphic needs have been in the last few years.

    Mostly, I've just read JPG/BMP/GIF files, done something minor with them (display, rotate, shrink, expand, ..), then write the changed image back out to a file format of my choice.

    With PowerBASIC I was surprised to find no built-in read/write features except for BMP formats.

    I figured that most programmers had at least my own basic image handling needs and that there were probably a few well-known API to do the trick, else PowerBASIC users would have revolted.

    The GRAPHIC statements seem completely adequate for my modest image manipulation needs. It's getting the image formats in and out where a non-PowerBASIC solution seems to be required.

    Despite the zillion graphic examples in the forums, I've been unsuccessful in pulling out what I thought should be simple code for:

    1. Reading a JPG/GIF/PNG file and getting a handle to a memory bitmap that I could use in PowerBASIC GRAPHIC statements.

    2. Write an existing GRAPHIC memory bitmap to JPG/GIF/PNG files.

    I think the problem is that I was expecting simplicity - a handful of lines of code. But what I'm finding is that I'm having to filter through far more lines of code that I expected. The "found a solution in 10 minutes" hasn't happened for me.

    So, off I go to look in more detail, learn more that I thought I'd have to. I will, of course, be the better for it - and it's a topic that I'm interested in. I'll post whatever I find/come up with that seems to meet my minimal needs.

    But have I missed some simple (probably GDI ?) code example that everyone else must already know about - except me?

    P.S. - I've seen 3rd party libraries, such as freeimage discussed, but like one of Jose's comments, it seems a shame not to use the built-in Windows API unless there's a fundamental issue. In my case, where I have very modest needs, it seems like a GDI solution would be sufficient and that 3rd party DLLs would be an unnecessary burden.

  • #2
    Use GDI+.

    There are hundreds of examples in my forum (the zip's also include DDT examples, both using a graphic control or not).

    And I have documented the whole GDI+ Flat API (MSDN only documents the C++ wrapper classes).

    GDI+ Flat API Reference: http://www.jose.it-berater.org/gdiplus/iframe/index.htm
    Last edited by José Roca; 16 Nov 2009, 12:55 PM.
    Forum: http://www.jose.it-berater.org/smfforum/index.php

    Comment


    • #3
      Originally posted by José Roca View Post
      Use GDI+.

      There are hundreds of examples in my forum (the zip's also include DDT examples, both using a graphic control or not).

      And I have documented the whole GDI+ Flat API (MSDN only documents the C++ wrapper classes).

      GDI+ Flat API Reference: http://www.jose.it-berater.org/gdiplus/iframe/index.htm
      I've seen that and it looks like you've done a LOT of work. The problem for me is that getting into GDI+ is a major undertaking. I retired about 15 years ago and did practically no programming till about 2 years ago. Since then I've just toyed around with simple stuff and that's all I really feel up to.

      I can do much of what I want to do with just BMP files but the people I share my little games with will be more likely to find JPG graphics to use with them.

      I've started learning Firefly and that lets me deal with JPG, etc. but it's also a big learning curve since I have almost no SDK programming experience. I like the simplicity of the PB editor and DDT.

      Maybe if I do learn Firefly I'll decide I like it well enough. It seems to have everything I want. Time will tell.

      Barry

      Comment


      • #4
        I like the simplicity of the PB editor and DDT.
        But it only supports bitmaps. Therefore, your choice is to use bitmaps or bite the bullet and learn GDI+. You can also request to PowerBASIC, Inc., support for other formats and wait.
        Forum: http://www.jose.it-berater.org/smfforum/index.php

        Comment


        • #5
          You might consider egrid32. I requested native support for other than BMP a few years ago.
          I think Michael M has some code to support JPG with Don Dickinson's ddoc. It would be
          nice to just drop an image into PbForms and later have print/preview.
          PowerBASIC could have another product like PowerTools.
          We could just add a DLL to our projects.
          Last edited by Mike Doty; 16 Nov 2009, 05:39 PM.

          Comment


          • #6
            Gary,
            just use the wraper function here:
            http://www.powerbasic.com/support/pb...ad.php?t=23625
            You don't need to understand GDI+ to use them but they'll convert other formats to BMP so you can use them in your programs and then convert them back again afterwards.

            Paul.

            Comment


            • #7
              Originally posted by José Roca View Post
              But it only supports bitmaps. Therefore, your choice is to use bitmaps or bite the bullet and learn GDI+. You can also request to PowerBASIC, Inc., support for other formats and wait.
              I did start reading about GDI+ on your site and on MSDN a few weeks ago and it's just more learning than I want to do. I'm just trying to play around with this stuff and I don't want any big projects, learning or doing. I enjoy learning little stuff and these days that's enough for me.

              In the other thread Peter Jinks just posted the code to get other formats into DDT so it looks like this might be solved now. It uses FreeImage, which means I need a DLL, which I've been trying to avoid, but that's a minor issue.

              Thanks,
              Barry

              Comment


              • #8
                Gary, a simple example http://www.powerbasic.com/support/pb...highlight=gdip

                Comment


                • #9
                  I did start reading about GDI+ on your site and on MSDN a few weeks ago and it's just more learning than I want to do.
                  Looks like the typical case of don't seeing the forest for the trees. GDI+ is huge (more than 600 functions), but you don't need to learn everything at once.

                  Here is an small example using a DDT dialog and a DDT graphic control. GDI+ is used to load and draw an image in the graphic control. After that, you can use both GRAPHIC statements and GDI+ functions. Frankly, I don't see the difficulty.

                  Code:
                  ' ========================================================================================
                  ' GdipDrawImage
                  ' ========================================================================================
                  
                  ' SED_PBWIN
                  #COMPILE EXE
                  #DIM ALL
                  #INCLUDE "GDIPLUS.INC"
                  
                  %IDC_GRAPHIC  = 101
                  
                  ' ========================================================================================
                  ' Main
                  ' ========================================================================================
                  FUNCTION PBMAIN
                  
                     LOCAL hr AS LONG
                     LOCAL hDlg AS DWORD
                     LOCAL hdc AS DWORD
                     LOCAL token AS DWORD
                     LOCAL StartupInput AS GdiplusStartupInput
                     LOCAL hStatus AS LONG
                     LOCAL pGraphics AS DWORD
                     LOCAL pImage AS DWORD
                     LOCAL strFileName AS STRING
                  
                     ' Initialize GDI+
                     StartupInput.GdiplusVersion = 1
                     hr = GdiplusStartup(token, StartupInput, BYVAL %NULL)
                     IF hr THEN
                        MSGBOX "Error initializing GDI+"
                        EXIT FUNCTION
                     END IF
                  
                     ' Create a new dialog
                     DIALOG NEW PIXELS, 0, "GdipDrawImage", , , 420, 250, %WS_SYSMENU TO hDlg
                  
                     ' Add a graphic control
                     CONTROL ADD GRAPHIC, hDlg, %IDC_GRAPHIC,"", 0, 0, 420, 250
                     ' Select the drawing target
                     GRAPHIC ATTACH hDlg, %IDC_GRAPHIC
                     ' Set the foreground and background color
                     GRAPHIC COLOR %BLACK, %WHITE
                     ' Clear the entire selected graphic window
                     GRAPHIC CLEAR
                     ' Retrieve the handle of the device context
                     GRAPHIC GET DC TO hdc
                  
                     ' Create the Image object
                     strFileName = UCODE$("climber.jpg")   ' <-- change me
                     hStatus = GdipLoadImageFromFile(STRPTR(strFileName), pImage)
                     ' Create the Graphic object
                     hStatus = GdipCreateFromHDC(hdc, pGraphics)
                     ' Draw the image
                     hStatus = GdipDrawImage(pGraphics, pImage, 10, 10)
                  
                     ' *** At this point you can use both GRAPHIC statements and GDI+ functions ***
                  
                     DIALOG SHOW MODAL hDlg, CALL DlgProc
                  
                     ' Cleanup
                     IF pImage THEN GdipDisposeImage(pImage)
                     IF pGraphics THEN GdipDeleteGraphics(pGraphics)
                  
                     ' Shutdown GDI+
                     GdiplusShutdown token
                  
                  END FUNCTION
                  ' ========================================================================================
                  
                  ' ========================================================================================
                  ' Main Dialog procedure
                  ' ========================================================================================
                  CALLBACK FUNCTION DlgProc() AS LONG
                  
                     SELECT CASE CBMSG
                  
                        CASE %WM_COMMAND
                           SELECT CASE CBCTL
                              CASE %IDCANCEL
                                 IF CBCTLMSG = %BN_CLICKED THEN DIALOG END CBHNDL, 0
                           END SELECT
                  
                     END SELECT
                  
                  END FUNCTION
                  ' ========================================================================================
                  Note: It uses my Windows API Headers, available in my forum.
                  Forum: http://www.jose.it-berater.org/smfforum/index.php

                  Comment


                  • #10
                    And if what you want is to use GDI+ just to load the picture and later use only GRAPHIC statements, then do the cleanup and shutdown after loading the image:

                    Code:
                    ' ========================================================================================
                    ' GdipDrawImage
                    ' ========================================================================================
                    
                    ' SED_PBWIN
                    #COMPILE EXE
                    #DIM ALL
                    #INCLUDE "GDIPLUS.INC"
                    
                    %IDC_GRAPHIC  = 101
                    
                    ' ========================================================================================
                    ' Main
                    ' ========================================================================================
                    FUNCTION PBMAIN
                    
                       LOCAL hr AS LONG
                       LOCAL hDlg AS DWORD
                       LOCAL hdc AS DWORD
                       LOCAL token AS DWORD
                       LOCAL StartupInput AS GdiplusStartupInput
                       LOCAL hStatus AS LONG
                       LOCAL pGraphics AS DWORD
                       LOCAL pImage AS DWORD
                       LOCAL strFileName AS STRING
                    
                       ' Initialize GDI+
                       StartupInput.GdiplusVersion = 1
                       hr = GdiplusStartup(token, StartupInput, BYVAL %NULL)
                       IF hr THEN
                          MSGBOX "Error initializing GDI+"
                          EXIT FUNCTION
                       END IF
                    
                       ' Create a new dialog
                       DIALOG NEW PIXELS, 0, "GdipDrawImage", , , 420, 250, %WS_SYSMENU TO hDlg
                    
                       ' Add a graphic control
                       CONTROL ADD GRAPHIC, hDlg, %IDC_GRAPHIC,"", 0, 0, 420, 250
                       ' Select the drawing target
                       GRAPHIC ATTACH hDlg, %IDC_GRAPHIC
                       ' Set the foreground and background color
                       GRAPHIC COLOR %BLACK, %WHITE
                       ' Clear the entire selected graphic window
                       GRAPHIC CLEAR
                       ' Retrieve the handle of the device context
                       GRAPHIC GET DC TO hdc
                    
                       ' Create the Image object
                       strFileName = UCODE$("climber.jpg")   ' <-- change me
                       hStatus = GdipLoadImageFromFile(STRPTR(strFileName), pImage)
                       ' Create the Graphic object
                       hStatus = GdipCreateFromHDC(hdc, pGraphics)
                       ' Draw the image
                       hStatus = GdipDrawImage(pGraphics, pImage, 10, 10)
                    
                       ' Cleanup
                       IF pImage THEN GdipDisposeImage(pImage)
                       IF pGraphics THEN GdipDeleteGraphics(pGraphics)
                    
                       ' Shutdown GDI+
                       GdiplusShutdown token
                    
                       ' *** At this point you can use GRAPHIC statements but not GDI+ functions ***
                    
                       DIALOG SHOW MODAL hDlg, CALL DlgProc
                    
                    
                    END FUNCTION
                    ' ========================================================================================
                    
                    ' ========================================================================================
                    ' Main Dialog procedure
                    ' ========================================================================================
                    CALLBACK FUNCTION DlgProc() AS LONG
                    
                       SELECT CASE CBMSG
                    
                          CASE %WM_COMMAND
                             SELECT CASE CBCTL
                                CASE %IDCANCEL
                                   IF CBCTLMSG = %BN_CLICKED THEN DIALOG END CBHNDL, 0
                             END SELECT
                    
                       END SELECT
                    
                    END FUNCTION
                    ' ========================================================================================
                    Forum: http://www.jose.it-berater.org/smfforum/index.php

                    Comment


                    • #11
                      Or do the cleanup but not the shutdown, and at any time you can create a graphics object retrieving the handle of the device context of the graphic control with GRAPHIC GET DC TO hdc and then calling GdipCreateFromHDC(hdc, pGraphics).
                      Forum: http://www.jose.it-berater.org/smfforum/index.php

                      Comment


                      • #12
                        Jose,

                        I'm about to go to bed so I'll take a look at the code you posted in the morning.

                        I realize that GDI+ probably doesn't have to be learned all at once but after a day of looking it over I still didn't have any idea how to find what I was looking for. It was obviously going to take more effort than I wanted to put into it.

                        I used to learn things pretty easily. Now I'm 69 years old and I've been away from programming for a long time since retirement and things just don't come easily to me anymore. A typical programming session when I was younger was likely to be between 4 and 16 hours at a stretch, leaving me exhausted and feeling good about it. Now about 20 or 30 minutes and I'm wiped out. I take an hour or two break and come back for more.

                        I can't really dig very deep anymore. What I can do is enjoy passing time by playing around with stuff that's not too far out of my reach.

                        I've had this same discussion in years past; not with older people but with people working on projects with me who didn't want to put in that extra effort and couldn't figure out why they weren't getting there, so I think I know where you're coming from and how you feel. And maybe now I also know a little more about how they feel.

                        Anyway, I appreciate the code. I'll spend some time with it. I have the code from the other thread on this topic that I'm already playing with and it's working great using FreeImage. If I can do this without a DLL using GDI+ that'll be even better.
                        ------

                        I just took a look at those and ran them and they're a bit simpler and don't require a DLL and I thank you.

                        These might make a good addition to your site.

                        Barry


                        Barry

                        Comment


                        • #13
                          Originally posted by José Roca View Post
                          And if what you want is to use GDI+ just to load the picture and later use only GRAPHIC statements, then do the cleanup and shutdown after loading the image:
                          This one is almost perfect. The one other thing I was trying to do was to put it into a graphic bitmap so I can then use it in various ways and places in a graphic control. I just changed your code so I can do that and it was simple. I think this is what I want to use.

                          Again, thanks. And now can I please get a little sleep?

                          Barry

                          Comment


                          • #14
                            And I, on the other hand, am just waking up!

                            Jose, thanks for the posts.

                            I'm a bit opposite of Barry in some ways - I'm almost 60, but the idea of spending hours learning new material is still one of my favorite things to do. I seem to still enjoy the hours-long sessions.

                            In the case of GDI, I've not found the time to work through it in detail - although today is the day (I hope). Well, except for working on a paid project, taking my daughter to the airport, tennis at 3pm and dinner with the wife - then I'll spend more time on the GDI/GDI+ documentation and forum examples.

                            Almost as an aside, I had an interesting revelation last night - discovering that I've mostly stayed away from examples which use INCLUDEs. I think that the hundreds of INCLUDE lines have somewhow made me think that although the example might work, that I wouldn't truly understand what was going on because I wasn't seeing the lines that were being pulled in - sometimes perhaps only a few lines. Somehow that translated into thinking that the example wasn't quite what I wanted.

                            Don't ask me where that attitude comes from - I was just able to articulate it myself last night.

                            Even as I had the thought, I remembered there's a tool floating around "IncLean", I think, that would extract the lines from the INCLUDE. I should give it a try, using it on posts to clarify the missing/unstated lines. I'll bet in many cases the extra line count would be very low and I'd certainly enjoy seeing just how much of the INCLUDE code actually gets used in most examples.

                            Okay, that's my stray thought ...

                            Anyway, I decided this was the week to dedicate time to GDI and GDI+. Even though I bill myself as a fan of minimalist code, I don't mind spending hours to generate 5 lines of compact code!

                            I'm sure I'll have more questions and thanks again for the posts.

                            Comment


                            • #15
                              Almost as an aside, I had an interesting revelation last night - discovering that I've mostly stayed away from examples which use INCLUDEs. I think that the hundreds of INCLUDE lines have somewhow made me think that although the example might work, that I wouldn't truly understand what was going on because I wasn't seeing the lines that were being pulled in - sometimes perhaps only a few lines. Somehow that translated into thinking that the example wasn't quite what I wanted.
                              Putting everything in a post (constants, declares, code, error checking, etc.) makes it to look complex. I always try to make the examples short and clean. Then add you all the error checking code you wish.
                              Forum: http://www.jose.it-berater.org/smfforum/index.php

                              Comment


                              • #16
                                Jose,
                                Thanks for the code. The idea of loading a JPG/GIF/PNG into a control so that I can use GRAPHIC statements is exactly what I was requesting.

                                However, using your code as a guide, I created a function that I thought I could call at any time. But when I run an application the function only works if I load the images before I show the dialog.

                                Is there a limitation to the code example you gave, such that after the dialog is shown the images cannot be loaded? Or perhaps I just haven't applied the code correctly into a Function.

                                Here's the function, followed by the compilable example. In this version, I assume that a Graphic Control is the target.

                                Code:
                                Function LoadImage_GDIP(sFileName As String, targetID as Long) As Long
                                   Local hr As LONG, token AS DWord, hDC As DWord
                                   Local hStatus As LONG, pGraphics AS DWord, pImage As DWord
                                   Local StartupInput AS GdiplusStartupInput
                                
                                   ' Initialize GDI+
                                   StartupInput.GdiplusVersion = 1
                                   hr = GdiplusStartup(token, StartupInput, BYVAL %NULL)
                                   If hr Then Function = 0 : Exit Function   'return 0 if fails
                                
                                   Graphic Attach hDlg, targetID             ' Select the drawing target
                                   Graphic Get DC TO hdc                     ' Retrieve the handle of the device context
                                   Graphic Color %BLACK, %WHITE              ' Set the foreground and background color
                                   Graphic Clear                             ' Clear the entire selected graphic window
                                
                                   sFileName = UCode$(sFileName)
                                '  hStatus = GdipLoadImageFromFile(StrPTR(strFileName), pImage) ' Create the Image object
                                   hStatus = GdipLoadImageFromFile(sFileName, pImage)           ' Create the Image object
                                   hStatus = GdipCreateFromHDC(hDC, pGraphics)                  ' Create the Graphic object
                                   hStatus = GdipDrawImage(pGraphics, pImage, 0, 0)             ' Draw the image
                                
                                   Graphic Copy pGraphics, 0                                           ' put image in target
                                
                                   If pImage Then GdipDisposeImage(pImage)                      ' Cleanup
                                   If pGraphics Then GdipDeleteGraphics(pGraphics)              ' Cleanup
                                '   GdiplusShutdown token                                        ' Shutdown GDI+
                                   Function = 1                                                 ' Success
                                End Function
                                And here's the compilable example. Note that if I call the image loading function before the dialog is shown, all works well. But if I call the function after the dialog is shown (by pressing one of the buttons), the function does not show the image.

                                You'll have to use your own image. Also, note that I'm using the gdiplus.inc file that Patrice posted. I haven't yet done a wholesale replacement of the PowerBASIC includes with the ones I downloaded from you site.

                                Code:
                                'Compilable Example:
                                'Credit: Jose Roca
                                #Compile Exe
                                #Dim All
                                #Include "Win32API.inc"
                                #Include "includes\GDIPLUS.INC"
                                Global hDlg as DWord
                                %IDC_GRAPHIC1  = 101
                                %IDC_GRAPHIC2  = 102
                                %IDC_GRAPHIC3  = 103
                                
                                Function PBMain() As Long
                                   Dialog New Pixels, 0, "GDI+ Load Image Test",300,300,500,200, %WS_OverlappedWindow To hDlg
                                   Control Add Button, hDlg, 110,"GIF", 10,10,60,20
                                   Control Add Button, hDlg, 120,"BMP", 100,10,60,20
                                   Control Add Button, hDlg, 130,"JPG", 190,10,60,20
                                   Control Add Graphic, hDlg, %IDC_GRAPHIC1, "",  20, 40, 125, 125   ' Add a graphic control
                                   Control Add Graphic, hDlg, %IDC_GRAPHIC2, "", 170, 40, 125, 125   ' Add a graphic control
                                   Control Add Graphic, hDlg, %IDC_GRAPHIC3, "", 320, 40, 125, 125   ' Add a graphic control
                                
                                   LoadImage_GDIP "garyface.gif", %IDC_Graphic1
                                   LoadImage_GDIP "cowgirl.bmp", %IDC_Graphic2
                                   LoadImage_GDIP "daniel.jpg", %IDC_Graphic3
                                
                                   Dialog Show Modal hDlg Call DlgProc
                                End Function
                                CallBack Function DlgProc() As Long
                                   If CB.Msg = %WM_Command Then
                                      Select Case CB.Ctl
                                         Case 110 : LoadImage_GDIP "garyface.gif", %IDC_Graphic1
                                         Case 120 : LoadImage_GDIP "cowgirl.bmp", %IDC_Graphic2
                                         Case 130 : LoadImage_GDIP "daniel.jpg", %IDC_Graphic3
                                      End Select
                                   End If
                                End Function
                                
                                Function LoadImage_GDIP(sFileName As String, targetID as Long) As Long
                                   Local hr As LONG, token AS DWord, hDC As DWord
                                   Local hStatus As LONG, pGraphics AS DWord, pImage As DWord
                                   Local StartupInput AS GdiplusStartupInput
                                
                                   ' Initialize GDI+
                                   StartupInput.GdiplusVersion = 1
                                   hr = GdiplusStartup(token, StartupInput, BYVAL %NULL)
                                   If hr Then Function = 0 : Exit Function   'return 0 if fails
                                
                                   Graphic Attach hDlg, targetID             ' Select the drawing target
                                   Graphic Get DC TO hdc                     ' Retrieve the handle of the device context
                                   Graphic Color %BLACK, %WHITE              ' Set the foreground and background color
                                   Graphic Clear                             ' Clear the entire selected graphic window
                                
                                   sFileName = UCode$(sFileName)
                                '  hStatus = GdipLoadImageFromFile(StrPTR(strFileName), pImage) ' Create the Image object
                                   hStatus = GdipLoadImageFromFile(sFileName, pImage)           ' Create the Image object
                                   hStatus = GdipCreateFromHDC(hDC, pGraphics)                  ' Create the Graphic object
                                   hStatus = GdipDrawImage(pGraphics, pImage, 0, 0)             ' Draw the image
                                
                                   Graphic Copy pGraphics, 0                                           ' put image in target
                                
                                   If pImage Then GdipDisposeImage(pImage)                      ' Cleanup
                                   If pGraphics Then GdipDeleteGraphics(pGraphics)              ' Cleanup
                                   GdiplusShutdown token                                        ' Shutdown GDI+
                                   Function = 1                                                 ' Success
                                End Function
                                
                                'gbs_00413
                                Last edited by Gary Beene; 19 Nov 2009, 12:14 AM.

                                Comment


                                • #17
                                  1. Remove Graphic Copy pGraphics, targetID. pGraphics is not an handle suitable to be used with GRAPHIC DDT statements. Besides isn't needed, since GdipDrawImage is who draws the image.

                                  2. Redraw the control with CONTROL REDRAW.
                                  Last edited by José Roca; 19 Nov 2009, 12:21 AM.
                                  Forum: http://www.jose.it-berater.org/smfforum/index.php

                                  Comment


                                  • #18
                                    Jose,

                                    I didn't catch that the code put the image into the graphic target, so I added the Graphic Copy. The code worked with that line in it, but only by luck of the draw. I've removed it.

                                    The Control Redraw was the key point that I didn't think of. The fact that the image displayed on startup led me to thinking something not-so-trivial was at fault. Once that line was added, all was well.

                                    BTW, did you notice that I had to replace STRPTR (as your original code listed) with just the string variable in this line:

                                    Code:
                                    '  hStatus = GdipLoadImageFromFile(StrPTR(strFileName), pImage) ' Create the Image object
                                    On compile, I got a message saying a string was needed so when I placed the string variable in the call, the compile error went away and the code worked.


                                    Thanks very much, Jose! The resulting Function will be very useful to me.

                                    Here's the corrected code, which seems to work just great.

                                    Code:
                                    'Compilable Example:
                                    'Credit: Jose Roca
                                    #Compile Exe
                                    #Dim All
                                    #Include "Win32API.inc"
                                    #Include "includes\GDIPLUS.INC"
                                    Global hDlg as DWord
                                    %IDC_GRAPHIC1  = 101
                                    %IDC_GRAPHIC2  = 102
                                    %IDC_GRAPHIC3  = 103
                                    
                                    Function PBMain() As Long
                                       Dialog New Pixels, 0, "GDI+ Load Image Test",300,300,500,200, %WS_OverlappedWindow To hDlg
                                       Control Add Button, hDlg, 110,"GIF", 10,10,60,20
                                       Control Add Button, hDlg, 120,"BMP", 100,10,60,20
                                       Control Add Button, hDlg, 130,"JPG", 190,10,60,20
                                       Control Add Graphic, hDlg, %IDC_GRAPHIC1, "",  20, 40, 125, 125   ' Add a graphic control
                                       Control Add Graphic, hDlg, %IDC_GRAPHIC2, "", 170, 40, 125, 125   ' Add a graphic control
                                       Control Add Graphic, hDlg, %IDC_GRAPHIC3, "", 320, 40, 125, 125   ' Add a graphic control
                                       Dialog Show Modal hDlg Call DlgProc
                                    End Function
                                    CallBack Function DlgProc() As Long
                                       If CB.Msg = %WM_Command Then
                                          Select Case CB.Ctl
                                             Case 110
                                                 LoadImage_GDIP "garyface.gif", %IDC_Graphic1
                                                 Control Redraw hDlg, %IDC_Graphic1
                                             Case 120
                                                 LoadImage_GDIP "cowgirl.bmp", %IDC_Graphic2
                                                 Control Redraw hDlg, %IDC_Graphic2
                                             Case 130
                                                 LoadImage_GDIP "daniel.jpg", %IDC_Graphic3
                                                 Control Redraw hDlg, %IDC_Graphic3
                                          End Select
                                       End If
                                    End Function
                                    
                                    Function LoadImage_GDIP(sFileName As String, targetID as Long) As Long
                                       Local hr As LONG, token AS DWord, hDC As DWord
                                       Local hStatus As LONG, pGraphics AS DWord, pImage As DWord
                                       Local StartupInput AS GdiplusStartupInput
                                    
                                       ' Initialize GDI+
                                       StartupInput.GdiplusVersion = 1
                                       hr = GdiplusStartup(token, StartupInput, BYVAL %NULL)
                                       If hr Then Function = 0 : Exit Function   'return 0 if fails
                                    
                                       Graphic Attach hDlg, targetID             ' Select the drawing target
                                       Graphic Get DC TO hdc                     ' Retrieve the handle of the device context
                                       Graphic Color %BLACK, %WHITE              ' Set the foreground and background color
                                       Graphic Clear                             ' Clear the entire selected graphic window
                                    
                                       sFileName = UCode$(sFileName)
                                       hStatus = GdipLoadImageFromFile(sFileName, pImage)           ' Create the Image object
                                       hStatus = GdipCreateFromHDC(hDC, pGraphics)                  ' Create the Graphic object
                                       hStatus = GdipDrawImage(pGraphics, pImage, 0, 0)             ' Draw the image
                                    
                                       If pImage Then GdipDisposeImage(pImage)                      ' Cleanup
                                       If pGraphics Then GdipDeleteGraphics(pGraphics)              ' Cleanup
                                       GdiplusShutdown token                                        ' Shutdown GDI+
                                    
                                       Function = 1                                                 ' Success
                                    End Function
                                    
                                    'gbs_00413
                                    I'll post this in the Source Code forum, with credit given to you, to make it easier for other folks to find.
                                    Last edited by Gary Beene; 19 Nov 2009, 12:33 AM.

                                    Comment


                                    • #19
                                      BTW, did you notice that I had to replace STRPTR (as your original code listed) with just the string variable in this line:
                                      That is because the parameter is declared as BYVAL STRING in Patrice's translation and as BYVAL DWORD in my translation. I have used BYVAL DWORD for consistency with the thousands of functions and methods in my Windows API headers that have unicode string parameters.
                                      Forum: http://www.jose.it-berater.org/smfforum/index.php

                                      Comment


                                      • #20
                                        Jose,
                                        Thank you for the clarification.

                                        Question - does the GDI+ determine the size of the image (from the file) and create a full-sized image/graphic object accordingly?

                                        I assume that the GDI+ puts as much of the image (from the file) into the target hDC as will fit, so it is up to me to make my Graphic Target large enough to fit the full image available from the file.

                                        ... and BTW, I tested the code using a memory bitmap instead of a graphic control and it also worked just fine.

                                        Life is good!
                                        Last edited by Gary Beene; 19 Nov 2009, 01:21 AM.

                                        Comment

                                        Working...
                                        X