REALbasic : Scaling an image for display on a canvas

In VFP, it’s easy to display a simple image on a form and VFP will take care of scaling the image based on the “Stretch” setting. “0” will clip the image; “1” will stretch the image isometrically; “2” will do a full stretch of the image based on the size of the image control.

In REALbasic, it leaves this up to the developer. This kind of surprised me as I thought that it would have had more built in tools. It also surprised me on how much digging I needed to do to find my ultimate solution. Hopefully, this will help someone else out.

I created a new project with a single window. I added one canvas (Canvas1), a pushbutton to “Retrieve Image”, and two text boxes. One text box was to display the full path to the image, and the second shows the path to the executable that is currently running. I’m going to want to calculate the relative path to the image and this is what I will want to store into my database of images when I add this functionality into my real life application.

Anyway, lets get to the code. I’ve also added two methods to the window. “mGetImage” and “mShowImage”. Forgive my modified Hungarian notation. I find that it really helps me understand what I’m working with whether it’s a method, an object, a string, or whatever.

Protected Sub mGetImage()
Dim ImageTypes As New FileType
Dim cImage As String
Dim oFolderItem As folderItem
Dim oPicture As Picture
Dim oGraphics As Graphics

ImageTypes.Name = “Image Files”
ImageTypes.Extensions = “.bmp;.jpg;.gif”

// Prompt for picture if you want to get it from the user.
// Commented out but it’s here if you want this option.
//oFolderItem = getOpenFolderItem(ImageTypes)

// Hard coded image. This value could have come from a database.
cImage = “C:\Documents and Settings\My Documents\My Pictures\P7130117.jpg”
oFolderItem = GetFolderItem(cImage)

If oFolderItem Nil Then
oPicture = oFolderItem.OpenAsPicture
// Showing the full path to the image.
txtImagePath.Text = oFolderItem.AbsolutePath
// Showing the full path to the executable that we’re running.
txtPath.Text = GetFolderItem(“”).AbsolutePath

// A call to the method that displays the scaled picture.
mShowImage(oPicture)
Else
MsgBox “Problem: Folder Item was nil”
End If
End Sub

Protected Sub mShowImage(oPicture AS Picture)
DIM nWidthScale, nHeightScale AS Double
DIM nNewWidth, nNewHeight AS Integer

nWidthScale = Canvas1.Width / oPicture.Width
nHeightScale = Canvas1.Height / oPicture.Height

nHeightScale = MIN(nWidthScale, nHeightScale)

nNewWidth = ROUND(oPicture.Width * nHeightScale)
nNewHeight = ROUND(oPicture.Height * nHeightScale)

Canvas1.graphics.drawPicture oPicture, 0,0,nNewWidth,nNewHeight, 0,0,oPicture.width, oPicture.height

End Sub

My only complaint is that if the Window/Form is resized that the image doesn’t automatically repaint even if it was to stay the same size. Easy enough to fix and from the “resize” event of the Window/Form, I just recall my methods. I hope this helps someone out there!

Leave a Reply