简体   繁体   English

在WPF中绘制数千个矩形

[英]Drawing thousands of rectangles in WPF

I need to draw many rectangles(~50000). 我需要绘制许多矩形(~50000)。 Currently I'm using the following approach. 目前我正在使用以下方法。

<ItemsControl ItemsSource="{Binding Elements}" IsHitTestVisible="False"  Background="Transparent">

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas IsItemsHost="True"
                    Background="Transparent"
                    Width="500" 
                    Height="500">
            </Canvas>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="Canvas.Left" Value="{Binding X}"/>
            <Setter Property="Canvas.Bottom" Value="{Binding Y}"/>
        </Style>
    </ItemsControl.ItemContainerStyle>

    <ItemsControl.ItemTemplate>
        <DataTemplate DataType="{x:Type models:Element}">
            <Rectangle Width  = "{Binding Width}"
                       Height = "{Binding Height}"
                       Fill= "{Binding Brush}"
                       SnapsToDevicePixels="True" >
            </Rectangle>
        </DataTemplate>
    </ItemsControl.ItemTemplate>

</ItemsControl>

The problem is that it takes a very long time to draw this amount of rectangles. 问题是绘制这个数量的矩形需要很长时间。 Is there a better way to draw a large amount of shapes? 是否有更好的方法来绘制大量的形状?

Wpf has a pretty inefficient rendering engine, and 50000 shapes are way too much for it, even without the binding overhead. Wpf有一个非常低效的渲染引擎,即使没有绑定开销,也有50000个形状太多了。

Take a short look at this document: WPF rendering system 简要看一下这个文档: WPF渲染系统

Instead, consider to use a drawing API such as Direct2D, which is compareably well accessible from WPF: Direct2D with WPF 相反,请考虑使用绘图API,例如Direct2D,它可以从WPF中轻松访问: Direct2D与WPF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM