Announcement

Collapse
No announcement yet.

Image/Video Resizing

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

  • Image/Video Resizing

    There are 3 ways of resizing an image/video in a dialog that are of interest to me.

    1. centered and not resized
    2. centered and resized to fit within a dialog
    3. centered and resized to fill the dialog

    An image/video could be positioned anywhere within the dialog but I'm most interested in having the center of the image/video to be at the center of of the dialog while maintaining the image/video aspect ratio.

    In the following equations, w/h is dialog size and wRes/hRes is video frame size (resolution). wNew/hNew is the displayed size of the video window.

    Resizing Options:
    There are 3 possible constraints on resizing the image/video that are of interest:

    1. image/video window size is not resized, just centered over the dialog
    wNew = wRes
    hNew = hRes

    2. image/video is resized to fit within the dialog client area. there may be empty borders. none of the image/video will be outside the dialog client area.
    wNew = wRes / Max(wRes / w, hRes / h)
    hNew = hRes/ Max(wRes / w, hRes / h)

    3. video window is resized to fill the dialog. there will be no empty borders. two opposing sizes of the video window will be outside the dialog client area unless image/video size = dialog client size.
    wNew = wRes / Min(wRes / w, hRes / h)
    hNew = hRes/ Min(wRes / w, hRes / h)

    In all three cases, this equation would apply to the position/size of the displayed image/video:
    SetWindowPosition(w/2-0.5*wNew,h/2-0.5*hNew,wNew,hNew)

  • #2
    Here's some code that demonstrates all 3 results - centered with 3 types of resizing. In this example the starting image/video is 500x300 and the output is an area of 300x200.


    Click image for larger version  Name:	pb_2225.jpg Views:	0 Size:	91.4 KB ID:	803835

    Code:
    'Compilable Example:
    #Compiler PBWin 10
    #Compile Exe
    #Dim All
    %Unicode = 1
    #Include "Win32API.inc"
    
    Enum Equates Singular
       IDC_Result = 500
    End Enum
    
    Global hDlg, hBMP, hFont As Dword
    
    Function PBMain() As Long
       Dialog Default Font "Tahoma",12,1
       Dialog New Pixels, 0, "Video Display ReSizing",,,1350,650, %WS_OverlappedWindow To hDlg
       Graphic Bitmap Load "500x300.bmp",0,0 To hBMP
       Control Add Graphic, hDlg, %IDC_Result,"", 0,0,1600,900, %WS_Border
       Graphic Attach hDlg, %IDC_Result
       Dialog Show Modal hDlg Call DlgProc
    End Function
    
    CallBack Function DlgProc() As Long
       Select Case Cb.Msg
          Case %WM_InitDialog
             Font New "Tahoma", 16,1 To hFont
             Graphic Set Font hFont
             DrawResult
       End Select
    End Function
    
    Sub DrawResult
       Local w,h,wNew,hNew,wRes,hRes,xCenter,yCenter As Long
    
       Graphic Clear
       Graphic Copy hBMP,0 To (100,100)
       Graphic Set Pos(100,75) : Graphic Print "Original"
    
       wRes = 500            : hRes = 300
       w = 300               : h = 200
       Graphic Width 3
    
       'no resize, center
       Graphic Set Pos(900,25) : Graphic Print "Not Resized"
       xCenter = 800 + w/2  : yCenter = 100  + h/2
       wNew = wRes
       hNew = hRes
       Graphic Stretch hBMP, 0, (0,0)-(500,300) To (xCenter-wNew/2,yCenter-hNew/2)-(xCenter+wNew/2,yCenter+hNew/2) ,%Mix_CopySrc, %HalfTone
       Graphic Box (800,100)-(1100,300),0,%Red
    
       'resize fit
       Graphic Set Pos(700,375) : Graphic Print "Fit"
       xCenter = 650 + w/2  : yCenter = 400  + h/2
       wNew = wRes / Max(wRes / w, hRes / h)
       hNew = hRes/ Max(wRes / w, hRes / h)
       Graphic Stretch hBMP, 0, (0,0)-(500,300) To (xCenter-wNew/2,yCenter-hNew/2)-(xCenter+wNew/2,yCenter+hNew/2) ,%Mix_CopySrc, %HalfTone
       Graphic Box (650,400)-(950,600),0,%Red
    
       'resize fill
       Graphic Set Pos(1100,375) : Graphic Print "Fill"
       xCenter = 1000 + w/2  : yCenter = 400  + h/2
       wNew = wRes / Min(wRes / w, hRes / h)
       hNew = hRes/ Min(wRes / w, hRes / h)
       Graphic Stretch hBMP, 0, (0,0)-(500,300) To (xCenter-wNew/2,yCenter-hNew/2)-(xCenter+wNew/2,yCenter+hNew/2) ,%Mix_CopySrc, %HalfTone
       Graphic Box (1000,400)-(1300,600),0,%Red
    End Sub

    Comment

    Working...
    X