FrameworkElement to BitmapSource Extension methode
18/01/2017
Aucun commentaire
An extension methode for transforme all FrameworkElement to BitmapSource
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public static BitmapSource ToBitmapSource(this FrameworkElement element) { double dw = Math.Max(element.ActualWidth, element.Width); double dh = Math.Max(element.ActualHeight, element.Height); // needed for no visible FrameworkElement if (!element.IsVisible) { var size = new Size(dw, dh); element.Measure(size); element.Arrange(new Rect(size)); } int width = (Int32)Math.Ceiling(dw); int height = (Int32)Math.Ceiling(dh); RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32); DrawingVisual drawingVisual = new DrawingVisual(); using (DrawingContext drawingContext = drawingVisual.RenderOpen()) { VisualBrush visualBrush = new VisualBrush(element); drawingContext.DrawRectangle( visualBrush, null, new Rect(new Point(), new Size(width, height))); } bmp.Render(drawingVisual); return bmp; } |
Categories: wpf
Commentaires récents