我正在尝试使用 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();
}
它什么也没画。我在做什么错?