Hi,
I have a Canvas with a width set to 800 (pixels ?) in the IDE
When I draw a picture of 800 pixels of width it doesn’t fill the full width of the canvas.
Can anyone explain please?
TIA
Hi,
I have a Canvas with a width set to 800 (pixels ?) in the IDE
When I draw a picture of 800 pixels of width it doesn’t fill the full width of the canvas.
Can anyone explain please?
TIA
Can you give us the code you’re using; big help to diagnosis it.
At a guess, setting the Canvas (or any Xojo control) width to say 800 (points, not pixels), on a 4K screen translates to 1600 pixels. So your actual picture size may need to be resized to compensate.
You could try multiplying the picture’s height & width by the ScaleFactor to help keep things relative.
Very simply:
1 - Open an image
Var f As FolderItem
f = FolderItem.ShowOpenFileDialog("")
If f <> Nil Then
MyImage = Picture.Open(f)
// Draw the Image
Canvas_PLAN.Refresh
End If
2 - In the paint hadler of the Canvas
g. DrawPicture(MyImage,dx, dy)
FYI, I wonder if there is any possibility related to the resolution of the image (300 dpi here)
Yes, agree with Scott. As an aside is your image height also the same as the canvas height?
Sorry Scott I didn’t see your answer earlier.
I will try that and come back to you.
Thank you vry much
Are you on Windows?
No, Mac OS
Image Width : 1000 pixels
Canvas Width : 1000 pixels
ScaleFactor : 2
Once the image loaded on the canvas I took a sceenshot of the image represented. It has a width of 492 pixels
A screenshot of my canvas has a width of 2000 pixels. Which could explain the ScaleFactor of 2.
But I can’t understand the Image representation in the canvas being 492 pixels
I would use a picture twice the size of the nominal canvas size and scale it to the canvas size in the DrawPicture call. Your picture appears to be too small for a Retina/HighDPI display.
Ah, sometimes it’s a DPI thing, but that’s usually a Windows problem.
From where do you take this value ?
Some software returns pixel values, some other correctly return the points values.
The difference is in the image dpi (72 dpi vs say 300 dpi, but remember the Windows default is 96 dpi - vs 72 in macOS -, that may explain your numbers…).
In your first post, it sounded like you were trying to enlarge your image to fit the canvas, which is why I suggested multiplying by the ScaleFactor.
But I guess we don’t know what you’re using for an image in the first place, so we can’t answer why your image is 492 pixels.
Anyway, I’ve put together a demo project from a utility project I have to help illustrate the relationship between canvas size, picture size & scaling. The zip includes two example images at 400x400 pixels in different DPI’s.
ImageScaling.xojo_binary_project.zip (49.4 KB)
Basically, in a canvas, an unscaled image (e.g., 400x400 pixels) will show as 400x400 screen “points” and have a lower resolution. If you have a high-res screen, then apply the ScaleFactor checkbox to display the image as it would appear for high resolution purposes which makes the image smaller (reduced number of screen points/pixels).
Just drag-and-drop a PNG or JPG onto the canvas and see what you get.
By the way, from what little I understand of image files on macOS, the DPI makes no difference in the size of the image you show in the canvas (though it may affect the quality).
My apologies if I’m not explaining this very well, but I hope the example project helps.
Edited to add: This high-res/smaller-image relationship is why the Image Set Editor in the IDE gives you three size options for adding icons and other pictures to your project. The higher the screen resolution, the larger (in pixels) image you need - to keep the image “relative” in size to your app.
to stretch a picture on canvas in the paint event you can use
g.DrawPicture pic,0,0,g.Width,g.Height,0,0,pic.Width,pic.Height
Does this maintain the aspect ratio?
No, you have to compute either the width or height. Usually, you’d take the same width or height (whichever is greatest) and compute the other dimension.
Sorry for the silence, as a new member I was blocked from posting for few hours…
Emile,
To determine the size of my picture I used Preview and Affinity Photo on Mac and they both give me the same values.
By using external methodes I determined the width and height of my picture in pixels (Preview and Affinity Photos).
When I define the size of a Canvas in the IDE is it in Pixels or in Points. Which I understand is different (sorry, I begin with programming).
No, you have to take care of that yourself. I’m using code like this in the Paint event handler to scale a picture to the available space within a Canvas:
if not(thePict is nil) then
var theWidth as Integer = thePict.Width
var theHeight as Integer = thePict.Height
var ScaleFactor as Double = Min(g.Width/theWidth, g.Height/theHeight)
g.DrawPicture thePict, 0, 0, theWidth * ScaleFactor, theHeight * ScaleFactor, 0, 0, theWidth, theHeight
end if
Do you account the dpi ?