WPF中,如何将窗口中绘制内容的保存为图像文件
窗口有有Grid,Grid有背景图像,网格中有各种对象,
现在要将看到的内容保存为图像文件,如何做呢
[解决办法]
SaveWindowContent(this, "c:\\tmp.bmp"); private void SaveWindowContent(Window source, string fileName) { FrameworkElement elem = source.Content as FrameworkElement; RenderTargetBitmap targetBitmap = new RenderTargetBitmap( (int)elem.ActualWidth, (int)elem.ActualHeight, 96d, 96d, PixelFormats.Default); targetBitmap.Render(source); BmpBitmapEncoder encoder = new BmpBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(targetBitmap)); // save file to disk using (FileStream fs = File.Open(fileName, FileMode.OpenOrCreate)) { encoder.Save(fs); } }