Resize an image to a canvas size question

Hello All.

Just a little “best way” question, that hopefully someone can point me in the right direction with.

I have a working procedure that, when I click a row in a listbox, paints an image into a canvas. Works fine.

However, if the picture is big (4000 x 1200) it takes a couple of seconds to load. It looks fine, but I am wondering if there is a faster way to get the image to display. I can resize the image before displaying, but it looks wrong. Either I get a distorted image or not all of it shows.

Is there a good way to do this? I’ve gone through the examples provided but it still isn’t “instantaneous.”

Regards

Well, how does your code look like? What have you tried? Do you calculate the aspect ratio correctly?

dim xScale as Double = me.Width/Picture1.Width
dim yScale as Double = me.Height/Picture1.Height

dim theScale as Double = Min(xScale, yScale)

g.DrawPicture(Picture1, 0, 0, Picture1.Width * theScale, Picture1.Height * theScale, 0, 0, Picture1.Width, Picture1.Height)

You didn’t say which OS you are using. The fastest way to resize images on macOS is CGImageSourceMBS.CreateThumbnailMT. If you load images from the hard disk you could try OpenCV.

Just use the image viewer.
See example.
myimageviewer.xojo_binary_project.zip (9.2 KB)

Hi Beatrix:

Here is my code…

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

var picLocal as Picture
var picCompare as Picture

var destWidth as integer = Canvas1.Width
var destHeight as integer = Canvas1.Height

var scaledLocalPic as New Picture (destWidth,destHeight)
var scaledComparePic as New Picture (destWidth, destHeight)

If f = NIL or f2 =  Nil Then
  
  MessageBox ("One of the images is a nil")
  Return
  
else
  
  picLocal = Picture.Open(f)
  picCompare = Picture.Open(f2)
  
  
  scaledLocalPic.Graphics.DrawPicture(picLocal, 0,0, destWidth, destHeight, 0, 0, picLocal.Width,picLocal.Height)
  scaledComparePic.Graphics.DrawPicture(picCompare,0,0,destWidth,destHeight,0,0, picCompare.Width,picCompare.Height)
  
end

Canvas1.Backdrop = scaledLocalPic
Canvas2.Backdrop = scaledComparePic

I think I see something, but I am going to keep my word hole closed until I do a little more analysis.

Regards

Thanks for this Rudolf. I will give it a look and see if it helps.

What Beatrix said … You need to take into account that the aspect ratio of the picture may not be the same as the aspect ratio of the canvas, still you need to scale the width and height of the image by the same factor so it doesn’t get distorted. To achieve this you calculate the scaling factors that the width and height require and use the smaller of the two for scaling the picture. Beatrix’ code does just that.

The error lies in the use of the Backdrop.

Draw the image(s) in the Graphics Event: there you can resize the Picture (or Add a new Picture, draw yours there and put that Picture to the backdrop)…

Hi All.

Ok. I found my issue. And as usual (for me) it was using the wrong variable.

I created a variable for the resized image, but used the one BEFORE it was resized to fill the canvas.

Thank you for your time.

Regards