Resize an image selected from folderItem.ShowOpenFileDialog

Hi All.

I know this is really easy, but for some reason I’m … missing it.

I want to scale an image AFTER it is selected with a folderItem.ShowOpenFileDialog.

Var f As FolderItem = FolderItem.ShowOpenFileDialog("")
var f2 as FolderItem = FolderItem.ShowOpenFileDialog("")

If f <> Nil and f2 <> Nil Then
  app.pic1 = Picture.Open(f)
  app.pic2 = Picture.Open(f2)
  
  Canvas1.Backdrop = app.pic1
  Canvas2.Backdrop = app.pic2
  
  // Assuming you've loaded or created your pictures...
  
  If areSimilar(app.pic1, app.pic2, 10) Then
    
    MessageBox "Same"
    
  else
    
    MessageBox "Different"
    
  End If
end if

I get the image fine, and have verified it so stepping through.
Normally, under the paint event of a canvas I can use

g.DrawPicture (canvas1.Backdrop, 0, 0, 183, 195, 0, 0, canvas1.Backdrop.Width, canvas1.Backdrop.Height)

but since it is firing when the program opens (the paint event) I get an (expected) NilObjectException.

How do I get around this?

Regards

Decouple the logic from the UI. Pictures don’t need a Canvas to operate.

// Nil check and be a little more helpful
if f = nil or f2 = nil then
  MessageBox("One of the FolderItems was nil")
  return
  
end

// Do not use global properties!
var picLocal as Picture = Picture.Open(f)
var picCompare as Picture = Picture.Open(f2)

// Find out if these are good
if areSimilar(picLocal, picCompare, 10) then
  MessageBox("Same")
  
else
  MessageBox("Different")
  
end

You’ll want to do something like this to scale the image into a new picture.

dim inputPic as Picture = Picture.Open(f)

dim destWidth as integer = 100
dim destHeight as integer = 100

dim scaledPic as new Picture(destWidth,destHeight)

scaledPic.Graphics.DrawPicture(inputPic, 0, 0, destWidth, destHeight, 0, 0, inputPic.Width, inputPic.Height)

Can you use:

if canvas1.backdrop <> nil then
   g.DrawPicture (canvas1.Backdrop, 0, 0, 183, 195, 0, 0, canvas1.Backdrop.Width, canvas1.Backdrop.Height)
end if

to not try to draw when Backdrop is Nil?

You may need to test the code as I don’t think the result is what you expect.

In API2, the parameters are Doubles (100.9) not integers (100).

And you to not take into account the Width and Height (the image can be distorded.

Yes. It was a basic example that can be expanded upon.

thank you for the response Tim, but how does this code show the pictures in the Canvas’s I have for their display?

I am showing each picture in a Canvas (if I failed to mention earlier, I apologize..) so the user can see them.

Regards

Where in Canvas1 do you want to display it ?

if in Backdrop (wheree your image loading code is…):
Canvas1.Backdrop = scaledPic

if in g (Paint Event):
g.Draw Picture scaledPic,0,0

I learned Xojo downloading source code to see how they were made. You can go on my website and download the app “GalleryCaptShot” . Once launch, select the menu “Capture - Bring first” and then the menu “Capture - Choose file”. (On Mac you can make a screencapture but not under Windows).
If you want to see to see the code, on my website select the menu “Developper - Xojo source” then download the “GalleryCaptShot” and “Zx_External” source. Unzip and open “GalleryCaptShot 1.5.0.xojo_binary_project”.

Sure I’m not the best teacher to show Xojo code, but it may help you for the first steps.

Hi Emile.

Thanks for the response.

After digging through some of the responses, I found what you just told me. But, once again, thank you ever so much for taking the time to respond!

Regards