Avalonia icon indicating copy to clipboard operation
Avalonia copied to clipboard

Allow Anti-Aliasing to be disabled

Open kolbyd opened this issue 2 years ago • 4 comments

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Drawing 1-pixel thick lines results in blurry, fuzzy outputs while using DrawingContext.DrawLine(). image I believe this is due to anti-aliasing.

Describe the solution you'd like A clear and concise description of what you want to happen.

A way to disable anti-aliasing for the drawing context (which I believe is included in native WPF).

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Right now, I can add 0.5 to all my x,y values to prevent this behavior. image

But this is less than ideal as I can't add a centre line through the square without it being blurred.

Additional context Add any other context or screenshots about the feature request here.

Blurry lines:

public void Draw(double x, double y, DrawingContext drawingContext)
{
    Pen _pen = new(new SolidColorBrush(Color.FromRgb(202, 205, 169)));

    List<Point> Points = new List<Point>()
    {
        new Point(x - 4, y - 4),
        new Point(x + 4, y - 4),
        new Point(x + 4, y + 4),
        new Point(x - 4, y + 4),
        new Point(x - 4, y - 4),
    };

    for (int i = 0; i < Points.Count - 1; i++)
    {
        drawingContext.DrawLine(_pen, Points[i], Points[i + 1]);
    }
}

Non-blurry lines:

public void Draw(double x, double y, DrawingContext drawingContext)
{
    Pen _pen = new(new SolidColorBrush(Color.FromRgb(202, 205, 169)));

    List<Point> Points = new List<Point>()
    {
        new Point(x - 4.5, y - 4.5),
        new Point(x + 4.5, y - 4.5),
        new Point(x + 4.5, y + 4.5),
        new Point(x - 4.5, y + 4.5),
        new Point(x - 4.5, y - 4.5),
    };

    for (int i = 0; i < Points.Count - 1; i++)
    {
        drawingContext.DrawLine(_pen, Points[i], Points[i + 1]);
    }
}

kolbyd avatar Apr 03 '22 23:04 kolbyd

I guess this one is related to a problem I came across... I initialized a 200x200 pixel WriteableBitmap with 4 colors, I did not want a blurry edge between them. I assume anti-aliasing is doing the same here: anti-aliasing on (this is a crop of the transition region)

But in this case it would be a property on Image instead of DrawingContext.

HoneyTauOverTwo avatar Jun 27 '22 21:06 HoneyTauOverTwo

@kolbyd @HoneyTauOverTwo you can set RenderOptions.BitmapInterpolationMode property:

<Image Source="{Binding MyBitmap}" RenderOptions.BitmapInterpolationMode="LowQuality" />

It has quite confusing naming for enum values, because we copied Skia naming. https://github.com/AvaloniaUI/Avalonia/blob/d1afd3bb591c266015dcdecc9611148ca08c2ee4/src/Skia/Avalonia.Skia/SkiaSharpExtensions.cs#L16 https://github.com/AvaloniaUI/Avalonia/blob/4c77f1eb3d11308412b915a82460e4408ce360a0/src/Windows/Avalonia.Direct2D1/Media/Imaging/WicBitmapImpl.cs#L28

maxkatz6 avatar Jun 27 '22 21:06 maxkatz6

@maxkatz6 I was digging into Avalonia's code and found this BitmapInterpolationMode enum. But did not find where to set it yet.

I changed my code to this (I am not using data bindings):

<Image x:Name="DisplayImage" RenderOptions.BitmapInterpolationMode="LowQuality"/>

Then in the code behind I have:

WriteableBitmap bmp = new WriteableBitmap(new PixelSize(200, 200), new Vector(96, 96));
// bmp initialization omitted
DisplayImage.Source = bmp;

but it still produces the same result. And sorry for cluttering this here, probably not the place to discuss.

HoneyTauOverTwo avatar Jun 27 '22 22:06 HoneyTauOverTwo

Related to #199

kolbyd avatar Aug 31 '22 03:08 kolbyd