1

我正在尝试用坐标绘制多边形,听起来很简单,但是有一个问题,它没有绘制。我正在使用 Microsoft Blend for Visual Studio 14。

微软 .NET 框架 4.6.01055

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

    public void DrawPolygon(
        Pen pen,
        Point[] points
        )
    { 

        public void DrawPolygonPoint(PaintEventArgs e)
        {
               //Creer Pen
               Pen blackPen = new Pen(Color.black, 3);

               //Creer points polygones
               Point point1 = new Point(10, 10);
               Point point2 = new Point(13, 11);
               Point point3 = new Point(15, 30);
               Point point4 = new Point(17, 10);
               Point point5 = new Point(20, 10);
               Point point6 = new Point(30, 15);
               Point point7 = new Point(30, 30);
               Point point8 = new Point(60, 40);
               Point point9 = new Point(65, 55);
               Point point10 = new Point(40, 60);
               Point point11 = new Point(40, 65);
               Point point12 = new Point(58, 70);
               Point point13 = new Point(60, 60);
               Point point14 = new Point(90, 60);
               Point point15 = new Point(90, 85);
               Point point16 = new Point(70, 61);
               Point point17 = new Point(60, 85);
               Point point18 = new Point(30, 85);
               Point point19 = new Point(12, 80);
               Point point20 = new Point(12, 78);
               Point point21 = new Point(16, 75);
               Point point22 = new Point(13, 68);
               Point point23 = new Point(17, 65);
               Point point24 = new Point(6, 62);
               Point point25 = new Point(16, 60);
               Point point26 = new Point(28, 56);
               Point point27 = new Point(27, 45);
               Point point28 = new Point(15, 32);
               Point point29 = new Point(15, 50);
               Point point30 = new Point(5, 50);
               Point point31 = new Point(10, 40);

               Point[] curvePoints =
               {
                    point1,
                    point2,
                    point3,
                    point4,
                    point5,
                    point6,
                    point7,
                    point8,
                    point9,
                    point10,
                    point11,
                    point12,
                    point13,
                    point14,
                    point15,
                    point16,
                    point17,
                    point18,
                    point19,
                    point20,
                    point21,
                    point22,
                    point23,
                    point24,
                    point25,
                    point26,
                    point27,
                    point28,
                    point29,
                    point30,
                    point31

                };

                 //Dessiner polygone
                 e.Graphics.DrawPolygon(blackPen, curvePoints);
      }
}
}

3 产生错误。我认为主要的是

CS1061:对象没有“DrawPolygon”的定义。

CS0117:“颜色”没有“黑色”的定义

关于解决这个问题的任何想法?谢谢!

4

1 回答 1

0

我可以在 WPF 中改用什么?帆布?

WPF 确实有画布,但我通常将它们用于其他用途。我们确实有一个Image可以间接绘制的。Image几乎可以在 XAML 中的任何位置放置。

您可以渲染到RenderTarget本质上是一个离屏缓冲区。其输出可用作 的源的输入Image

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

    private void Window_Loaded(object sender, RoutedEventArgs e)
    { 


        Point[] curvePoints =
        {
            new Point(10, 10),
            new Point(13, 11),
            new Point(15, 30),
            new Point(17, 10),
            new Point(20, 10),
            new Point(30, 15),
            new Point(30, 30),
            new Point(60, 40),
            new Point(65, 55),
            new Point(40, 60),
            new Point(40, 65),
            new Point(58, 70),
            new Point(60, 60),
            new Point(90, 60),
            new Point(90, 85),
            new Point(70, 61),
            new Point(60, 85),
            new Point(30, 85),
            new Point(12, 80),
            new Point(12, 78),
            new Point(16, 75),
            new Point(13, 68),
            new Point(17, 65),
            new Point(6, 62),
            new Point(16, 60),
            new Point(28, 56),
            new Point(27, 45),
            new Point(15, 32),
            new Point(15, 50),
            new Point(5, 50),
            new Point(10, 40)
        };

        var pointCollection = new PointCollection(curvePoints);
        var polygon = new Polygon
        {
            Stroke = Brushes.GreenYellow,
            StrokeThickness = 1,
            Fill = Brushes.Blue,
            Points = pointCollection
        };

        const int cx = 800;
        const int cy = 600;
        polygon.Measure(new Size(cx, cy)); 
        polygon.Arrange(new Rect(0, 0, cx, cy)); 


        RenderTargetBitmap bmp = new RenderTargetBitmap(cx, cy, 120, 96, PixelFormats.Pbgra32);
        bmp.Render(polygon);

        _image.Source = bmp;

    }
}

假设您有这样的 XAML(因为上面的代码引用Image了 XAML 中名为“_image”的元素:

<Window x:Class="WpfPolygon1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfPolygon1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <StackPanel>
            <Image x:Name="_image"/>
        </StackPanel>

    </Grid>
</Window>

现在,您不必像我一样使用 XAML,可以在运行时随意创建StackPanelImage编程!

告诉我更多

于 2016-02-07T06:54:41.723 回答