How can i take screenshot from code?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Check here, it seems its possible on the emulator.
回答2:
Taking a screenshot of your application from your application code is very simple using WriteableBitmap
. Laurent Bugnion has a good write up here: http://geekswithblogs.net/lbugnion/archive/2010/12/28/taking-a-screenshot-from-within-a-silverlight-wp7-application.aspx
回答3:
Taking a screenshot from within a Silverlight #WP7 application.
public static void SaveToMediaLibrary(
FrameworkElement element,
string title)
{
try
{
var bmp = new WriteableBitmap(element, null);
var ms = new MemoryStream();
bmp.SaveJpeg(
ms,
(int)element.ActualWidth,
(int)element.ActualHeight,
0,
100);
ms.Seek(0, SeekOrigin.Begin);
var lib = new MediaLibrary();
var filePath = string.Format(title + ".jpg");
lib.SavePicture(filePath, ms);
MessageBox.Show(
"Saved in your media library!",
"Done",
MessageBoxButton.OK);
}
catch
{
MessageBox.Show(
"There was an error. Please disconnect your phone from the computer before saving.",
"Cannot save",
MessageBoxButton.OK);
}}
回答4:
Here is how you can take a screenshot from your app, from within your page's code and saves it to the picture library of your phone. Note that this won't capture the SysTray or the AppBar:
WriteableBitmap w = new System.Windows.Media.Imaging.WriteableBitmap(this, null); // 'this' is your current page
WriteableBitmap w2 = new System.Windows.Media.Imaging.WriteableBitmap(480, 800);
// space for SysTray
for (int i = 0; i < 32; i++)
{
for (int j = 0; j < 480; j++)
{
w2.Pixels[i * 480 + j] = -16777216; // black #ff000000
}
}
// actual client area
for (int i = 32; i < 728; i++)
{
for (int j = 0; j < 480; j++)
{
w2.Pixels[i * 480 + j] = w.Pixels[(i - 32) * 480 + j];
}
}
// space for AppBar
for (int i = 728; i < 800; i++)
{
for (int j = 0; j < 480; j++)
{
w2.Pixels[i * 480 + j] = -16777216; // black #ff000000
}
}
MemoryStream ms = new MemoryStream();
w2.SaveJpeg(ms, 480, 800, 0, 100);
Microsoft.Xna.Framework.Media.MediaLibrary lib = new Microsoft.Xna.Framework.Media.MediaLibrary();
ms.Position = 0;
lib.SavePicture("screenshot", ms);
回答5:
You can't i'm afraid. If you want screenshots you need to do outside using something like the snipping tool.