【问题标题】:Draw polygon with coordinates用坐标绘制多边形
【发布时间】:2016-02-07 05:47:49
【问题描述】:

我正在尝试使用坐标绘制多边形,听起来很简单,但是有一个问题,它没有绘制。我正在使用 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:“颜色”没有“黑色”的定义

有解决这个问题的想法吗?谢谢!

【问题讨论】:

  • 嗯...不是 PaintEventArgs 用于 Windows 窗体吗?在 WPF 中,一个不会绘制
  • 是的,我想是的,我可以在 WPF 中使用什么来代替?画布?

标签: c# coordinates draw polygon paintevent


【解决方案1】:

我可以在 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(因为上面的代码引用了 XAML 中名为“_image”元素的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

告诉我更多

【讨论】:

  • 工作!非常感谢!
  • @user3558419 很高兴听到。不要忘记投票,如果您认为可以标记为答案。它有助于支付账单;)玩得开心 WPF'ing!
猜你喜欢
  • 2012-08-19
  • 2017-12-11
  • 2016-05-21
  • 1970-01-01
  • 2021-12-06
  • 1970-01-01
  • 2013-04-04
  • 1970-01-01
  • 2012-07-05
相关资源
最近更新 更多