exception in paint event

I am getting an exception error on the // scaledown if statement which triggers a paint event on the image “MainImg” ( canvas ), i have marked the error line as this << .
It is an exception error with the REALbasic.Rect(-1) which i am not sure what the means.
MainImage is a global string property which is the name of the image.

The puzzling thing is this has been working fine with no problems for maybe 1/2 year , until today. as far as i know i have changed nothing.

here is the code below

please advise

[code]dim f as FolderItem
dim p as Picture
Dim nW, nH as integer
Dim ratio as Integer

if MainWin.DBLocation.Text = “” then
End if

if MainImage = “” then
MsgBox “Main Image Not Set”
end if

f = MainWin.PictureFolder.Child(MainImage)

if f = Nil or Not f.Exists Then
Status1.Text = “Photo " + MainImage + " not found”
End if

p = f.OpenAsPicture

//msgBox Str(p.Height)

// scaledown
if p.Height > 800 then << Exception Error Here /
ratio = p.Height - 400
end if

nW = p.Width - ratio
nH = p.Height - ratio
g.ForeColor= &c000000
g.DrawRect 0,0,Me.width,Me.height
g.DrawPicture(p,0,0, nW ,nH ,0,0,p.Width,p.Height)[/code]

  • isn’t the syntax “f.openpicture” been replaced with “picture.open(f)”???
  • my guess is the “P” is nil … as in while “F” may be valid as a file, it did not contain a valid picture

as a side note… .you should never put MSGBOX in a Paint event…

Hi Dave,

I will immediately remove the Msgbox.

I tried
//p = f.OpenAsPicture
p = Picture.Open(f)
but got the same exception

The photo is a .JPG taken by a cannon. I drag and drop the photo on to my “thumb” canvas, it this reads the name of the photo, gets the location, sets the FolderItem location, then attempts to open the picture with f.OpenAsPicture OR picture.open(f)

below is the drag and drop event

[code]Dim DropImgPath As FolderItem
Dim ImageName As String = MainWin.ItemCode.Text + “-” + format(index+1, “00”) + “.jpg”
Dim rtn as string

If obj <> Nil Then

// copy full size image to _DB
DropImgPath = obj.FolderItem

//MsgBox DropImgPath.NativePath + Chr(13) + PictureFolder.NativePath

// show new image on screen
MainImage = imageName

// Create Thumbnail for image & reload
mkThumbnail(ImageName, MainWin.PictureFolder.Child(ImageName), MainWin.ThumbFolder, 80)
mkThumbnail(ImageName, MainWin.PictureFolder.Child(ImageName), MainWin.MtnFolder, 130)


End if

if index = 0 then // refresh the 1st Image
MainWin.ItemID = MainWin.ItemCode.text
if MainWIn.ItemID <> “” then
end if
end if[/code]

I’m just curious as to why?

very simple. a Msgbox takes focus away from the window… when you dismiss it, the window gets focus back, which in turn fires the Paint event, which may display the Msgbbox again, which takes focus away from the window … [add recursion here]

That makes perfect sense. I didn’t even consider that paint would be fired again when closing a msgbox. Thank you.


Found the problem. the image was accidentally created as a folder causing the exception.

but thank you sir