ispiro
ispiro

Reputation: 27633

How to Resize an image on an Image Control?

I’m trying to resize an image on a UWP Image control (XAML) using

ScaleTransform t = (ScaleTransform)image.RenderTransform;

But am getting an error:

Unable to cast object of type 'Windows.UI.Xaml.Media.MatrixTransform' to type 'Windows.UI.Xaml.Media.ScaleTransform'.

So how do I resize it (not using the Stretch property)?

Upvotes: 1

Views: 1215

Answers (2)

Robert Graves
Robert Graves

Reputation: 2320

The existing RenderTransform is of type MatrixTransform which cannot be cast to a ScaleTransform.

You can either replace the existing MatrixTransform with a new ScaleTransform:

image.RenderTransform = new ScaleTransform(2, 2);

or you can update the existing MatrixTransform with the desired scale:

(image.RenderTransform as MatrixTransform).Matrix = new MatrixTransform(2, 0, 0, 2, 0, 0);

Upvotes: 3

Clemens
Clemens

Reputation: 128013

Assign a new ScaleTransform to the RenderTransform property once:

image.RenderTransform = new ScaleTransform();

Now you can later safely access it by

var t = (ScaleTransform)image.RenderTransform

Upvotes: 2

Related Questions