I have 2 apps that use the exact same code to drop an object on a canvas - one works and the other gets a nil object exception.
I literally copied and pasted the Canvas from the app that works to the app that doesn’t. And I copied and pasted the ‘ResizeToFit’ method from the app that works to the one that doesn’t.
I can drag a jpg file from the desktop and drop it on the canvas and everything works perfectly on one app. I can do the same image drop on the other and it gets the exception.
Heres the code copied from the app that DOES NOT WORK:
The nil exception is on the line indicated by *** get the nil exception here —> near the bottom of the ResizeToFit Method.
The open event for the canvas:
Sub Open() Handles Open
Me.AcceptPictureDrop
Me.AcceptFileDrop("image/jpeg")
End Sub
The DropObject for the canvas:
[code]Sub DropObject(obj As DragItem, action As Integer) Handles DropObject
Dim pPictureIn As Picture
If Obj.PictureAvailable Then
pPictureIn = obj.Picture
Elseif Obj.FolderItemAvailable Then
pPictureIn = Picture.Open(obj.FolderItem)
End If
Me.backdrop = ResizeToFit(pPictureIn, 135, 175)
If bEditCheckStatus Then
SetStatus(“E”)
End If
Return
End Sub
[/code]
The ResizeToFit Method:
[code]Private Function ResizeToFit(p As Picture, maxWidth As Integer, maxheight As Integer) as Picture
Dim Hin As Integer
Dim Win As Integer
Dim ratio As Double
Hin = p.Height
Win = p.Width
ratio = Win / Hin
Dim w1 As Integer = maxWidth
Dim h1 As Integer = w1 / ratio
Dim h2 As Integer = maxHeight
Dim w2 As Integer = h2 * ratio
Dim useWidth, useHeight As Integer
If h1 > maxHeight Then
useWidth = w2
useHeight = h2
Elseif w2 > maxWidth Then
useWidth = w1
useHeight = h1
Elseif ( maxHeight - h1 ) < ( maxWidth - w2 ) Then
useWidth = w1
useHeight = h1
Else
useWidth = w2
useHeight = h2
End If
Dim newPict As New Picture( useWidth, useHeight, p.Depth )
*** get the nil exception here —> newPict.Graphics.DrawPicture( p, 0, 0, newPict.Width, newPict.Height, 0, 0, p.Width, p.Height )
Return newPict
End Function
[/code]