用坐标绘制多边形

时间:2016-02-07 05:47:50

标签: c# coordinates draw polygon paintevent

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

Microsoft .NET Framework 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:'颜色'没有' black'

的定义

有关解决此问题的任何想法?谢谢!

1 个答案:

答案 0 :(得分:0)

  

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

WPF确实有画布,但我通常将它们用于其他方面。我们确实有一个Image我们可以间接绘制。 {1}}几乎可以在XAML中的任何位置进行拼写。

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

Image

假设您有这样的XAML(因为上面的代码引用了XAML中名为" _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; } } 元素:

Image

现在,您不必像我一样使用XAML,可以在运行时自由地创建<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> StackPanel

告诉我更多