0

我正在尝试使用 WPF 绘制一些东西,但它不起作用。我正在寻找 WinForm Graphics 绘图方法的东西,我这样做:我有一个 class Tetrahedron,它有Point[]field 和 method Draw。我想把它画在表格上。我正在这样做:

using System;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Cg1Model;

namespace Cg1Wpf
{
    internal class Tetrahedron
    {
        public readonly ShapePoint[] Points;

        public Tetrahedron(ShapePoint[] points)
        {
            if (points.Length != 4)
                throw new ArgumentException(@"Invalid points count", "points");
            Points = points;
        }

        public ImageSource Draw()
        {
            var points = Points.Select(IsometricConvertion).ToArray();

            int pixelWidth = (int) (points.Max(p => p.X) - points.Min(p => p.X));
            int pixelHeight = (int) (points.Max(p => p.Y) - points.Min(p => p.Y));

            var bmp = new RenderTargetBitmap(pixelWidth, pixelHeight, 120, 96, PixelFormats.Pbgra32);
            for (int i = 0; i < points.Length - 1; i++)
            {
                var line = new Line
                           {
                               X1 = points[i].X,
                               Y1 = points[i].Y,
                               X2 = points[i + 1].X,
                               Y2 = points[i + 1].Y,
                               SnapsToDevicePixels = true,
                               Stroke = Brushes.Black,
                               StrokeThickness = 5,
                               Fill = Brushes.Blue
                           };
                bmp.Render(line);
            }
            return bmp;
        }

        private Point IsometricConvertion(ShapePoint point)
        {
            //TODO: change to normal isometric
            var result = new Point();

            result.X = point.X;
            result.Y = point.Y;
            return result;
        }
    }
}

ShapePoint 在哪里

public struct ShapePoint
{
    public int X { get; set; }
    public int Y { get; set; }
    public int Z { get; set; }


    public ShapePoint(int y, int x, int z) : this()
    {
        Y = y;
        X = x;
        Z = z;
    }
}

但是当我打电话给它时

private void Button_Click(object sender, RoutedEventArgs e)
{
    ShapePoint[] points =
    {
        new ShapePoint(10,20,30), 
        new ShapePoint(50,30,20),
        new ShapePoint(70,90,190),
        new ShapePoint(20,54,0)
    };
    var tetra = new Tetrahedron(points);
    Img.Source = tetra.Draw();
}

它什么也没画。我在做什么错?

4

1 回答 1

0

这就是您在 WPF 中“绘制”的方式:

<Window x:Class="MiscSamples.TetraHedron"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TetraHedron" Height="300" Width="300">
    <Polyline Points="{Binding}"
              Stroke="Black" Fill="LightGray" StrokeThickness="1"
              Stretch="Fill"/>
</Window>

代码背后:

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

public partial class TetraHedron : Window
{
    public TetraHedron()
    {
        InitializeComponent();

        DataContext = new PointCollection
            {
                new Point(10, 20),
                new Point(50, 30),
                new Point(70, 90),
                new Point(20, 54)
            };
    }
}

结果:

在此处输入图像描述

  • 简单而美观的DataBindingPointCollection. 不需要可怕的类似 winforms 的黑客攻击。
  • 你的观点肯定不像四面体。相应地修改它们。
  • 将我的代码复制并粘贴到 a 中File -> New Project -> WPF Application,然后自己查看结果。

旁注:如果您正在寻找绘制 3D 内容,WPF 具有对 3D 的内置支持。您应该阅读文档以开始使用。

于 2013-09-14T22:41:05.367 回答