How can I stretch bitmap in WPF without smoothing pixels

柔情痞子 提交于 2019-12-03 08:42:46

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/2381012/how-can-i-stretch-bitmap-in-wpf-without-smoothing-pixels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!