[Gambas-user] Having a bit of trouble with ImageView ZoomfFit
Cedron Dawg
cedron at exede.net
Sat May 11 15:27:40 CEST 2019
I'm not that familiar with the internals, so if I misstate something, somebody please correct me.
Your requirements are:
1) Load an image file
2) It will resize to fit as user adjusts form
In order to pull that off, the original image has to be stored somewhere in memory, and then any time a new size is needed, the original image needs to be zoomed (stretched retaining aspect ration) to fit on the larger dimension. You can't keep resizing the same image.
My understanding is that a PictureBox is the least clunky way to display an image on a form. The whole point of an ImageView (I thought) was to provide scroll bars when an image is too large to fit the window area.
This code works. Whether it is the best way, I don't know.
Ced
' Gambas class file
Private mySourceImage As Image
Private myDisplayImage As Image
Private myAspectRatio As Float
'=============================================================================
Public Sub Form_Open()
mySourceImage = Image.Load("~/Pictures/Wallpaper1920/Squirrel1920.JPG")
myAspectRatio = mySourceImage.W / mySourceImage.H
End
'=============================================================================
Public Sub Form_Resize()
Dim theZoomW, w, h, mw, mh As Integer
Dim theDisplayH, theDisplayW As Integer
theDisplayW = Me.W - 20
theDisplayH = Me.H - 20
theZoomW = theDisplayH * myAspectRatio
mw = theDisplayW - theZoomW
If mw >= 0 Then
mh = 0
w = theZoomW
h = theDisplayH
Else
mw = 0
w = theDisplayW
h = theDisplayW / myAspectRatio
mh = theDisplayH - h
Endif
myDisplayImage = mySourceImage.Stretch(w, h)
DisplayPictureBox.Move(10 + mw / 2, 10 + mh / 2, w, h)
DisplayPictureBox.Image = myDisplayImage
End
'=============================================================================
----- Original Message -----
From: "adamnt42" <adamnt42 at gmail.com>
To: "user" <user at lists.gambas-basic.org>
Sent: Saturday, May 11, 2019 6:19:12 AM
Subject: Re: [Gambas-user] Having a bit of trouble with ImageView ZoomfFit
Yes, that was how I worked around it initially. But it is a bit of an
impost on the user, I (and they) just want to see it zoomed to fit.
I'm grasping at straws here, it wont ZTF (zoom to fit) if the image is
slightly larger than the control or it's to do with the
OS/Desktop/GUI/gardengnome that manages images? We used to use
PictureBoxes but they are such a clunky control. I thought, through
recent updates, that ImageViews were a much nicer control to work with.
I don't think I use <Imageview>.Show anywhere, we just load it and hope
we can set the active zoom via ZoomFit().
Horribly, but not very often, it works.
Bugger, damn, etc
b
More information about the User
mailing list