question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding an Image with a TransformedBitmap (with a ScaleTransform) as Source into a canvas changes the expected size

See original GitHub issue

Hello, I have a project in where you can put Images 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:closed
  • Created a year ago
  • Reactions:1
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Miryunkucommented, Jul 29, 2022

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!!! 😭

1reaction
miloushcommented, Jul 29, 2022

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.0

image.Stretch = Stretch.Fill;

should do what you want.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Image added to WPF canvas changes size
I have set the canvas width/height explicitly in the XAML - no effect. I have set the size of the image explicitly -...
Read more >
Creating Image Thumbs with TransformedBitmap and ...
The code fragment below illustrates how to create thumbnails from a source image using TransformedBitmap and ScaleTransform.
Read more >
How to: Apply a Transform to a BitmapImage - WPF .NET ...
Create a BitmapImage and set it's DecodePixelWidth to 200. Use ///// ///// this BitmapImage as a source for other BitmapSource objects.
Read more >
Resizing images from the server using WPF/WIC instead of GDI+
The second way to resize is to apply a ScaleTransform on the image. In order to do that, we need to first read...
Read more >
I had a problem when it is necessary to print an Canvas ...
Hello! I had a problem when it is necessary to print an Canvas of the big size. Tried it all to convert in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found