C# WPF 控件截屏,将当前控件的内容转化为图片。
public partial class MyWindow : Window
{
private void ToImage()
{
RenderTargetBitmap targetBitmap = new RenderTargetBitmap((int)Width, (int)Height, 96d, 96d, PixelFormats.Default);
targetBitmap.Render(this);
PngBitmapEncoder saveEncoder = new PngBitmapEncoder();
saveEncoder.Frames.Add(BitmapFrame.Create(targetBitmap));
string tempFile = System.IO.Path.GetTempFileName() + ".png";
System.IO.FileStream fs = System.IO.File.Open(tempFile, System.IO.FileMode.OpenOrCreate);
saveEncoder.Save(fs);
fs.Close();
}
}