简体   繁体   English

WPF:复杂图像的快速绘制/缩放

[英]WPF: Fast drawing/scaling of complex image

I'm drawing a complex image that consist of hundreds of GeometryDrawing objects.我正在绘制由数百个 GeometryDrawing 对象组成的复杂图像。 The drawing is quite fast, but rescaling is slow.绘图速度很快,但重新缩放很慢。 I can imagine that redrawing hundreds of object takes some time.我可以想象重绘数百个 object 需要一些时间。 As a solution, I want to turn my drawing into a bitmap.作为解决方案,我想将我的绘图变成 bitmap。

I've tried the following, but this doesn't work.我尝试了以下方法,但这不起作用。 The result image is blank.结果图像为空白。

private Image CreateBitmapImage(DrawingImage drawingImage)
{
    var image = new Image()
    image.Source = drawingImage;
    var bitmap = new RenderTargetBitmap(300, 300, 96, 96, PixelFormats.Pbgra32);
    bitmap.Render(image);
    image.Source = bitmap;
    return image;
}

How can I turn a DrawingImage into a bitmap?如何将 DrawingImage 转换为 bitmap?

If you are using RenderTargetBitmap with newly created elements you need to make sure you call Measure and Arrange.如果您将 RenderTargetBitmap 与新创建的元素一起使用,则需要确保调用 Measure 和 Arrange。

private Image CreateBitmapImage(DrawingImage drawingImage)
{
    var image = new Image();
    image.Source = drawingImage;
    var bitmap = new RenderTargetBitmap(300, 300, 96, 96, PixelFormats.Pbgra32);
    var size = new Size(bitmap.PixelWidth,bitmap.PixelHeight);
    image.Measure(size);
    image.Arrange(new Rect(size));
    bitmap.Render(image);
    image.Source = bitmap;
    return image;
}

You will probably want to pass the size in as a parameter and it might be better to return the RenderTargetBitmap instead of an Image element so you can use it in multiple places.您可能希望将大小作为参数传递,并且返回 RenderTargetBitmap 而不是 Image 元素可能会更好,这样您就可以在多个地方使用它。

WPF has some features that you might want to look into such as Freezables , BitmapCache and CachingHint for TileBrushes however RenderTargetBitmap is still a good choice as it is predictable. WPF 具有一些您可能想要研究的功能,例如FreezablesBitmapCacheCachingHint for TileBrushes 但是 RenderTargetBitmap 仍然是一个不错的选择,因为它是可预测的。

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

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