Welcome to Pete Brown's 10rem.net

First time here? If you are a developer or are interested in Microsoft tools and technology, please consider subscribing to the latest posts.

You may also be interested in my blog archives, the articles section, or some of my lab projects such as the C64 emulator written in Silverlight.

(hide this)

More on Image Resizing in .NET 4 vs .NET 3.5sp1: Bilinear vs Fant

Pete Brown - 16 May 2010

In a previous post, I demonstrated how to use RenderOptions.BitmapScalingMode in WPF to change the quality of the resize of an image. Some folks have questioned why that is necessary when the resizing worked just fine in 3.5sp1

In .NET 4, the default BitmapScalingMode changed from Fant to bilinear for performance reasons. The Fant scaler is CPU-intensive whereas the now default Bilinear is handled by hardware.

The net result is a move from 3.5sp1 to .NET 4 will show, in many cases, decreased quality but increased performance for resized bitmaps. The actual result depends on how much resizing you are doing (resizing to less than 30-50% of size will generally show artifacts).

To reset to Fant, you can set the RenderOptions.BitmapScaling mode in the root of each window. The image below shows a comparison between the two scaling modes for two fairly large images. The first scaling is down to 400px widthfrom 3008x2000 fro the police line image and 3374x2257 for the roses image: both are pretty significant resize options.

(view full size to see the comparison)

image

 

These close-ups show how bilinear really starts to break down at small sizes. The 50x50 image can still be understood in fant, but in bilinear - not so much.

image

Another option, of course, is to use images that are closer to the resolution you require. When you resize to something closer to the source resolution, the hardware resize does a passable job.

Keep in mind that this will also affect ASP.NET applications that use WPF server-side for image resizing (an increasingly common practice)

       
posted by Pete Brown on Sunday, May 16, 2010
filed under:        

9 comments for “More on Image Resizing in .NET 4 vs .NET 3.5sp1: Bilinear vs Fant”

  1. Szymonsays:
    Could you tell us what is the best practice when doing animation with scaled images?

    I experimented with switching the RenderOptions.BitmapScaleMode to LowQuality when animation starts and then restored to HighQuality when it finished. I wonder if this is necessary at all or WPF does something similar under the hood on its own (like it does with scaled text)?
  2. Petesays:
    @Szymon

    Are you animating the scale or just the position? If just the position, then you can cache the scaled bitmap using the cachemode settints, but often times that is not necessary unless you have done something else to the image, like scale it.

    If you are animating the scale, you can cache the largest size (using the renderatascale setting), assuming it is significantly smaller than the original size. From there, whether you chose bilinear or fant is going to depened on your own tradeoff tolerance for quality vs. perf.
  3. Ericsays:
    Pete - How does this apply to something like creating a RenderTargetBitmap from a VisualBrush? We are generating thumbnails of our windows by creating a DrawingVisual, drawing a VisualBrush on it, and then rendering the DrawingVisual to a RenderTargetBitmap. The quality of the thumbnails dropped dramatically from WPF 3.5 SP1 to WPF 4.0. I tried setting BitmapScalingMode.Fant on all of the following objects that are part of this process:

    the DrawingVisual
    the VisualBrush
    the RenderTargetBitmap
    the Window that the thumbnails eventually show up in

    all to no avail. How to I get the quality of these bitmaps to match WPF 3.5?

    Thanks,

    Eric
  4. Nathanael Jonessays:
    <p>You might be interested in this project (for both ASP.NET and WinForms, .NET 2, 3, 3.5, and 4).</p>

    <a href="http://imageresizing.net">Image Resizing In .Net</a>

    <p>It has a one-line API, can be used as a web service or as a class library, and supports a wide range of image processing actions.</p>

    <p>For example, you can serve a dynamically resized image by just adding a querystring: &lt;img src="image.jpg?width=200&height=200" /></p>

  5. Nathanael Jonessays:
    Sorry folks, I guessed wrong about the comment syntax :) Feel free to delete the previous comment.

    You might be interested in this project (for both ASP.NET and WinForms, .NET 2, 3, 3.5, and 4).

    http://imageresizingin.net

    It has a one-line API, can be used as a web service or as a class library, and supports a wide range of image processing actions.

    For example, you can serve a dynamically resized image by just adding a querystring: <img src="image.jpg?width=200&height=200" />

Comment on this Post

Remember me