如何在Windows Phone 8.1 SDK中以编程方式捕获屏幕?

时间:2014-05-27 20:39:01

标签: c# windows-phone windows-phone-8.1

任何想法如何从代码中捕获SDK 8.1 windows手机中的屏幕?对于Windows Phone 7.5我已经看过代码并试图使用,但它失败了。 :(

2 个答案:

答案 0 :(得分:6)

您可以使用RenderTargetBitmap并传递一个表示该页面的FrameworkElement,然后从中呈现位图。

private async Task<RenderTargetBitmap> CreateBitmapFromElement(FrameworkElement uielement)
{
    try
    {
        var renderTargetBitmap = new RenderTargetBitmap();
        await renderTargetBitmap.RenderAsync(uielement);

        return renderTargetBitmap;
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(ex);
    }

    return null;
}

尝试类似:

private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
    this.imagePreview.Source = await CreateBitmapFromElement(this);
}

页面上的XAML是:

<Grid x:Name="controlsGrid">
    <Button Click="ButtonBase_OnClick">take screenshot</Button>
    <Image x:Name="imagePreview"
           Height="200" VerticalAlignment="Bottom"
           Stretch="UniformToFill" />
</Grid>

答案 1 :(得分:1)

要将渲染图像保存为图像文件,我们必须将其发送到流,将其编码为我们想要的文件类型。

这是我们可以使用的方法(它包含UI元素,流和Guid):

//Creates RenderTargetBitmap from UI Element 
 async Task<RenderTargetBitmap> CaptureToStreamAsync(FrameworkElement uielement, IRandomAccessStream stream, Guid encoderId)
  {
      try
      {
          var renderTargetBitmap = new RenderTargetBitmap();
          await renderTargetBitmap.RenderAsync(uielement);
          var pixels = await renderTargetBitmap.GetPixelsAsync();
          var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
          var encoder = await BitmapEncoder.CreateAsync(encoderId, stream);
          encoder.SetPixelData(
              BitmapPixelFormat.Bgra8,
              BitmapAlphaMode.Ignore,
              (uint)renderTargetBitmap.PixelWidth,
              (uint)renderTargetBitmap.PixelHeight,
              logicalDpi,
              logicalDpi,
              pixels.ToArray());

          await encoder.FlushAsync();
          return renderTargetBitmap;        
  }

然后我们可以使用Windows Phone 8.1上的FileSavePicker类来确定文件类型,名称和保存位置。

void CreateFileSavePicker()
    {
        //Create the picker object
        FileSavePicker savePicker = new FileSavePicker();

        // Dropdown of file types the user can save the file as   
        savePicker.FileTypeChoices.Add
            (
            "Image", new List<string>() { ".jpg" });
        // Default file name if the user does not type one in or select // a file to replace
        savePicker.SuggestedFileName = "Screenshot";
        savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; 

        //// Open the picker for the user to pick a file
        savePicker.ContinuationData["Operation"] = "SomeDataOrOther";
        savePicker.PickSaveFileAndContinue();

    }

当用户选择文件位置时,它会返回到ContinueFileSavePicker。

public async void      ContinueFileSavePicker(Windows.ApplicationModel.Activation.FileSavePickerContinu ationEventArgs args)
 {
        StorageFile file = args.File;
        if (file != null)
{
            // Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
            CachedFileManager.DeferUpdates(file);

            Guid encoderId = BitmapEncoder.JpegEncoderId;

            try
            {
                using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    await CaptureToStreamAsync(LayoutRoot, stream, encoderId);

                }
            }
            catch (Exception ex)
            {
                DisplayMessage(ex.Message);
            }
}
}

更多详情here