I want to resize a picture to the size of a canvas. I’m using the following code, but it doesn’t work.
Open file:
Var jpegType As New FileType
jpegType.Name = “image/jpeg”
jpegType.Extensions = “jpg;jpeg”
Var pngType As New FileType
pngType.Name = “image/png”
pngType.Extensions = “png”
Var f As FolderItem
dim p as Picture
f = FolderItem.ShowOpenFileDialog(jpegType + pngType)
p = f.OpenAsPicture
paint:
var p as new picture
g.DrawPicture(p 0, 0, g.Width, g.Height, 0, 0, p.Width, p.Height)
me.Backdrop=p
Var f As FolderItem
dim p as Picture
dim p2 as new Picture(Canvas1.Width, Canvas1.Height)
f = FolderItem.ShowOpenFileDialog(jpegType + pngType)
p = f.OpenAsPicture
p2 = p2.Graphics.DrawPicture(p, 0, 0, p2.Graphics.Width, p2.Graphics.Height, 0, 0, p2.Width, p2.Height)
Canvas1.Backdrop = p2 'that should cause a invalivate and a repaint and the backdrop should be visible
or sub class your canvas, add a new picture property MyBackground
and use in real time in the paint event if MyBackground = nil then return g.DrawPicture(MyBackground, 0, 0, g.Width, g.Height, 0, 0, MyBackground.Width, MyBackground.Height)
You need to keep the picture in memory, add a property to your canvas subclass or to the window that holds the canvas class: p As Picture
Somewhere open the picture (button action or something):
Var jpegType As New FileType
jpegType.Name = “image/jpeg”
jpegType.Extensions = “jpg;jpeg”
Var pngType As New FileType
pngType.Name = “image/png”
pngType.Extensions = “png”
Var f As FolderItem
dim p as Picture
f = FolderItem.ShowOpenFileDialog(jpegType + pngType)
p = f.OpenAsPicture
Now in the paint event:
If p <> Nil Then
g.DrawPicture(p, 0, 0, g.Width, g.Height, 0, 0, p.Width, p.Height)
End If
The pictures I try to load are real, they open in other graphics software
When I open the app there is a black square op the window. After “loading” the picture it is still black.
I do not see any error message.
i guess this canvas p property is still nil and you see this rect if you use example from jeff.
first assign a property of this sub class canvas the picture
then invalidate the canvas. it will call paint event then.
remove all dim p as Picture
if you sub class a canvas by context menu you can add own propertys and methods.
then drop into your window.
in your “own” canvas you could use computed propertys so you can invalidate it if picture was changed. that is very useful.
if you use a normal property for a own picture u have to trigger the paint event by .invaliate
property names are written with first letter upper case.
Mainly because whatever code you have, it isnt what we have supplied.
So do this, and ONLY this:
start a new project.
On the window that appears, add a property called P of type picture
To the window, add a click event
In the click event, this code and only this code:
Var jpegType As New FileType
jpegType.Name = “image/jpeg”
jpegType.Extensions = “jpg;jpeg”
Var pngType As New FileType
pngType.Name = “image/png”
pngType.Extensions = “png”
Var f As FolderItem
f = FolderItem.ShowOpenFileDialog(jpegType + pngType)
p = f.OpenAsPicture
if p = nil then
msgbox "no picture was received"
end if
self.invalidate
Add a paint event to the window, containing this and only this code:
If p <> Nil Then
g.DrawPicture(p, 0, 0, g.Width, g.Height, 0, 0, p.Width, p.Height)
else
g.fillrect 10,10,20,20
End If
Run it, click the window.
You will either get a message to say there is no picture, or you will get a picture.