Stretch image to fill available width / height (separately)

Is it possible to fill in the available width / height with an image in xaml? I need something like UniformToFill, but where can I control the direction of stretching (width or height)

Suppose I have the following code:

<UniformGrid Columns="2" Rows="2"> <Image Source="Desert1.jpg" Stretch="Uniform"/> // <Image Source="Desert2.jpg" Stretch="UniformToFill"/> // <Image Source="Desert3.jpg" /> <Image Source="Desert4.jpg" /> </UniformGrid> 

EDIT: for example (width): if the image is half the size of what I want to show, I'm not interested in the height and just scale the height and width of the image x2. Therefore, the image should correspond to the width and does not care about the height. This is the desired behavior, but if it is impossible, good. So you can rethink the question IF it is possible, HOW can I do it in xaml.

Also, all images may have different widths and heights.

+7
c # image wpf
source share
3 answers

I think that you can achieve the desired effect in certain conditions. If your images are larger than the size that will be displayed, you can use this:

 <Image Source="Desert.jpg" Stretch="UniformToFill" StretchDirection="DownOnly" /> 

A ViewBox has the same Stretch properties as Image , and there is a good example of the differences between the various combinations in How to: Apply stretch properties to the contents of the View window on MSDN.

+3
source share

This may be what you are looking for ...

TransformedBitmap

Here is the static method I made in the ImageUtility class.

 public static TransformedBitmap GetScaledBitmapImageSprite(BitmapSource src, double x_scale, double y_scale) { return (new TransformedBitmap(src, new ScaleTransform(x_scale, y_scale))); { 

x_scale and y_scale are doubles of the form:

required_width / original_width

It may be a little different from what you are looking for, but I think it can make you start correctly.

You can save the TransformedBitmap in memory and apply the new transforms via:

 TransformedBitmap x = new TransformedBitmap(); x.Transform = new ScaleTransform(x,y); 
+2
source share

You have to use

 <Image Source="{Binding Image}" Stretch="Fill"/> 

as if you used Stretch = "UnifrmtoFill", then it will change the length and width in the ratio, or I'll say both together.

therefore, if you use Stretch = "Fill", this gives you the ability to change the height or width at a time, which has been changed.

+1
source share

All Articles