截取控件并保存为图片(绝对好用)

2019-04-15 17:36发布

原文:截取控件并保存为图片(绝对好用) /// /// 截取保存控件为PNG /// /// 你要截取的控件名 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(); } }