I can save all the UserControl to image. But all the pictures is dark when I click save button in the first time except current UserControl, That's ok when I click save button again.
This is my C# code:
public static void SaveView(UserControl viewName, int dpi, string fileName, string destFolder) { Rect bounds = VisualTreeHelper.GetDescendantBounds(viewName); int width = (int)viewName.RenderSize.Width; int height = (int)viewName.RenderSize.Height; if (width == 0 && height == 0) { width = 1920; height = 1080; } RenderTargetBitmap rtb = new RenderTargetBitmap( width, height, dpi, dpi, PixelFormats.Pbgra32 ); DrawingVisual dv = new DrawingVisual(); using (DrawingContext ctx = dv.RenderOpen()) { VisualBrush vb = new VisualBrush(viewName); ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size)); } rtb.Render(viewName); try { PngBitmapEncoder jpgEncoder = new PngBitmapEncoder(); jpgEncoder.Frames.Add(BitmapFrame.Create(rtb)); Byte[] _imageArray; using (MemoryStream outputStream = new MemoryStream()) { jpgEncoder.Save(outputStream); _imageArray = outputStream.ToArray(); } //Try Find Save Path, if doesn't exists, create it. if (Directory.Exists(destFolder) == false) Directory.CreateDirectory(destFolder); FileStream fileStream = new FileStream(Path.Combine(destFolder, fileName), FileMode.Create, System.IO.FileAccess.Write); fileStream.Write(_imageArray, 0, _imageArray.Length); fileStream.Close(); } catch (Exception e) { Log4Net.Instance.Info(System.Reflection.MethodInfo.GetCurrentMethod().ToString()); Log4Net.Instance.Info("Exception Generate Screenshot: " + fileName); Log4Net.Instance.Info(e.StackTrace.ToString()); Debug.WriteLine(e.ToString()); } }
Many thanks in advance.