使用下面的代码我正在绘制 DrawingVisual 然后将其渲染为 Image 使用 RenderTargetBitmap. 。决赛 Image 后来被添加到 Canvas 并显示在屏幕上。

我的问题是 pixelWidthpixelHeight 论证 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;
    }
}
有帮助吗?

解决方案

您可以查询DrawingVisual的 ContentBounds 财产,其中

获取 ContainerVisual 内容的边界框

或者 DescendantBounds 财产其中

获取所有内容边界框的结合,以适用于contunervisual的所有后代,但不包括containerVisal的内容。

像这样的东西应该有效:

var bounds = drawingVisual.DescendantBounds;
var bmp = new RenderTargetBitmap(
    (int)Math.Ceiling(bounds.Width), (int)Math.Ceiling(bounds.Height),
    96, 96, PixelFormats.Pbgra32);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top