Adding an Image with a TransformedBitmap (with a ScaleTransform) as Source into a canvas changes the expected size
See original GitHub issueHello, I have a project in where you can put Image
s in a Canvas
and move them around and resize them.
You can extract images from the canvas (removing an Image
from Canvas.Children) and the image “will remember” the size it had if you put the image again into the canvas, so that the user does not have to resize it again. The problem is that the image is draw with the wrong size, even though the values that I pass are correct. I checked with the debugger and they are correct at all times. It seems that the size changes when I add the image into the canvas (canvas.Children.Add(UIElement)).
I created a demo project to see if that behaviour would still be present, and it is. You can download the demo project in here: https://1drv.ms/u/s!Amb20j9mfkXDc7HKkr6n6T7Y0rU?e=ZfOn7r The link points to my OneDrive, towards a .7z file of 148KB. Inside the file you can find the source code, don’t worry, it is a small project. I included an image that you will need as source for a BitmapImage, just copy it to the working directory.
When you compile the program, all you have to do is to press the “Insert in canvas” button for an image to appear, you can move the image around and resize it with you mouse as advertised. To resize the image, just check the “Resizer” RadioButton
and click on the Image
for a ResizingBorder (a UserControl) to appear.
It is more noticeable if you resize the height.
Important places are: the Button_Click_InsertInCanvas function and Canvas_MouseLeftButtonUp function (which book-keeps the values needed to set the size of the image, you will see in the debugger that they are correct).
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:11 (5 by maintainers)
Top GitHub Comments
How could I not have realized that (┬┬﹏┬┬) @miloush , thank you so much again, I have learned a lot in these days thanks to you, and my program can be coded to completion also thanks to you, and now with simpler code, all I can feel is gratitude, thank you!!! 😭
I see. That’s because the
Image
control is being fancy and by default scales the bitmap to fit the control size. You can control that using the Stretch property: https://docs.microsoft.com/en-us/dotnet/api/system.windows.media.stretch?view=windowsdesktop-6.0should do what you want.