Article From:https://www.cnblogs.com/lonelyxmas/p/9688775.html
Intercepting controls and saving them as pictures (absolutely easy to use)

        /// <summary>
        /// Intercept the save control as PNG/// </summary>
        /// <param name="panel">The name of the control you want to intercept.</param>
        public void ScreenShotSave(FrameworkElement panel)
        {
            string rootFolder = Directory.GetCurrentDirectory() + @"\Paper\";
            var dlg = new SaveFileDialog
            {
                FileName = rootFolder + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss-ff") + ".png"
            };
            if (!Directory.Exists(rootFolder))
                Directory.CreateDirectory(rootFolder);

            using (var file = new FileStream(dlg.FileName, FileMode.Create, FileAccess.Write))
            {
                int marg = int.Parse(panel.Margin.Left.ToString(CultureInfo.InvariantCulture));
                var rtb = new RenderTargetBitmap((int)panel.ActualWidth - marg,
                                (int)panel.ActualHeight - marg, 0, 0, PixelFormats.Default);
                rtb.Render(panel);
                var encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(rtb));
                encoder.Save(file);
                file.Close();
            }
        }

 

Leave a Reply

Your email address will not be published. Required fields are marked *