문제

왜 이것이 작동하지 않는지, 또는 작동 해야하는지 이해할 수 없습니다.

재현하려면 간단한 WPF 응용 프로그램을 작성하고 주 창의 생성자를 대체하십시오.

    public MainWindow()
    {
        InitializeComponent();

        // simple visual definition
        var grid = new Grid { Width = 300, Height = 300 };
        var text = new TextBlock 
                       { 
                         Text = "Y DON'T I WORK???", 
                         FontSize = 100, 
                         FontWeight = 
                         FontWeights.Bold 
                       };
        grid.Children.Add(text);

        // update the layout so everything is awesome cool
        grid.Measure(grid.DesiredSize);
        grid.Arrange(new Rect(grid.DesiredSize));
        grid.UpdateLayout();

        // create a BitmapSource from the visual
        var rtb = new RenderTargetBitmap(
                                    (int)grid.Width,
                                    (int)grid.Height,
                                    96,
                                    96,
                                    PixelFormats.Pbgra32);
        rtb.Render(grid);

        // Slap it in the window
        this.Content = new Image { Source = rtb, Width = 300, Height = 300 };
    }

이로 인해 빈 이미지가 발생합니다. RTB를 디스크에 PNG로 저장하면 올바른 크기이지만 투명합니다.

그러나 화면에 표시된 시각적 으로이 작업을 수행하면 잘 작동합니다.

오프 스크린을 비트 맵으로 구성한 비주얼을 어떻게 렌더링 할 수 있습니까?

도움이 되었습니까?

해결책

요소는 측정 할 때까지 원하는 크기가 없기 때문입니다. 사용 가능한 0x0 공간으로 그리드를 크기로 지정했습니다. 코드 변경 :

grid.Measure(new Size(grid.Width, grid.Height));
grid.Arrange(new Rect(new Size(grid.Width, grid.Height)));

(updatelayout에 대한 호출은 불필요합니다.)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top