Get System.Drawing.Bitmap of a WPF Area Using VisualBrush

The thing is, I need to convert to System.Drawing.Bitmap (.Net Framework 2.0) in order to get one WPF Grid frame with its content.

I read about VisualBrush and DrawingBrush , but I can't imagine how it should work.

I can convert any WPF BitmapSource to my System.Drawing.Bitmap successfully. But how to get BitmapSource from my grid?

thanks

+2
source share
1 answer

To convert a Visual to a BitmapSource , you can use RenderTargetBitmap , VisualBrush and DrawingVisual :

 public BitmapSource ConvertToBitmapSource(UIElement element) { var target = new RenderTargetBitmap((int)(element.RenderSize.Width), (int)(element.RenderSize.Height), 96, 96, PixelFormats.Pbgra32); var brush = new VisualBrush(element); var visual = new DrawingVisual(); var drawingContext = visual.RenderOpen(); drawingContext.DrawRectangle(brush, null, new Rect(new Point(0, 0), new Point(element.RenderSize.Width, element.RenderSize.Height))); drawingContext.Close(); target.Render(visual); return target; } 
+5
source

Source: https://habr.com/ru/post/921977/


All Articles