Igor
Igor

Reputation: 316

How to display pixel-perfect bitmap on screen in WPF?

I need to display basic geometric shapes (line, square, ellipse, ...) in the corner of the screen with pixel precision. This means that if the line is 5 pixels long in the application, I need it to have the same number of pixels on the screen. The reason for this is that such an image is streamed to the video processing (HDMI) and it then continues to work with the individual pixels.

So far I have a WPF application that draws geometric shapes using WriteableBitmapEx and then I render this bitmap to Canvas using OnRender

protected override void OnRender(DrawingContext drawingContext)
{
    base.OnRender(drawingContext);

    if (_bitmap is null) return;

    var width = _bitmap.PixelWidth;
    var height = _bitmap.PixelHeight;

    drawingContext.DrawImage(_bitmap, new Rect(0, 0, width, height));
}

For the Canvas I set RenderOptions.BitmapScalingMode="NearestNeighbor" and RenderOptions.EdgeMode="Aliased"

Also, the Canvas size matches the bitmap size.

But, the result is not good: this is what I draw

enter image description here

the bitmap itself is OK but the output on the screen is this

enter image description here

Looks like some lines are doubled based on their position. Any idea why and how to correct it?

UPDATE:

On different PC the same image looks like this

enter image description here

Upvotes: 0

Views: 63

Answers (0)

Related Questions