如何在WPF C#中将DrawingVisual转换为位图 [英] How to convert DrawingVisual to Bitmap in WPF C#

查看:440
本文介绍了如何在WPF C#中将DrawingVisual转换为位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在

DrawingVisual

上绘制文字。我听说你可以使用`

. I've heard that you can use `

RenderTargetBitmap

`将它转换为Bitmap以获得更好的性能。



` to convert this to Bitmap to have better performance.

public class ModelBeamSectionNamesInPlan : UIElement
{
    private readonly VisualCollection _visuals;
    public ModelBeamSectionNamesInPlan(BaseWorkspace space)
    {
        var typeface = Settings.BeamTextTypeface;
        var cultureinfo = Settings.CultureInfo;
        var flowdirection = Settings.FlowDirection;
        var beamtextsize = Settings.BeamTextSize;
        var beamtextcolor = Settings.InPlanBeamTextColor;

        const double scale = 0.5;

        _visuals = new VisualCollection(this);
        foreach (var beam in Building.ModelBeamsInTheElevation)
        {
            var drawingVisual = new DrawingVisual();
            using (var dc = drawingVisual.RenderOpen())
            {
                var text = beam.Section.Id;
                var ft = new FormattedText(text, cultureinfo, flowdirection,
                                           typeface, beamtextsize, beamtextcolor,
                                           null, TextFormattingMode.Display)
                {
                    TextAlignment = TextAlignment.Center
                };

                // Draw Text
                dc.DrawText(ft, space.FlipYAxis(x, y));
            }
            _visuals.Add(drawingVisual);
        }
    }

    protected override Visual GetVisualChild(int index)
    {
        return _visuals[index];
    }

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





最后我用的是以下将文本添加到Canvas的代码:





And finally I'm using the below code to add the texts to `Canvas`:

var beamtexts = new ModelBeamSectionNamesInPlan(this);
MyCanvas.Children.Add(beamtexts);





我没有丝毫的线索我应该使用`RenderTargetBitmap`转换为BMP以及如何使用将它添加到MyCanvas?





I don't have the slightest clue where I should use the `RenderTargetBitmap` to convert to BMP and how to add it to MyCanvas?

In the

RenderTargetBitmap 

文档示例中我发现了这个:



documentation example I have found this:

RenderTargetBitmap bmp = new RenderTargetBitmap(180, 180, 120, 96, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);
myImage.Source = bmp;





但我不知道如何在我的代码中实现这一点。



But I don't know how to implement this in my code.

推荐答案

目前尚不清楚您在本文档中遗漏了什么。注意:方法 System.Windows.Media.Imaging.RenderTargetBitmap.Render 获取 System.Windows.Media.Visual 作为参数。你的类 ModelBeamSectionNamesInPlan UIElement ,因此, Visual

http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.render%28v=vs.110%29.aspx [ ^ ],

http:// msdn .microsoft.com / zh-cn / library / system.windows.uielement.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.media.visual.aspx [ ^ ] 。



获取 ModelBeamSectionNamesInPlan 的实例,将其作为 RenderTargetBitmap的参数传递。渲染,依此类推。



-SA
It's not clear what do you miss in this documentation. Pay attention: the method System.Windows.Media.Imaging.RenderTargetBitmap.Render takes the instance of System.Windows.Media.Visual as a parameter. And your class ModelBeamSectionNamesInPlan is UIElement and, consequently, Visual:
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.render%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.uielement.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.media.visual.aspx[^].

Get an instance of ModelBeamSectionNamesInPlan, pass it as a parameter of RenderTargetBitmap.Render, and so on.

—SA


这篇关于如何在WPF C#中将DrawingVisual转换为位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆