Resize image

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

You create p in the Paint method. It should be passed to the Paint method as a parameter.

What does this mean? What is the result of your code?

Why? Frank is doing a BackDrop.

How can I do that?

in a click event its more

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)

assign any picture to MyBackground at runtime

I get items that doesn’t exist: p, p2, graphics, height

Just remove me.backdrop=p and keep the g.DrawPicture

gives the same result

You have p as new picture? Why?
You need to keep the picture once loaded.

Do not re instantiate it (remove the dim p as new picture)

I removed p as new picture, but in the paint event I still get “items not found or not exist”

Maybe you should follow the user guide first.

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

works perfect here.

1 Like

It doesn’t crash anymore, but I get no picture.

OK.
Step by step:

Have p as a property of the window
Do not have Var P or Dim P as picture anywhere else

Use your code to open p as a picture.

In the paint event of the window (?), have this

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

After opening the picture into P, issue this command:
self.refresh // (or self.invalidate)

This assumes you do not have a canvas on the window.
If you see a black square, you havent loaded a picture yet

1 Like

I get image expects class picture, but this is class window

Okay, it’s kinda working now, but I don’t get a picture, only a black square.

please ty some example projects, check for graphics.

Go to File → New Project → Examples → Graphics

So you havent selected a picture yet, or the thing you selected could not be opened as a picture.
P is nil until it contains a picture.

I get image expects class picture, but this is class window

Where? When running what code?
You are asking us to do a lot of guessing…

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.

2 Likes