gpt4 book ai didi

c# - 在WPF中高效绘制二维网格

转载 作者:太空宇宙 更新时间:2023-11-03 11:14:45 26 4
gpt4 key购买 nike

这是一个 Windows 窗体程序,它绘制了一个随机着色为黑色或红色的二维正方形网格:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Forms_Panel_Random_Squares
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

Width = 350;
Height = 350;

var panel = new Panel() { Dock = DockStyle.Fill };

Controls.Add(panel);

var random = new Random();

panel.Paint += (sender, e) =>
{
e.Graphics.Clear(Color.Black);

for (int i = 0; i < 30; i++)
for (int j = 0; j < 30; j++)
{
if (random.Next(2) == 1)
e.Graphics.FillRectangle(
new SolidBrush(Color.Red),
i * 10,
j * 10,
10,
10);
}
};
}
}
}

生成的程序看起来像这样:

enter image description here

这是对每个正方形使用 Rectangle 对象的(简单的)WPF 转换:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WPF_Canvas_Random_Squares
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

Width = 350;
Height = 350;

var canvas = new Canvas();

Content = canvas;

Random random = new Random();

Rectangle[,] rectangles = new Rectangle[30, 30];

for (int i = 0; i < rectangles.GetLength(0); i++)
for (int j = 0; j < rectangles.GetLength(1); j++)
{
rectangles[i, j] =
new Rectangle()
{
Width = 10,
Height = 10,
Fill = random.Next(2) == 0 ? Brushes.Black : Brushes.Red,
RenderTransform = new TranslateTransform(i * 10, j * 10)
};

canvas.Children.Add(rectangles[i, j]);
}
}
}
}

WPF 版本的内存效率似乎更低,因为世界上的每个单元格都有 Rectangle 对象的开销。

有没有一种方法可以像 Forms 版本一样高效地编写此程序?还是没有办法创建所有这些 Rectangle 对象?

最佳答案

这是一个纯 WPF 解决方案。 FrameworkElement 是子类。这个新的子类 (DrawingVisualElement) 公开了一个可用于绘制的 DrawingVisual 对象。

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

namespace DrawingVisualSample
{
public class DrawingVisualElement : FrameworkElement
{
private VisualCollection _children;

public DrawingVisual drawingVisual;

public DrawingVisualElement()
{
_children = new VisualCollection(this);

drawingVisual = new DrawingVisual();
_children.Add(drawingVisual);
}

protected override int VisualChildrenCount
{
get { return _children.Count; }
}

protected override Visual GetVisualChild(int index)
{
if (index < 0 || index >= _children.Count)
throw new ArgumentOutOfRangeException();

return _children[index];
}
}

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

Width = 350;
Height = 350;

var stackPanel = new StackPanel();

Content = stackPanel;

var drawingVisualElement = new DrawingVisualElement();

stackPanel.Children.Add(drawingVisualElement);

var drawingContext = drawingVisualElement.drawingVisual.RenderOpen();

var random = new Random();

for (int i = 0; i < 30; i++)
for (int j = 0; j < 30; j++)
drawingContext.DrawRectangle(
random.Next(2) == 0 ? Brushes.Black : Brushes.Red,
(Pen)null,
new Rect(i * 10, j * 10, 10, 10));

drawingContext.Close();
}
}
}

关于c# - 在WPF中高效绘制二维网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12993543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com