导入以下命名空间:
using System.Windows.Media.Imaging;
using System.IO;
using Microsoft.Xna.Framework.Media;
全部代码如下:
1
public
void
CaptureScreen(
object
sender, EventArgs e)
2 {
3 WriteableBitmap bmp = new WriteableBitmap( 480 , 800 );
4 bmp.Render(App.Current.RootVisual, null );
5 bmp.Invalidate();
6
7 MemoryStream stream = new MemoryStream();
8 bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0 , 80 );
9 stream.Seek( 0 , SeekOrigin.Begin);
10
11 MediaLibrary library = new MediaLibrary();
12 string fileName = " ScreenShot_ " + DateTime.Now.ToString( " yyyy-mm-dd_hh:mm:ss " );
13 library.SavePicture(fileName, stream);
14 stream.Close();
15
16
17 Dispatcher.BeginInvoke(() =>
18 {
19 PictureCollection picCollection = library.Pictures;
20 foreach (Picture item in picCollection)
21 {
22 if (item != null )
23 {
24 BitmapImage bitmap = new BitmapImage();
25 bitmap.SetSource(item.GetImage());
26 ScreenShot.Source = bitmap;
27 PicName.Text = " 图片名称 : " + item.Name;
28 }
29
30 }
31 });
32
33
34
2 {
3 WriteableBitmap bmp = new WriteableBitmap( 480 , 800 );
4 bmp.Render(App.Current.RootVisual, null );
5 bmp.Invalidate();
6
7 MemoryStream stream = new MemoryStream();
8 bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0 , 80 );
9 stream.Seek( 0 , SeekOrigin.Begin);
10
11 MediaLibrary library = new MediaLibrary();
12 string fileName = " ScreenShot_ " + DateTime.Now.ToString( " yyyy-mm-dd_hh:mm:ss " );
13 library.SavePicture(fileName, stream);
14 stream.Close();
15
16
17 Dispatcher.BeginInvoke(() =>
18 {
19 PictureCollection picCollection = library.Pictures;
20 foreach (Picture item in picCollection)
21 {
22 if (item != null )
23 {
24 BitmapImage bitmap = new BitmapImage();
25 bitmap.SetSource(item.GetImage());
26 ScreenShot.Source = bitmap;
27 PicName.Text = " 图片名称 : " + item.Name;
28 }
29
30 }
31 });
32
33
34
35 }
调用该方法后得到的效果如下:
本文转自 terry_龙 51CTO博客,原文链接:http://blog.51cto.com/terryblog/551752,如需转载请自行联系原作者