1

使用我正在绘制的以下代码,DrawingVisual然后将其渲染为Imageusing RenderTargetBitmap。最后Image添加到 aCanvas并显示在屏幕上。

我的问题是方法想要的pixelWidthandpixelHeight参数。RenderTargetBitmap我应该赋予它什么价值?我注意到,如果我给它较低的数字,则不会渲染图像的部分。我应该根据什么来选择这些?我在下面的代码中给了它 1000。

public class ModelBeamSectionNamesInPlan : Image
{
    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;

        beamtextcolor.Freeze();
        const double scale = 0.6;

        var drawingVisual = new DrawingVisual();
        using (var dc = drawingVisual.RenderOpen())
        {
            foreach (var beam in Building.ModelBeamsInTheElevation)
            {
                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));
            }
        }

        var bmp = new RenderTargetBitmap(1000, 1000, 120, 96, PixelFormats.Pbgra32);
        bmp.Render(drawingVisual);
        Source = bmp;
    }
}
4

1 回答 1

3

您可以查询 DrawingVisual 的ContentBounds属性,其中

获取 ContainerVisual 内容的边界框

DescendantBounds财产

获取 ContainerVisual 的所有后代的所有内容边界框的并集​​,但不包括 ContainerVisual 的内容。

像这样的东西应该工作:

var bounds = drawingVisual.DescendantBounds;
var bmp = new RenderTargetBitmap(
    (int)Math.Ceiling(bounds.Width), (int)Math.Ceiling(bounds.Height),
    96, 96, PixelFormats.Pbgra32);
于 2014-08-04T10:31:18.817 回答