【问题标题】:Take a screenshot programmatically when My App is loaded [duplicate]加载我的应用程序时以编程方式截取屏幕截图[重复]
【发布时间】:2013-12-13 08:24:46
【问题描述】:

我正在尝试在加载“我的应用”时截取屏幕截图,而无需任何用户交互。

我的代码是这样的

 public MainPage()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(ApplicationBarIconButton);

    }

    private void ApplicationBarIconButton(object sender, EventArgs e)
    {
        var fileName = String.Format("WmDev_{0:}.jpg", DateTime.Now.Ticks);
        WriteableBitmap bmpCurrentScreenImage = new WriteableBitmap((int)this.ActualWidth, (int)this.ActualHeight);
        bmpCurrentScreenImage.Render(LayoutRoot, new MatrixTransform());
        bmpCurrentScreenImage.Invalidate();
        SaveToMediaLibrary(bmpCurrentScreenImage, fileName, 100);
        MessageBox.Show("Captured image " + fileName + " Saved Sucessfully", "WmDev Capture Screen", MessageBoxButton.OK);

        currentFileName = fileName;
    }

    public void SaveToMediaLibrary(WriteableBitmap bitmap, string name, int quality)
    {
        using (var stream = new MemoryStream())
        {
            // Save the picture to the Windows Phone media library.
            bitmap.SaveJpeg(stream, bitmap.PixelWidth, bitmap.PixelHeight, 0, quality);
            stream.Seek(0, SeekOrigin.Begin);
            new MediaLibrary().SavePicture(name, stream);
        }
    }

我收到以下错误:

【问题讨论】:

标签: c# xaml windows-phone-7 windows-phone-8


【解决方案1】:

诺基亚开发者 wiki 上的这篇文章解释了如何以编程方式捕获应用的屏幕(您也可以找到大量代码示例):

http://developer.nokia.com/Community/Wiki/How_to_capture_screen_programmatically_in_Windows_Phone_7

没有 API 可让您从应用内部截取整个屏幕的屏幕截图(例如截取另一个应用)。

关于您在图片中向我们展示的那个错误,您的应用似乎没有将图片保存在 MediaLibrary 中的授权。

尝试将代码放在 try-catch 块内的 SaveToMediaLibrary 方法中:

    public void SaveToMediaLibrary(WriteableBitmap bitmap, string name, int quality)
{
    using (var stream = new MemoryStream())
    {
        try
        {
           // Save the picture to the Windows Phone media library.
           bitmap.SaveJpeg(stream, bitmap.PixelWidth, bitmap.PixelHeight, 0, quality);
           stream.Seek(0, SeekOrigin.Begin);
           new MediaLibrary().SavePicture(name, stream);
        }
        catch(UnauthorizedAccessException uae)
        {
          // log the exception message, uae.Message, in your favourite way :)
        }
    }
}

如果在调用 SaveLibrary() 时出现异常,如 msdn 上的this article 所示,在第一个答案中,您需要在应用清单中指定媒体库功能,例如这个:ID_CAP_MEDIALIB

我希望这会有所帮助。

【讨论】:

  • Sriram 向我们展示的代码取自那篇文章 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-17
  • 1970-01-01
相关资源
最近更新 更多