【问题标题】:How to capture screen programatically in windows phone 8.1 SDK?如何在 windows phone 8.1 SDK 中以编程方式捕获屏幕?
【发布时间】:2014-05-27 20:39:00
【问题描述】:

知道如何从代码中捕获 SDK 8.1 windows phone 中的屏幕吗?对于 windows phone 7.5,我已经看到了代码并尝试使用,但它失败了。 :(

【问题讨论】:

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


    【解决方案1】:

    您可以使用 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>
    

    【讨论】:

    • 我应该如何调用这个方法?
    • @SyedWajahatKareem 示例用法已添加
    • 我怎样才能把它保存到存储中?
    【解决方案2】:

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

    这是我们可以使用的一种方法(它接受一个 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.

    【讨论】:

      猜你喜欢
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多