Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I stretch bitmap in WPF without smoothing pixels

Tags:

I'm working on SEM image processing application, written in WPF. I have an image display control, derived from Canvas, which displays image & overlays using DrawingVisuals (one for each "layer"). It also implements Zoom & Pan using scale & translate transform, applied on DrawingVisuals.

When I zoom in the image to see individual pixels, they are displayed smooth, evidently using bilinear filtering to stretch the bitmap (no surprise, as WPF is rendered through Direct3D). However, for my use case, I would rather see individual pixels as sharp boxes, as usual in any image editor like Photoshop. That's why user of my app zooms the image -> to be able to operate on pixel level.

Is there such option in WPF (other than manually stretching the bitmap before displaying it)? I was not able to find anything.

thanks in advance, Zbynek Vrastil Czech Republic

like image 565
Zbynek Vrastil Avatar asked Mar 04 '10 16:03

Zbynek Vrastil


2 Answers

Finally found an answer, with some help from Experts Exchange. Class RenderOptions defines attached property BitmapScalingMode, which can be set to NearestNeighbor. So,

RenderOptions.SetBitmapScalingMode(imageDisplay, BitmapScalingMode.NearestNeighbor);

does the trick.

Zbynek Vrastil

like image 197
Zbynek Vrastil Avatar answered Oct 21 '22 16:10

Zbynek Vrastil


Hate to put a dampener on things, but if NearestNeighbor works like GDI+, then this will give you a limited success. As you increase magnification in areas of high contrast you might not get the desired results. In GDI+ you find blacks becoming blue, and whites becoming red - again I stress in areas of high contrast! If this isn't the case in WPF, think yourself lucky!

Perhaps a WCF developer could confirm that?

I've found that there are more options to consider, but I can only speak for the GDI+ Graphics class, which might be useful to someone.

Graphics graph = e.Graphics;
graph.InterpolationMode = InterpolationMode.NearestNeighbor;
graph.CompositingQuality = CompositingQuality.AssumeLinear;
graph.SmoothingMode = SmoothingMode.None;

This works for me. I think the SmoothingMode is the trick. Hope this helps someone else out there.

like image 40
Simon Miller Avatar answered Oct 21 '22 15:10

Simon Miller