2

I have read this question, High Quality Image Scaling Library , but it only pertains to GDI+.

When I scale down an Image in my app, it becomes very jaggy.

I can't find any property on the Image class that allows me to increase the quality of the down-scaling.

How can I do this?

EDIT:

I have now used this code:

BitmapImage img = new BitmapImage();
img.UriSource = new Uri(Graphic, UriKind.Relative);
return img;

where Graphic is a string that contains the path to the image, but am getting this exception:

An exception of type 'System.ArgumentException' occurred in Weather.DLL but was not handled in user code

Additional information: The given System.Uri cannot be converted into a Windows.Foundation.Uri. Please see http://go.microsoft.com/fwlink/?LinkID=215849 for details.

As far as I know, Windows.Foundation.Uri isn't used in the .net framework.

So why is it giving me this error?

Community
  • 1
  • 1
annonymously
  • 4,708
  • 6
  • 33
  • 47
  • Which Image class are you talking about? – Doctor Jones Oct 18 '12 at 07:57
  • What factor are you downscaling the image? –  Oct 18 '12 at 08:14
  • Can you please post your code? – Paolo Moretti Oct 18 '12 at 08:18
  • @PaoloMoretti My code is basically just an `` tag that sets a smaller width and height. – annonymously Oct 18 '12 at 08:20
  • @DoctaJonez [Windows.UI.Xaml.Controls.Image](http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.image.aspx) – annonymously Oct 18 '12 at 08:23
  • 2
    I don't think you can do it in XAML. WPF has an attached property [`RenderOptions.BitmapScalingMode`](http://msdn.microsoft.com/en-us/library/system.windows.media.renderoptions.bitmapscalingmode.aspx) to change the interpolation mode, but this is not available in WinRT. You probably have to create a new scaled image in the code-behind. [This post](http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/4e49ec9e-db56-4155-a296-91c1ba54d84b) might help you. – Paolo Moretti Oct 18 '12 at 08:47
  • @PaoloMoretti you should add that as an answer – Doctor Jones Oct 18 '12 at 09:02
  • I guess I just have to ask; why are you down scaling? – Jerry Nixon Oct 18 '12 at 16:16
  • @JerryNixon Because I have large images which need to be displayed as small thumbnails as well as full images. – annonymously Oct 18 '12 at 20:30

3 Answers3

10

In XAML:

    <Image Width="250" Height="250" Stretch="UniformToFill">
        <Image.Source>
           <BitmapImage UriSource="{Binding ImagePath}" DecodePixelHeight="250"/>
        </Image.Source>
    </Image>

In C#:

    BitmapImage bitmapImage = BitmapImage("URL HERE");
    bitmapImage.DecodePixelHeight = 250;

Only set DecodePixelHeight or DecodePixelWidth.

http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.decodepixelheight.aspx

Ro R
  • 101
  • 2
0

I ended up creating a second, smaller image and using it in all the places where I needed to scale it down.

annonymously
  • 4,708
  • 6
  • 33
  • 47
0

You might be able to avoid the problem you're running into entirely by using the built-in support for image scaling:

http://msdn.microsoft.com/en-us/library/windows/apps/hh465362.aspx

You can include multiple versions of your images at different resolutions (same filename but either with a known suffix or in a known folder), and the system will automatically load the appropriate one for the DPI of the user's device.

Travis
  • 956
  • 5
  • 11