locked
High Quality Image Resizing in WinRT RRS feed

  • Question

  • I have been using the following .NET resizing code that works fantastic in not WinRT apps:

    Image newImage = new Bitmap(NewWidth, NewHeight, objImage.PixelFormat);
    Graphics ig = Graphics.FromImage(newImage);
    ig.DrawImage(objImage, new Rectangle(0, 0, NewWidth, NewHeight));
    newImage.Save(string.Format(@"{0}\{1}", target, file), ImageFormat.Png);
    newImage.Dispose();
    ig.Dispose();
    objImage.Dispose();

    Unfortunately nothing that I have tried in WinRT produces a resized image w/nearly the quality I am looking for.  I have tried every example/variation of BitmapDecoder/Encoder, BitmapTransformation, WriteableBitmap, WriteableBitmapEx, etc.  If anyone has any suggestions on how to get a high-quality image resizer working in WinRT, please point me in the right direction.

    Thanks,
    Bob

    Saturday, January 4, 2014 3:18 AM

Answers

  • Hi Bob,

    You can control the quality of the resizing done by the BitmapDecoder by setting its BitmapTransform's InterpolationMode . By default this is will emphasize performance at the expense of quality, but you can change modes to emphasize quality over performance where needed.

    --Rob 

    • Marked as answer by Bob Willer Tuesday, January 7, 2014 4:53 PM
    Tuesday, January 7, 2014 3:09 AM
    Moderator

All replies

  • Hi,

    You can use DecodePixelWidth and DecodePixelHeight properties of the BitmapImage to resize your images.

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

    Refer to the disscusion:

    http://www.geekchamp.com/forums/windows-8-development/resizing-an-image-without-losing-any-quality-in-windows-store-app#forum-post-386827 

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, January 6, 2014 6:30 AM
  • Thanks Anne, even doing something that simple does not resize images properly - and I am doing this in code, not XAML (essentially uploading a file, doing a resizing, and saving the new image off)  For an example of what I am talking about, check out these images:

    The original:
    http://resizesample.azurewebsites.net/original.png

    Resized using the code from my original post using the .NET Graphics class: http://resizesample.azurewebsites.net/resized-native.png

    And finally resized using WinRT - again, it doesn't seem to matter how I try to do this:
    http://resizesample.azurewebsites.net/resized-winrt.jpg

    So I am still trying to find a method in WinRT that creates the same quality as I could get w/the full .NET library.
    Thanks,
    Bob

    Tuesday, January 7, 2014 2:55 AM
  • Hi Bob,

    You can control the quality of the resizing done by the BitmapDecoder by setting its BitmapTransform's InterpolationMode . By default this is will emphasize performance at the expense of quality, but you can change modes to emphasize quality over performance where needed.

    --Rob 

    • Marked as answer by Bob Willer Tuesday, January 7, 2014 4:53 PM
    Tuesday, January 7, 2014 3:09 AM
    Moderator
  • Bingo!  Thanks Rob, "Fant" was the answer...I missed it as none of the samples I found anywhere set InterpolationMode!

    Much Appreciated,
    Bob

    Tuesday, January 7, 2014 4:53 PM