-->

宽度和WPF中RenderTargetBitmap像高(Width and Height of th

2019-10-20 14:47发布

使用下面的代码我画上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;
    }
}

Answer 1:

您可以查询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);


文章来源: Width and Height of the Image in RenderTargetBitmap in WPF