gpt4 book ai didi

c# - 混叠问题

转载 作者:行者123 更新时间:2023-11-30 17:20:44 27 4
gpt4 key购买 nike

下面的代码在 Canvas 上绘制两条垂直线。这些线条在屏幕上看起来粗细不同,尽管它们在代码中是相同的。我想找到一种方法让它们看起来像 Canvas 周围的边框一样清晰。设置 Path.SnapsToDevicePixels 没有任何效果。该代码是一个人为的示例,通常绘制这些线的 Canvas 可以嵌套在可视化树的更深处。

感谢您的帮助康斯坦丁


<Window x:Class="wpfapp.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Border BorderBrush="Black"
BorderThickness="1"
Margin="10">
<Canvas x:Name="Canvas"
SizeChanged="OnCanvasSizeChanged" />
</Border>
</Grid>
</Window>

using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace wpfapp
{
public partial class MyWindow : Window
{
public MyWindow()
{
InitializeComponent();
}

private void OnCanvasSizeChanged(object sender, SizeChangedEventArgs e)
{
StreamGeometry g = new StreamGeometry();
double h = this.Canvas.ActualHeight;

using (StreamGeometryContext c = g.Open())
{
c.BeginFigure(new Point(7, 0), false, false);
c.LineTo(new Point(7, h), true, false);

c.BeginFigure(new Point(14, 0), false, false);
c.LineTo(new Point(14, h), true, false);
}
g.Freeze();

Path p = new Path();

p.Data = g;
p.SnapsToDevicePixels = true;
p.Stroke = new SolidColorBrush(Colors.Black);
p.StrokeThickness = 1;

this.Canvas.Children.Clear();
this.Canvas.Children.Add(p);
}
}
}

最佳答案

需要使用GuidelineSet:


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

Pen pen = new Pen(Brushes.Black, 1);
double h = this.ActualHeight;
double d = pen.Thickness / 2;

foreach (double x in new double[] { 7, 14 })
{
GuidelineSet g = new GuidelineSet(new double[] { x + d },
new double[] { 0 + d, h + d });

c.PushGuidelineSet(g);
c.DrawLine(pen, new Point(x, 0), new Point(x, h));
c.Pop();
}
}

关于c# - 混叠问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4034279/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com