Announcement

Collapse
No announcement yet.

BMP Image resize display problem

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

  • BMP Image resize display problem

    I've compiled this example code snip from Gary Beene

    https://www.garybeene.com/code/gbsnippets_gbs_01136.htm

    The BMP image is not being displayed full colour.

    I had to comment out this from the original code in order to get it to compile.

    Code:
     Sub GetNewImageSize
    Code:
    #Compiler PBWin 10
    #Compile EXE
    #Dim All
    %TRUE = -1
    #Include "Win32API.inc"
    
    'This code shows how to resize a Graphic control as the dialog
    'is resized. Also, the image is redrawn to fill the graphic control
    'while maintaining the original image aspect ratio.
    
    'Compiler Comments:
    'This code compiles only in PBWin10. It uses a resizable Graphic Control,
    'which is not available in PBWin9.  However, this link, http://gbl_00528,
    'shows an approach to use in PBWin9.
    
    'Primary Code:
    'This Sub generates the new image size
    Sub GetNewImageSize(wCont As Long, hCont As Long, wImg As Long, hImg As Long, wNew As Long, hNew As Long)
       'wCont,hCont = container size : hImg,wImg = original image size : wNew,hNew = image size to fit in container
       wNew = wImg / Max(wImg / wCont, hImg / hCont)
       hNew = hImg / Max(wImg / wCont, hImg / hCont)
    End Sub
    
    'Compilable Example:
    
    %IDC_Graphic = 600
    Global hDlg,hBMP,wImg,hImg As Dword
    
    Function PBMain() As Long
       Dialog New Pixels, 0, "Image Resize",300,300,300,200, %WS_OverlappedWindow To hDlg
       Control Add Graphic, hDlg, %IDC_Graphic,"",10,10,200,150, %WS_Border
       Graphic Attach hDlg, %IDC_Graphic
       Dialog Show Modal hDlg Call DlgProc
    End Function
    
    CallBack Function DlgProc() As Long
       Local x,y,wNew,hNew,w,h As Long
       Select Case Cb.Msg
          Case %WM_InitDialog
             'Graphic Bitmap Load "C:\PhotoShoot_Print_Server\Image_Cache_BMP\2,2017-03-18,IMG_0001.JPG,1,0,1.bmp", 0, 0 To hBMP  'put image in memory bitmap
             Graphic Bitmap Load "C:\DevPB\Epos\000017.bmp", 0, 0 To hBMP  'put image in memory bitmap
    
             Graphic Attach hBMP,0
             Graphic Get Canvas To wImg,hImg    
             Graphic Attach hDlg, %IDC_Graphic
             Graphic Copy hBMP,0                   'copy from memory bitmap to graphic control, put in upper/left
          Case %WM_Size
             Dialog Get Client hDlg To w,h
             Control Set Size hDlg, %IDC_Graphic, w-20, h-20
             RedrawImage
       End Select
    End Function
    
    'Sub GetNewImageSize(wCont As Long, hCont As Long, wImg As Long, hImg As Long, wNew As Long, hNew As Long)
    '   'wCont,hCont = container size : hImg,wImg = original image size : wNew,hNew = image size to fit in container
    '   wNew = wImg / Max(wImg / wCont, hImg / hCont)
    '   hNew = hImg / Max(wImg / wCont, hImg / hCont)
    'End Sub
    
    Sub RedrawImage
       Local w,h,x,y,wNew,hNew As Long
       Graphic Clear
       Graphic Get Client To w,h
       GetNewImageSize(w,h,wImg,hImg,wNew,hNew)     'get resized image dimensions
       x = (w-wNew)/2 : y = (h-hNew)/2               'upper/left position so resized image is centered
       Graphic Stretch hBMP, 0, (0,0)-(wImg-1,hImg-1) To (x,y)-(x+wNew-1,y+hNew-1)  'copy (resized) from memory bitmap to visible image
    End Sub

  • #2
    So add this to the GRAPHIC STRETCH statement:

    Code:
    , ,%halftone

    Comment


    • #3
      Perfect Thanks

      Comment


      • #4
        I've got a list view and 2 buttons the move when resize the dialog.

        I'm trying to add code to limit the min width and height to 250px but in the following code when I resize below that the dialog goes crazy

        Code:
        
        
        '----------------------------------------------------------------------------(')
        
        
        
        #Compiler PBWin 10
        #COMPILE EXE
        #DIM ALL
        
        
        
        '----------------------------------------------------------------------------(')
        
        
        
        %IDC_ListView = 600
        %IDC_Graphic = 601
        %IDC_Zoom_In_Button = 602
        %IDC_Zoom_Out_Button = 603
        
        
        
        '----------------------------------------------------------------------------(')
        
        
        
        #INCLUDE "Win32API.inc"
        
        
        
        '----------------------------------------------------------------------------(')
        
        
        
        FUNCTION PBMAIN( ) AS LONG
            '
            LOCAL hDlg AS LONG
            '
            DIALOG NEW PIXELS, 0, "Image Resize", 1, 1, 600, 400, %WS_OverlappedWindow TO hDlg
          '
            CONTROL ADD listview, hDlg, %IDC_ListView, "", 5, 10, 130, 150, _
              %WS_CHILD OR %WS_VISIBLE OR %WS_BORDER OR %WS_TABSTOP OR %LVS_REPORT OR %LVS_SHOWSELALWAYS, _
              %WS_EX_LEFT OR %WS_EX_RIGHTSCROLLBAR
            '
            CONTROL ADD Graphic, hDlg, %IDC_Graphic, "", 150, 10, 60, 150, %WS_Border
            '
            CONTROL ADD BUTTON, hDlg, %IDC_Zoom_Out_Button, "Zoom -", 5, 320, 60, 30, _
              %WS_TABSTOP OR %BS_CENTER OR %BS_VCENTER OR _
              %BS_TEXT OR %WS_CHILD OR %WS_VISIBLE, %WS_EX_LEFT OR %WS_EX_LTRREADING
            '
            CONTROL ADD BUTTON, hDlg, %IDC_Zoom_In_Button, "Zoom +", 75, 320, 60, 30, _
              %WS_TABSTOP OR %BS_CENTER OR %BS_VCENTER OR _
              %BS_TEXT OR %WS_CHILD OR %WS_VISIBLE, %WS_EX_LEFT OR %WS_EX_LTRREADING
            '
            Graphic ATTACH hDlg, %IDC_Graphic
            '
            DIALOG SHOW MODAL hDlg CALL DlgProc
            '
        END FUNCTION
        
        
        
        '----------------------------------------------------------------------------(')
        
        
        
        CALLBACK FUNCTION DlgProc( ) AS LONG
            '
            LOCAL x AS LONG
            LOCAL y AS LONG
            LOCAL wNew AS LONG
            LOCAL hNew AS LONG
            LOCAL w AS LONG
            LOCAL h AS LONG
            '
            DIM hBMP_ST AS STATIC DWORD
            DIM wImg_ST AS STATIC DWORD
            DIM hImg_ST AS STATIC DWORD
            '
            SELECT CASE Cb.Msg
                    '
                CASE %WM_InitDialog
                    '
                    Graphic Bitmap Load "C:\PhotoShoot_Print_Server\Image_Cache_BMP\2,2017-03-18,IMG_0001.JPG,1,0,1.bmp", 0, 0 TO hBMP_ST        ' put image in memory bitmap
                    '
                    GRAPHIC SET STRETCHMODE %HALFTONE
                    Graphic ATTACH hBMP_ST, 0
                    Graphic GET Canvas TO wImg_ST, hImg_ST
                    Graphic ATTACH cb.hndl, %IDC_Graphic
                    Graphic Copy hBMP_ST, 0        ' copy from memory bitmap to graphic control, put in upper/left
                    '
                CASE %WM_Size
                    '
                    DIALOG GET CLIENT cb.hndl TO w, h
                    '
                    DIALOG SET TEXT cb.hndl, STR$( w ) + " : " + STR$( h )
                    '
                    IF w < 250 THEN
                        '
                        w = 250
                        '
                        DIALOG SET SIZE cb.hndl, w, h
                        '
                    END IF
                    '
                    IF h < 250 THEN
                        '
                        h = 250
                '
                        DIALOG SET SIZE cb.hndl, w, h
                        '
                    END IF
                    '
                    CONTROL SET SIZE cb.hndl, %IDC_Graphic, w - 150, h - 20
                    '
                    CONTROL SET SIZE cb.hndl, %IDC_ListView, 130, h - 100
                    '
                    CONTROL SET LOC cb.hndl, %IDC_Zoom_Out_Button, 5, h - 80
                    '
                    CONTROL SET LOC cb.hndl, %IDC_Zoom_In_Button, 75, h - 80
                    '
                    Graphic Clear
              '
                    Graphic GET CLIENT TO w, h
                    '
                    GetNewImageSize( w, h, wImg_ST, hImg_ST, wNew, hNew )        ' get resized image dimensions
                    '
                    x = ( w - wNew ) / 2
                    y = ( h - hNew ) / 2        ' upper/left position so resized image is centered
                    '
                    Graphic Stretch hBMP_ST, 0,( 0, 0 ) - ( wImg_ST - 1, hImg_ST - 1 ) TO ( x, y ) - ( x + wNew - 1, y + hNew - 1 )        ' copy (resized) from memory bitmap to visible image
                    '
            END SELECT
            '
        END FUNCTION
        
        
        
        '----------------------------------------------------------------------------(')
        
        
        
        SUB GetNewImageSize( wCont AS LONG, hCont AS LONG, wImg AS LONG, hImg AS LONG, wNew AS LONG, hNew AS LONG )
            '
            ' wCont,hCont = container size
            ' hImg,wImg = original image size
            ' wNew,hNew = image size to fit in container
            '
            wNew = wImg / MAX( wImg / wCont, hImg / hCont )
            hNew = hImg / MAX( wImg / wCont, hImg / hCont )
            '
        END SUB

        Comment


        • #5
          Don't use "Dialog Set Size..." statements. Add GetMinMaxInfo instead, something like..

          Code:
                 
                  CASE %WM_GETMINMAXINFO             ' << Add to DlgProc
                    LOCAL MM AS MINMAXINFO PTR
                     MM=CB.LPARAM
                     @MM.ptMinTrackSize.x=250        '<-- Min X size of dialog
                     @MM.ptMinTrackSize.y=250        '<-- Min Y size of dialog
                     @MM.ptMaxTrackSize.x=600        '<-- Max X size of dialog
                     @MM.ptMaxTrackSize.y=400        '<-- Max Y size of dialog
          
                  CASE %WM_Size
                      '
          Rgds, Dave

          Comment


          • #6
            Don't know what 'crazy' means but I used this for something similar and didn't have any issues
            If I recall correctly, the DIALOG REDRAW was important.
            Code:
            CASE %WM_SIZE
                  CONTROL GET LOC CB.HNDL, %IDC_LISTBOX1 TO cleft, ctop
                  DIALOG GET CLIENT CB.HNDL TO dwide,dhigh
                  IF dwide<%MINDIALOGWIDTH THEN
                    DIALOG SET CLIENT CB.HNDL, %MINDIALOGWIDTH, dhigh
                    dwide=%MINDIALOGWIDTH
                  ELSEIF dhigh<%MINDIALOGHEIGHT THEN
                    DIALOG SET CLIENT CB.HNDL, dwide, %MINDIALOGHEIGHT
                    dhigh=%MINDIALOGHEIGHT
                  END IF
                  CONTROL SET SIZE CB.HNDL, %IDC_LISTBOX1, dwide-(cleft*2), dhigh-(ctop+60)
                  CONTROL SET LOC CB.HNDL, %IDC_DATESTR, 5, dhigh-20
                  CONTROL SET LOC CB.HNDL, %IDC_YYYYMMDD, 65, dhigh-20
                  CONTROL SET LOC CB.HNDL, %IDC_YYYY_MM_DD, 125, dhigh-20
                  CONTROL SET LOC CB.HNDL, %IDC_YYYYMMDDHHMMSS, 184, dhigh-20
                  CONTROL SET LOC CB.HNDL, %IDC_HHMMSS, 270, dhigh-20
                  CONTROL SET LOC CB.HNDL, %IDC_HH_MM_SS, 325, dhigh-20
                  CONTROL SET LOC CB.HNDL, %IDC_USER1,   5, dhigh-40
                  CONTROL SET LOC CB.HNDL, %IDC_USER2,  50, dhigh-40
                  CONTROL SET LOC CB.HNDL, %IDC_USER3,  95, dhigh-40
                  CONTROL SET LOC CB.HNDL, %IDC_USER4, 140, dhigh-40
                  CONTROL SET LOC CB.HNDL, %IDC_USER5, 185, dhigh-40
                  CONTROL SET LOC CB.HNDL, %IDC_USER6, 230, dhigh-40
                  CONTROL SET LOC CB.HNDL, %IDC_USER7, 280, dhigh-40
                  CONTROL SET LOC CB.HNDL, %IDC_USER8, 335, dhigh-40
                  CONTROL SET LOC CB.HNDL, %IDC_RSRV0,   5, dhigh-60
                  CONTROL SET LOC CB.HNDL, %IDC_RSRV1,  50, dhigh-60
                  CONTROL SET LOC CB.HNDL, %IDC_RSRV2,  95, dhigh-60
                  CONTROL SET LOC CB.HNDL, %IDC_RSRV3, 140, dhigh-60
                  CONTROL SET LOC CB.HNDL, %IDC_RSRV4, 185, dhigh-60
                  CONTROL SET LOC CB.HNDL, %IDC_RSRV5, 230, dhigh-60
                  CONTROL SET LOC CB.HNDL, %IDC_RSRV6, 280, dhigh-60
                  CONTROL SET LOC CB.HNDL, %IDC_RSRV7, 335, dhigh-60
            
                  CONTROL SET LOC CB.HNDL, %IDC_HELPME, 375, dhigh-25
                  DIALOG REDRAW CB.HNDL
            
            
                  DIALOG GET LOC CB.HNDL TO dleft, dtop
                  DIALOG GET CLIENT CB.HNDL TO dwidth, dheight
                  CONTROL GET SIZE CB.HNDL, %IDC_LISTBOX1 TO cwidth, cheight
                  CONTROL SET FOCUS CB.HNDL, %IDC_LISTBOX1
                  phylnum = FREEFILE
                  OPEN "stringers.ini" FOR OUTPUT AS phylnum
                    WRITE #phylnum, dleft, dtop, dwidth, dheight, cwidth, cheight
                  CLOSE #phylnum
            :
            Rod
            "To every unsung hero in the universe
            To those who roam the skies and those who roam the earth
            To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

            Comment


            • #7
              Thanks everyone for your input.

              Dave's method works perfectly.

              Comment


              • #8
                I've updated the code below. I'm trying to get a zoom / scroll feature added.

                So far when you click the zoom buttons or move the mouse wheel the image is zoomed. I can't figure out how to keep the aspect correct.

                Would be great if when the user zooms in so when the image is clipped, scroll bars are shown.

                Help very much appreciated!

                Code:
                
                
                '----------------------------------------------------------------------------(')
                
                
                
                #Compiler PBWin 10
                #COMPILE EXE
                #DIM ALL
                
                
                
                '----------------------------------------------------------------------------(')
                
                
                
                %IDC_ListView = 600
                %IDC_Graphic = 601
                %IDC_Zoom_In_Button = 602
                %IDC_Zoom_Out_Button = 603
                
                
                
                '----------------------------------------------------------------------------(')
                
                
                
                #INCLUDE "Win32API.inc"
                
                
                
                '----------------------------------------------------------------------------(')
                
                
                
                FUNCTION PBMAIN( ) AS LONG
                    '
                    LOCAL hDlg AS LONG
                    '
                    DIALOG NEW PIXELS, 0, "Image Resize", 1, 1, 600, 400, %WS_OverlappedWindow TO hDlg
                    '
                    CONTROL ADD listview, hDlg, %IDC_ListView, "", 5, 10, 130, 150, _
                      %WS_CHILD OR %WS_VISIBLE OR %WS_BORDER OR %WS_TABSTOP OR %LVS_REPORT OR %LVS_SHOWSELALWAYS, _
                      %WS_EX_LEFT OR %WS_EX_RIGHTSCROLLBAR
                    '
                    CONTROL ADD Graphic, hDlg, %IDC_Graphic, "", 150, 10, 444, 374, %WS_CHILD OR %WS_VISIBLE OR %WS_BORDER, %WS_EX_CLIENTEDGE
                    '
                    CONTROL ADD BUTTON, hDlg, %IDC_Zoom_Out_Button, "Zoom -", 5, 320, 60, 30, _
                      %WS_TABSTOP OR %BS_CENTER OR %BS_VCENTER OR _
                      %BS_TEXT OR %WS_CHILD OR %WS_VISIBLE, %WS_EX_LEFT OR %WS_EX_LTRREADING
                    '
                    CONTROL ADD BUTTON, hDlg, %IDC_Zoom_In_Button, "Zoom +", 75, 320, 60, 30, _
                      %WS_TABSTOP OR %BS_CENTER OR %BS_VCENTER OR _
                      %BS_TEXT OR %WS_CHILD OR %WS_VISIBLE, %WS_EX_LEFT OR %WS_EX_LTRREADING
                    '
                    Graphic ATTACH hDlg, %IDC_Graphic
                    '
                    DIALOG SHOW MODAL hDlg CALL DlgProc
                    '
                END FUNCTION
                
                
                
                '----------------------------------------------------------------------------(')
                
                
                
                CALLBACK FUNCTION DlgProc( ) AS LONG
                    '
                    LOCAL x AS LONG
                    LOCAL y AS LONG
                    LOCAL wNew AS LONG
                    LOCAL hNew AS LONG
                    LOCAL w AS LONG
                    LOCAL h AS LONG
                    LOCAL MM AS MINMAXINFO PTR
                    '
                    DIM hBMP_ST AS STATIC DWORD
                    DIM wImg_ST AS STATIC DWORD
                    DIM hImg_ST AS STATIC DWORD
                    DIM Zoom_ST AS STATIC LONG
                    '
                    SELECT CASE Cb.Msg
                            '
                        CASE %WM_InitDialog
                            '
                            ' Path Below To A Valid BMP File :-
                            Graphic Bitmap Load "C:\PhotoShoot_Print_Server\Image_Cache_BMP\2,2017-03-18,IMG_0001.JPG,1,0,1.bmp", 0, 0 TO hBMP_ST        ' put image in memory bitmap
                            '
                            GRAPHIC SET STRETCHMODE %HALFTONE
                            Graphic ATTACH hBMP_ST, 0
                            Graphic GET Canvas TO wImg_ST, hImg_ST
                            Graphic ATTACH cb.hndl, %IDC_Graphic
                            GRAPHIC COLOR %BLACK, %BLACK
                            Graphic Copy hBMP_ST, 0        ' copy from memory bitmap to graphic control, put in upper/left
                            '
                        CASE %WM_GETMINMAXINFO
                            '
                            MM = CB.LPARAM
                            '
                            @MM.ptMinTrackSize.x = 250        '<-- Min X size of dialog
                            @MM.ptMinTrackSize.y = 250        '<-- Min Y size of dialog
                            '
                            ' @MM.ptMaxTrackSize.x=600        '<-- Max X size of dialog
                            ' @MM.ptMaxTrackSize.y=400       '<-- Max Y size of dialog
                            '
                        CASE %WM_SIZE
                            '
                            DIALOG GET CLIENT cb.hndl TO w, h
                            '
                            graphic SET SIZE w - 150, h - 20
                            CONTROL SET SIZE cb.hndl, %IDC_ListView, 130, h - 100
                            CONTROL SET LOC cb.hndl, %IDC_Zoom_Out_Button, 5, h - 80
                            CONTROL SET LOC cb.hndl, %IDC_Zoom_In_Button, 75, h - 80
                            '
                            Graphic Clear
                            '
                            GOSUB Draw_Graphic
                            '
                        CASE %WM_SETCURSOR
                            '
                        CASE %WM_MOUSEMOVE
                            '
                        CASE %WM_MOUSEWHEEL
                            '
                            Zoom_ST = Zoom_ST + IIF& ( HI( INTEGER, CBWPARAM ) = 120, 50, - 50 )
                            '
                            GOSUB Draw_Graphic
                            '
                        CASE %WM_COMMAND
                            '
                            IF CB.CTLMSG = %BN_CLICKED THEN
                                '
                                SELECT CASE CB.CTL
                                        '
                                    CASE %IDC_Zoom_Out_Button, %IDC_Zoom_In_Button
                                        '
                                        Zoom_ST = Zoom_ST + IIF& ( cb.ctl = %IDC_Zoom_In_Button, 50, - 50 )
                                        '
                                        GOSUB Draw_Graphic
                                        '
                                END SELECT
                                '
                            END IF
                            '
                    END SELECT
                    '
                    EXIT FUNCTION
                    '
                Draw_Graphic :
                    '
                    Graphic GET CLIENT TO w, h
                    '
                    GetNewImageSize( w, h, wImg_ST, hImg_ST, wNew, hNew )        ' get resized image dimensions
                    '
                    x = ( w - wNew ) / 2
                    y = ( h - hNew ) / 2
                    '
                    Graphic Stretch _
                      hBMP_ST, 0, _
                      ( Zoom_ST, Zoom_ST ) - ( wImg_ST - 1 - Zoom_ST, hImg_ST - 1 - Zoom_ST ) _
                      TO _
                      ( x, y ) - ( x + wNew - 1, y + hNew - 1 )        ' copy (resized) from memory bitmap to visible image
                    '
                    RETURN
                    '
                END FUNCTION
                
                
                
                '----------------------------------------------------------------------------(')
                
                
                
                SUB GetNewImageSize( wCont AS LONG, hCont AS LONG, wImg AS LONG, hImg AS LONG, wNew AS LONG, hNew AS LONG )
                    '
                    ' wCont,hCont = container size
                    ' hImg,wImg = original image size
                    ' wNew,hNew = image size to fit in container
                    '
                    wNew = wImg / MAX( wImg / wCont, hImg / hCont )
                    hNew = hImg / MAX( wImg / wCont, hImg / hCont )
                    '
                END SUB

                Comment


                • #9

                  When I zoomed in too big the image is turned upside down, so I amended the code
                  to stop it from displaying the image upside down

                  Code:
                     CASE %WM_MOUSEWHEEL
                              Zoom_ST = Zoom_ST +  IIF& ( HI( INTEGER, CBWPARAM ) = 120, 50, - 50 )
                              IF Zoom_St < 100 THEN
                                ' do NOT allow to exceed 100 otherwise it
                                ' will display upside down
                                 GOSUB Draw_Graphic
                              END IF
                  
                          CASE %WM_COMMAND
                              '
                              IF CB.CTLMSG = %BN_CLICKED THEN
                                  '
                                  SELECT CASE CB.CTL
                                          '
                                      CASE %IDC_Zoom_Out_Button, %IDC_Zoom_In_Button
                                          Zoom_ST = Zoom_ST + IIF& ( CB.CTL = %IDC_Zoom_In_Button, 50, - 50 )
                                          IF Zoom_St < 100 THEN
                                           ' do NOT allow to exceed 100 otherwise it
                                           ' will display upside down
                                             GOSUB Draw_Graphic
                                          END IF
                                  END SELECT
                                  '
                              END IF

                  Comment


                  • #10
                    "Would be great if when the user zooms in so when the image is clipped, scroll bars are shown. "

                    See GRAPHIC SET VIRTUAL.
                    Rod
                    "To every unsung hero in the universe
                    To those who roam the skies and those who roam the earth
                    To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

                    Comment


                    • #11
                      Example GRAPHIC SET VIRTUAL test.
                      Code:
                      #COMPILE EXE                                  ' PBWin10
                      #DIM ALL
                      #INCLUDE ONCE "WIN32API.INC"
                       '#RESOURCE MANIFEST, 1, "C:\PBWin10\samples\DDT\Address\XPTheme.xml"   ' for 'Themed' Dialog and controls
                      
                      %IDC_GRAPHIC = 200
                      %IDC_Btn1    = 201
                      %IDC_Btn2    = 202
                      %IDC_Btn3    = 203
                      %IDC_Grip    = 303
                      '------------------/
                      
                      FUNCTION PBMAIN()
                        ShowDlg %HWND_DESKTOP
                      END FUNCTION
                      '------------------/PBMain
                      
                      FUNCTION GetBmpFile(BYVAL hDlg AS DWORD, bmX AS LONG, bmY AS LONG, sFile AS STRING) AS LONG
                       LOCAL sFilter AS STRING
                       LOCAL lFlags, nFile AS LONG
                        sFilter     = CHR$("Bitmap files", 0, "*.bmp", 0, "All", 0, "*.*", 0)
                        lFlags      = %OFN_HIDEREADONLY OR %OFN_FILEMUSTEXIST
                        DISPLAY OPENFILE hDlg,,, "Open Image File", EXE.PATH$, sFilter, "", "", lFlags TO sFile
                      
                        nFile = FREEFILE                            ' Get size
                        OPEN sFile FOR BINARY AS nFile
                          GET #nFile, 19, bmX
                          GET #nFile, 23, bmY
                        CLOSE nFile
                        FUNCTION = bmX * bmY                        ' Return 0 if width and/or height = 0
                      END FUNCTION
                      '------------------/GetBmpFile
                      
                      CALLBACK FUNCTION DlgProc()
                       LOCAL lRes AS LONG
                        SELECT CASE AS LONG CB.MSG
                          CASE %WM_INITDIALOG
                           STATIC hGrip AS DWORD : CONTROL HANDLE CB.HNDL, %IDC_Grip TO hGrip   ' grip handle for resizable dialog
                           STATIC dX, dY, X, Y AS LONG              ' Dialog size / resize
                           STATIC bmX, bmY, zX, zY AS LONG          ' Bmp sizes (orig. and zoom)
                           STATIC sFile AS STRING                   ' Bmp Filename
                            DIALOG GET CLIENT CBHNDL TO dX, dY
                            SetWindowPos hGrip, %NULL, dX-20, dY-20, 20, 20, %SWP_NOZORDER     ' Set Grip pos
                            DIALOG POST CB.HNDL, %WM_APP + 101, dX, dY      ' Dialog initialization complete, do GUI stuff
                      
                          CASE %WM_APP + 101                                ' Common pos / resize adjust
                            dX = CB.WPARAM : dY = CB.LPARAM
                            CONTROL SET LOC CB.HNDL, %IDC_Btn1, dX-70, 5
                            CONTROL SET LOC CB.HNDL, %IDC_Btn2, dX-70, 35
                            CONTROL SET LOC CB.HNDL, %IDC_Btn3, dX-70, 65
                            SetWindowPos hGrip, %NULL, dX-20, dY-20, 20, 20, %SWP_NOZORDER     ' ReSet Grip pos
                      
                            GRAPHIC ATTACH CB.HNDL, %IDC_GRAPHIC, REDRAW    ' Reset Graphic control
                            GRAPHIC SET VIRTUAL zX, zY
                            CONTROL SET CLIENT CB.HNDL, %IDC_Graphic, MIN&(zX, dX-100), MIN(zY, dY-20)
                            GRAPHIC SET VIRTUAL zX, zY
                            GRAPHIC RENDER sFile, (0,0)-(zX,zY)
                            GRAPHIC REDRAW
                            CONTROL SET FOCUS CB.HNDL, %IDC_Graphic         ' (so that mousewheel etc sent to Graphic)
                      
                          CASE %WM_SIZE                                     ' Dialog size has changed - adjust controls to suit
                            X = LO(WORD, CB.LPARAM)                         ' New Dlg Client Width
                            Y = HI(WORD, CB.LPARAM)                         ' New Dlg Client Height
                      
                       '    CASE %WM_EXITSIZEMOVE                             ' fewer redraws :)
                            DIALOG SEND CBHNDL, %WM_App + 101, X, Y
                      
                          CASE %WM_COMMAND
                            SELECT CASE AS LONG CB.CTL
                              CASE %IDC_Btn1    ' (Bmp)
                                IF CB.CTLMSG = %BN_CLICKED THEN
                                  lRes = GetBmpFile(CB.HNDL, bmX, bmY, sFile)
                                  DIALOG SET TEXT CB.HNDL, PATHNAME$(NAMEX, sFile) + STR$(bmX) + " x" + STR$(bmY)
                                  IF lRes = 0 THEN MSGBOX "Bmp error", %MB_ICONMASK, "No Bmp" : EXIT FUNCTION
                                  zX = bmX : zY = bmY                       ' preserve bmp size ?
                                  DIALOG GET CLIENT CBHNDL TO dX, dY
                                  DIALOG POST CB.HNDL, %WM_APP + 101, dX, dY
                                END IF
                              CASE %IDC_Btn2    ' (+)
                                IF CB.CTLMSG = %BN_CLICKED THEN
                                   zX = zX*2 : zY = zY*2                     ' Trap Max size here!
                                   DIALOG GET CLIENT CBHNDL TO dX, dY
                                   DIALOG POST CB.HNDL, %WM_APP + 101, dX, dY
                                END IF
                              CASE %IDC_Btn3    ' (-)
                                IF CB.CTLMSG = %BN_CLICKED THEN
                                   zX = zX\2 : zY = zY\2                     ' Trap min size here!
                                   DIALOG GET CLIENT CBHNDL TO dX, dY
                                   DIALOG POST CB.HNDL, %WM_APP + 101, dX, dY
                                END IF
                            END SELECT
                      
                          CASE %WM_GETMINMAXINFO  ': Exit function            ' uncomment ": Exit Function" to use default Min Max dialog sizes
                           LOCAL MM AS MINMAXINFO PTR
                            MM=CB.LPARAM
                            @MM.ptMinTrackSize.x=bmX / 2            '<-- Min X size of dialog
                            @MM.ptMinTrackSize.y=bmY / 2            '<-- Min Y size of dialog
                       '      @MM.ptMaxTrackSize.x=bmX + 160          '<-- Max X size of dialog
                       '      @MM.ptMaxTrackSize.y=bmY + 60           '<-- Max Y size of dialog
                      
                        END SELECT
                      END FUNCTION
                      '------------------/DlgProc
                      
                      FUNCTION ShowDlg(BYVAL hParent AS DWORD) AS LONG
                       LOCAL hDlg  AS DWORD
                        DIALOG NEW PIXELS, hParent, "Bmp Viewer", 100, 100, 300, 200, %WS_THICKFRAME OR %WS_CAPTION OR %WS_SYSMENU _
                                           OR %WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX, TO hDlg
                          CONTROL ADD GRAPHIC, hDlg, %IDC_GRAPHIC, "", 10, 10, 1000, 1000, %WS_TABSTOP OR %WS_BORDER OR %SS_NOTIFY
                          CONTROL ADD BUTTON, hDlg, %IDC_Btn1, "Bitmap", 600-80, 5,  60, 20
                          CONTROL ADD BUTTON, hDlg, %IDC_Btn2, "&+", 600-80, 35, 60, 20
                          CONTROL ADD BUTTON, hDlg, %IDC_Btn3, "&-", 600-80, 55, 60, 20
                          CONTROL ADD SCROLLBAR, hDlg, %IDC_Grip, "", 0, 0, 0, 0, _                  ' "Scrollbar" ~ actually just Grip
                                                 %SBS_SIZEGRIP OR %SBS_BOTTOMALIGN
                      
                        DIALOG SHOW MODAL hDlg, CALL DlgProc
                      END FUNCTION
                      '------------------/ShowDlg
                      
                      #IF 0
                            GripSizeX = GetSystemMetrics(%SM_CXVSCROLL)   ' Get width of grip  example just uses '20'
                            GripSizeY = GetSystemMetrics(%SM_CYHSCROLL)   ' Get height of grip
                      #ENDIF
                      Rgds, Dave

                      Comment


                      • #12
                        Thanks Dave,

                        At work today so will have a good look tomorrow.

                        In the documentation it doesn't explain what the parameters mean, x1,y1 etc
                        GRAPHIC STRETCH hBmp, ID, (x1,y1)-(x2,y2) TO (x3,y3)-(x4,y4) [, Mix, Stretch]

                        Comment


                        • #13
                          "In the documentation it doesn't explain what the parameters mean, x1,y1 etc"
                          Well, it kind of does as most times, if not all, you start with the left top and finish with right bottom.
                          So it could be
                          GRAPHIC STRETCH hBmp, ID, (left,top)-(right,bottom) TO (x3,y3)-(x4,y4) [, Mix, Stretch]
                          and similar for the x3,y3, x4,y4.
                          Rod
                          "To every unsung hero in the universe
                          To those who roam the skies and those who roam the earth
                          To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

                          Comment

                          Working...
                          X