3

我正在使用 WFP 构建应用程序 C#,我正在尝试绘制形状,每个形状都属于一个类,并且此类包含一个用于外部形状的 Polygon,以及 1 到 3 条折线以使其看起来像真正的 2D 对象,这个方式我可以动态更改整个形状的某些属性(颜色,可见性等)我必须说一些折线是根据所需的高度和宽度由循环创建的

但是现在我正面临着一些折线渲染的问题我希望最终结果看起来像位图像素,没有阴影、模糊或边缘效果。

这是一个例子(面板是给网格的名称)

 public partial class Window1 : Window {

    private Polyline zigzag;

    public Window1() {
        InitializeComponent();
        PointCollection points = new PointCollection();
        ArrayList axisX = new ArrayList();
        ArrayList axisY = new ArrayList();

        zigzag = new Polyline();
        zigzag.Stroke = Brushes.Black;

        int count = 1;
        Boolean increase = true;
        //the number 60 in this loop is to represent the width of the main shape
        for (int p = 3; p < 60 - 3; p++) {
            if (count == 1) {
                axisX.Add(p);
                axisY.Add(5);
                increase = true;
            }
            if (count == 4) {
                axisX.Add(p);
                axisY.Add(2);
                increase = false;
            }
            if (increase) {
                count++;
            }
            else {
                count--;
            }
        }

        for (int i = 0; i < axisX.Count; i++) {
            //the number 10 is to represent the position where the Poliline is to be placed
            int tmpx = 10 + (int)axisX[i];
            int tmpy = 10 + (int)axisY[i];
            points.Add(new Point(tmpx, tmpy));
        }

        this.zigzag.Points = points;
        RenderOptions.SetEdgeMode(Panel , EdgeMode.Aliased);
        Panel.Children.Add(zigzag);

    }

}

The picture shows the drawn zigzag on above, and the way it should look like below

enter image description here

4

1 回答 1

2

The coordinate system has its origin on the top left corner of the top left pixel. To hit the pixel in the middle, you must specify coordinates like 3.5 etc.

I shortened your code a little, I hope you don't mind. (Still does the same, just less lines)

PointCollection points = new PointCollection();

zigzag = new Polyline();
zigzag.Stroke = Brushes.Black;

for (int i = 1; i < 60 - 3; i = i + 3)
{
    points.Add(
        new Point(
            10.5f + i,
            10.5f + (i % 2 == 0 ? 2 : 5)
         ));
}

this.zigzag.Points = points;
RenderOptions.SetEdgeMode(Panel1, EdgeMode.Aliased);
Panel1.Children.Add(zigzag);

I increased the translation from 10 to 10.5 in both directions. The fractional part should be 0.5 to indicate the center of the pixel.

于 2013-03-29T16:16:37.890 回答