SeToY
SeToY

Reputation: 5895

Only resize large images

I've got a bunch of (very) large images in my application. For them to fit into my ImageView and the corresponding Image control, I'm using the following XAML:

<Image Name="imageEdit" Stretch="Uniform" />

Now this works marvelously when using large images, as they're automatically scaled down. However, when displaying small images they're being scaled higher and thus become blurry.

Is there a way I can prevent that from happening and only have those images resizes, that would otherwise actually reach behind the boundaries of the Image control?

Upvotes: 1

Views: 1115

Answers (3)

Mark Vincze
Mark Vincze

Reputation: 8033

In WPF there is a property just for this reason called StretchDirection, setting it to DownOnly does exactly what you'd like to achieve.

Upvotes: 9

Matt
Matt

Reputation: 2682

As a workaround, you may want to try setting BitmapScalingMode.

Also take a look at this SO question discussing blurry WPF images.

Upvotes: 0

ChrisF
ChrisF

Reputation: 137108

You could bind the image size (dimensions) to the Stretch property through a converter.

Then in the converter return Stretch.None if the image size is less than or equal to the space available and Stretch.Uniform if it's larger. For example (just checking the X dimension):

public class SizeToStretchConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (int)value <= targetWidth ? Stretch.None : Stretch.Uniform;
    }

    public object ConvertBack(object value, Type targetType, object parameter,CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Upvotes: 2

Related Questions