DrawingContext.DrawLine:笔没有完全不透明吗? [英] DrawingContext.DrawLine: Pen has no full opacity?

查看:194
本文介绍了DrawingContext.DrawLine:笔没有完全不透明吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我绘制类似的东西时(这里只是随机绘制):

when I draw something like that (just random drawings here):

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DrawingVisual visual = new DrawingVisual();
        DrawingContext context = visual.RenderOpen();

        Pen pen = new Pen(Brushes.Black, 1);

        context.DrawEllipse(Brushes.YellowGreen, pen, new Point(0,0), 40, 40);

        for (int i = 0; i <= 100 - 1; i++)
          context.DrawLine(new Pen(Brushes.Black, 1), new Point(0, i), new Point(i, i));

        context.Close();

        RenderTargetBitmap bmp = new RenderTargetBitmap(100, 100, 96, 96, PixelFormats.Pbgra32);

        bmp.Render(visual);
        image1.Source = bmp;
    }
}

DrawLine和DrawEllipse混合的颜色.(我发现,只有使用笔的DrawLine才会出现,而使用画笔的其他形式(例如Rectangle和Ellipse则不会)).即使来自基础网格背景(argh)的LinearGradientBrush中的颜色也很奇怪. 我希望它们按z顺序具有完全不透明性.

the colors of DrawLine and DrawEllipse mix. (I figured out that it's only with DrawLine which uses a pen, and not with other forms like Rectangle and Ellipse, that use a Brush). Strangely even with colors from the LinearGradientBrush of a underlying Grids' Background (argh). I would like them to be z-Ordered with full opacity each.

以下是XAML代码:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Image Name="image1" Stretch="None" />
</Window>

感谢您的帮助.

推荐答案

RenderTargetBitmap有两个抗锯齿或亚像素化的问题:

There are two issues of antialiasing, or sub-pixeling, with RenderTargetBitmap:

1.

禁用位图本身的子像素化(例如,在UIElement中渲染时).这可以通过以下方法解决:

Disabling sub-pixeling for the bitmap itself (for example, when it is rendered within a UIElement). this is resolved by applying:

    RenderOptions.SetBitmapScalingMode(image1, BitmapScalingMode.NearestNeighbor);

(其中image1是WPF图像对象).

(where image1 is the WPF Image object).

仅.NET 4及更高版本支持. 在您的特定情况下,线是逐像素渲染的,都没关系.

It is only supported for .NET 4 and above. In your specific case it doesn't matter as the lines are rendered pixel-after-pixel.

2.

在渲染为RenderTargetBitmap时禁用子像素化.可以通过方法RenderOptions.SetEdgeMode和EdgeMode.Aliased的参数值来实现.

Disabling sub-pixeling when rendering INTO the RenderTargetBitmap. It can be achieved by the method RenderOptions.SetEdgeMode with the parameter value of EdgeMode.Aliased.

但是,该方法仅在以下情况下有效:

HOWEVER, this method will work only if:

  • 为DrawingGroup对象调用该方法.

  • The method is called for a DrawingGroup object.

仅通过常规图形合成嵌套抗锯齿的几何体(例如,如果DrawingGroup包含一个矩形,而VisualBrush封装了DrawingVisual,则即使使用了该方法,该DrawingVisual的内容也将被抗锯齿). /p>

The antialiased geometry is nested only through regular drawing composite (for example, if the DrawingGroup contains a rectangle with a VisualBrush encapsulates a DrawingVisual, the content of that DrawingVisual will be antialiased even if you used that method).

因此,您可以按以下方式重写代码:

Thus you can rewrite your code as follows:

    DrawingVisual visual = new DrawingVisual();
    DrawingGroup group = new DrawingGroup();

    Pen pen = new Pen(Brushes.Black, 1);

    group.Children.Add(new GeometryDrawing(Brushes.YellowGreen, pen, new EllipseGeometry(new Point(0,0), 40, 40)));

    for (int i = 0; i <= 100 - 1; i++)
      group.Children.Add(new GeometryDrawing(null, new Pen(Brushes.Black, 1), new LineGeometry(new Point(0, i), new Point(i, i))));

    RenderOptions.SetEdgeMode(group, EdgeMode.Aliased);

    DrawingContext context = visual.RenderOpen();
    context.DrawDrawing(group);
    context.Close();

    RenderTargetBitmap bmp = new RenderTargetBitmap(100, 100, 96, 96, PixelFormats.Pbgra32);

    bmp.Render(visual);
    image1.Source = bmp;

这篇关于DrawingContext.DrawLine:笔没有完全不透明吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆