Silverlight 4 - Render UIElement as an Image

后端 未结 4 454
不思量自难忘°
不思量自难忘° 2021-01-17 16:33

I have a UIElement that I want to capture a snapshot of when a user clicks a button. When a user clicks the button, I want to take the UIElement an

4条回答
  •  失恋的感觉
    2021-01-17 17:18

    Assuming the FrameworkElement you want to render is named elementToRender and the Image where you want to place the rendered output is called image, use the following code on your button's click handler:

    var writeableBitmap = new WriteableBitmap((int)elementToRender.RenderSize.Width, (int)elementToRender.RenderSize.Height);
    
    writeableBitmap.Render(elementToRender, new ScaleTransform() { ScaleX = 1, ScaleY = 1 });
    writeableBitmap.Invalidate();
    
    image.Source = writeableBitmap;
    

提交回复
热议问题