문제

내가 그리는 아래 코드를 사용하여 DrawingVisual 그런 다음 이를 Image 사용하여 RenderTargetBitmap.마지막 Image 나중에 추가됩니다 Canvas 그리고 화면에 표시됩니다.

내 문제는 pixelWidth 그리고 pixelHeight 주장 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 재산

containervisual의 모든 후손들에 대해 모든 컨텐츠 경계 박스의 결합을 가져옵니다.

다음과 같이 작동해야 합니다.

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