1、分享一个取得屏幕截图的代码,但是由于程序不能在后台运行,
所以只能通过按钮或者菜单取得截图,然后把图片保存在相册中。
public void CaptureScreen(object sender, EventArgs e) { WriteableBitmap bmp = new WriteableBitmap(480, 800); bmp.Render(App.Current.RootVisual, null); bmp.Invalidate(); MemoryStream stream = new MemoryStream(); bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 80); stream.Seek(0, SeekOrigin.Begin); MediaLibrary library = new MediaLibrary(); string filename = "ScreenShot_" + DateTime.Now.ToString("yyyy-MM-dd_hh:mm:ss"); library.SavePicture(filename, stream); stream.Close(); }
2、每隔一段时间屏幕截图一次:
在App.xaml.cs的构造函数数添加如下方法,
就能在程序每执行10秒就截一次图并且保存在Pictures中的Saved Pictures目录下:
private void SnapShot() { var timer = new System.Windows.Threading.DispatcherTimer { Interval = System.TimeSpan.FromSeconds(5) }; timer.Tick += (sender, args) => { Microsoft.Devices.VibrateController.Default.Start( TimeSpan.FromSeconds(0.1)); var bitmap = new System.Windows.Media.Imaging.WriteableBitmap( this.RootFrame, null); var stream = new System.IO.MemoryStream(); System.Windows.Media.Imaging.Extensions.SaveJpeg(bitmap, stream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 100); stream.Position = 0; var mediaLib = new Microsoft.Xna.Framework.Media.MediaLibrary(); var datetime = System.DateTime.Now; var filename = System.String.Format("Capture-{0}-{1}-{2}-{3}-{4}-{5}", datetime.Year % 100, datetime.Month, datetime.Day, datetime.Hour, datetime.Minute, datetime.Second); mediaLib.SavePicture(filename, stream); }; timer.Start(); }
注意需要添加XNA的引用,还有一个需要注意的是AppBar并不会被截下来,
如果你的应用程序界面中有AppBar,截下来的效果是下面是一栏黑色的。