2013-04-17 3 views
3

현재 도면과 함께 하나의 writeablebitmap 이미지와 캔버스가 있으며 피어로 이미지를 보내려고합니다. 대역폭을 줄이기 위해 canvas를 writeablebitmap으로 변환하고 싶습니다. 따라서 두 이미지를 모두 새로운 것으로 blit 할 수 있습니다. writeablebitmap. 문제는 내가 캔버스를 변환하는 좋은 방법을 찾을 수 없다는 것입니다. 따라서 캔버스를 writeablebitmap 클래스로 변환하는 직접적인 방법이 있는지 묻고 싶습니다.WPF에서 캔버스를 writeablebitmap으로 변환하는 가장 빠른 방법은 무엇입니까?

+0

저는 WPF와 함께 C#을 사용하고 있습니다. –

답변

4

이것은 this blog post에서 가져온 것이지만 파일에 쓰는 대신 WriteableBitmap에 기록합니다.

public WriteableBitmap SaveAsWriteableBitmap(Canvas surface) 
{ 
    if (surface == null) return null; 

    // Save current canvas transform 
    Transform transform = surface.LayoutTransform; 
    // reset current transform (in case it is scaled or rotated) 
    surface.LayoutTransform = null; 

    // Get the size of canvas 
    Size size = new Size(surface.ActualWidth, surface.ActualHeight); 
    // Measure and arrange the surface 
    // VERY IMPORTANT 
    surface.Measure(size); 
    surface.Arrange(new Rect(size)); 

    // Create a render bitmap and push the surface to it 
    RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
     (int)size.Width, 
     (int)size.Height, 
     96d, 
     96d, 
     PixelFormats.Pbgra32); 
    renderBitmap.Render(surface); 


    //Restore previously saved layout 
    surface.LayoutTransform = transform; 

    //create and return a new WriteableBitmap using the RenderTargetBitmap 
    return new WriteableBitmap(renderBitmap); 

}