The DrawPicture method of the graphics class can be used to scale a
picture
to any width and height. This is done by providing the width and height
of
the picture as well as the new width and height. In most cases you would
probably want to scale the picture proportionally but that's not built
in to
the DrawPicture method. The code below will present an open file dialog
box
allowing you to choose a picture and will then scale the picture
proportionally to no bigger than the size of the canvas control. To try
this
out:
1. Create a new project.
2. Drag a canvas and a pushbutton on to the window.
3. Copy the code below into the action event of the pushbutton.
4. Choose Run from the Debug menu and click your pushbutton then choose
a
picture.
Dim f As FolderItem
Dim p As Picture
Dim maxWidth, maxHeight As Integer
Dim factor As Double
maxWidth = Canvas1.width
maxHeight = Canvas1.height
f = GetOpenFolderItem("????")
if f <> nil then
p = f.OpenAsPicture
end if
factor = Min( maxWidth / p.Width, maxHeight / p.Height )
factor = Min( factor, 1.0 ) // (don't scale it up if it's too small!)
pic = NewPicture( p.Width * factor, p.Height * factor, 32 )
pic.graphics.DrawPicture p, 0,0,pic.width,pic.height,
0,0,p.width,p.height
Canvas1.Refresh
Thanks to Joe Strout for providing this helpful tip.
--
Geoff Perlman
President & CEO
REAL Software, Inc.
http://www.realsoftware.com
mailto:geoff at realsoftware dot com
Phone: 512-263-1233 x711
Fax: 512-263-1441
- - - - - - - - - -
Got a useful tip to share? Send it to us at:
REALbasic-tips at lists dot realsoftware dot com dot
For list commands, send "Help" in the body of a message to
<requests at lists dot realsoftware dot com>
|