WPF에서 레이어를 병합하고 이미지로 저장하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/1536547

  •  20-09-2019
  •  | 
  •  

문제

두 개의 레이어가 있습니다. 첫 번째 레이어는 이미지 컨트롤입니다. 그것의 소스는 비트 맵 이미지입니다. 그리고 이것은 배경 레이어입니다. 전면 레이어 인 두 번째는 형상 객체 (예 : 선, 폴리 라인, 사각형 등)를 그릴 수있는 캔버스이며 캔버스 배경은 투명합니다. 이 두 레이어를 병합하고 WPF를 사용하여 이미지로 저장하려면 어떻게해야합니까?

도움이 되었습니까?

해결책

"레이어"는 무엇을 의미합니까? 그리드의 같은 셀에 두 개의 컨트롤이 앉았습니까? 두 "레이어"가 다른 컨테이너 (예 : 그리드 또는 창)에 앉은 경우 해당 컨테이너와 함께 RenderTargetBitMap을 사용하여 이미지를 얻을 수 있습니다. 세부 사항, 확장 방법이 있습니다 내 블로그에서 WPF "스크린 샷"을 찍습니다.

다른 팁

이미지 제어 및 캔버스를 배치 한 부모 패널의 비트 맵을 얻을 수 있습니다.

WPF에서 Uielement의 비트 맵을 얻는 코드는 어떻게됩니까?

 RenderTargetBitmap bmp = new RenderTargetBitmap(Width, Height, 96, 96, PixelFormats.Pbgra32);
bmp.Render(parentPanel);

캔버스 에서이 방법을 호출하십시오 (캔버스) -

private Bitmap ImageGenerator()
    {
        var transform = this.LayoutTransform;
        // Call UpdateLayout to make sure changes all changes 
        // while drawing objects on canvas are reflected
        var layer = AdornerLayer.GetAdornerLayer(this);
        layer?.UpdateLayout();
        // Get the size of canvas
        var size = new System.Windows.Size(this.ActualWidth, this.ActualHeight);
        // Measure and arrange the surface
        // VERY IMPORTANT
        this.Measure(size);
        this.Arrange(new Rect(RenderSize));

        RenderTargetBitmap renderBitmap =
                     new RenderTargetBitmap(
                       (int)this.ActualWidth,
                       (int)this.ActualHeight,
                       96d,
                       96d,
                       PixelFormats.Pbgra32);
        renderBitmap.Render(this);

        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
        // push the rendered bitmap to it
        encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
        var stream = new MemoryStream();
        encoder.Save(stream);
        this.LayoutTransform = transform;
        return new Bitmap(stream);
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top