Auto-adjust an image/bitmap control to keep picture aspect ratio. Useful when you have to show variable pictures in same control and pictures are different in width or height.
Code: Select all
USAGE:
DEFINE DIALOG oDlg.....
REDEFINE IMAGE | BITMAP oImg OF oDlg ADJUST .....
ACTIVATE DIALOG oDlg ON INIT SetAspectRatio(oImg)
or
REDEFINE .....
ON CHANGE
(cBmpFile:=....,;
( oImg:LoadImage(,cBmpFile)
SetAspectRatio(oImg))
NOTES: It could be added as a method to tBitmap class: oImg:SetAspectRatio(lKeepAspect)
Tested with FWH803-xHb1B,BCC55
FUNCTION SetAspectRatio(oImg)
Local nImgWidth, nImgHeight
Local nCtrlWidth, nCtrlHeight
Local y1,x1,y2,x2,dx,dy
WITH OBJECT oImg
:Hide()
IF Empty(:Cargo)
WITH OBJECT :tControl()
:Cargo:={:nTop,:nLeft,:nWidth,:nHeight}
//use Cargo to store init control position and dims. if added as
// method, we can use a new data to store them
END
ELSE
:Move(:Cargo[1],:Cargo[2],:Cargo[3],Cargo[4])
//always resize to original pos before changing image
ENDIF
nImgWidth:=:nWidth
nImgHeight:=:nHeight
WITH OBJECT :tControl()
y1:=:nTop; x1:=:nLeft
y2:=y1+:nHeight-1; x2:=x1+:nWidth-1
END
nCtrlWidth:=x2-x1+1
nCtrlHeight:=y2-y1+1
ImgAdj(nImgWidth,nImgHeight,nCtrlWidth,nCtrlHeight,@dx,@dy)
x1:=x1+(x2-x1-dx)/2; y1:=y1
:Move(y1,x1,dx,dy)
:Show()
END
return nil
STATIC Function ImgAdj(dx1,dy1,dx2,dy2,dx,dy)
//Get new control dimensions dx,dy to assure aspect ratio is kept.
Local l:=dy1/dx1
//dx2+=8; dy2+=8
IF dy2/dx2<l
dy=dy2; dx:=dy/l
ELSE
dx:=dy2; dy:=l*dx
ENDIF
IF dx>dx2
dx:=dx2; dy:=l*dx
ENDIF
IF dy>dy2
dy:=dy2; dx:=dy/l
ENDIF
dx:=Int(dx); dy:=Int(dy)
return nil