背水一战 Windows 10 (65) - 控件(WebView): 对 WebView 中的内容截图, 通过 Share Contract 分享 WebView 中的被选中的内容

[源码下载]


背水一战 Windows 10 (65) - 控件(WebView): 对 WebView 中的内容截图, 通过 Share Contract 分享 WebView 中的被选中的内容



作者:webabcd


介绍
背水一战 Windows 10 之 控件(WebView)

  • 对 WebView 中的内容截图
  • 通过 Share Contract 分享 WebView 中的被选中的内容



示例
1、演示如何对 WebView 中的内容截图
Controls/WebViewDemo/WebViewDemo5.xaml

<Page
    x:Class="Windows10.Controls.WebViewDemo.WebViewDemo5"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.WebViewDemo"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="Transparent">
        <StackPanel Margin="10 0 10 10">

            <Button Name="btnCapture" Content="对此 WebView 中的当前内容截图" Click="btnCapture_Click" Margin="5" />

            <WebView Name="webView" Width="400" Height="300" Source="http://webabcd.cnblogs.com/" HorizontalAlignment="Left" Margin="5" />

            <StackPanel Margin="5" Orientation="Horizontal">
                <Image Name="imageOriginal" Width="400" Height="300" HorizontalAlignment="Left" />
                <Image Name="imageThumbnail" Width="400" Height="300" HorizontalAlignment="Left" Margin="10 0 0 0" />
            </StackPanel>

        </StackPanel>
    </Grid>
</Page>

Controls/WebViewDemo/WebViewDemo5.xaml.cs

/*
 * WebView - 内嵌浏览器控件(继承自 FrameworkElement, 请参见 /Controls/BaseControl/FrameworkElementDemo/)
 *     CapturePreviewToStreamAsync() - 对 WebView 当前显示的内容截图,并将图片写入指定的流
 *     
 *     
 * 本例用于演示如何对 WebView 中的内容截图
 */

using System;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Graphics.Imaging;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;

namespace Windows10.Controls.WebViewDemo
{
    public sealed partial class WebViewDemo5 : Page
    {
        public WebViewDemo5()
        {
            this.InitializeComponent();
        }

        private async void btnCapture_Click(object sender, RoutedEventArgs e)
        {
            // 对 WebView 中的内容截图,并将原始图像数据放入内存流
            InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream();
            await webView.CapturePreviewToStreamAsync(ms);

            // 显示原始截图
            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.SetSource(ms);
            imageOriginal.Source = bitmapImage;



            // 定义缩略图的大小(最长边定义为 180)
            int longlength = 180, width = 0, height = 0;
            double srcwidth = webView.ActualWidth, srcheight = webView.ActualHeight;
            double factor = srcwidth / srcheight;
            if (factor < 1)
            {
                height = longlength;
                width = (int)(longlength * factor);
            }
            else
            {
                width = longlength;
                height = (int)(longlength / factor);
            }

            // 显示原始截图的缩略图
            BitmapSource thumbnail = await resize(width, height, ms);
            imageThumbnail.Source = thumbnail;
        }

        // 将指定的图片修改为指定的大小,并返回修改后的图片
        private async Task<BitmapSource> resize(int width, int height, IRandomAccessStream source)
        {
            WriteableBitmap thumbnail = new WriteableBitmap(width, height);
            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(source);
            BitmapTransform transform = new BitmapTransform();
            transform.ScaledHeight = (uint)height;
            transform.ScaledWidth = (uint)width;
            PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
                BitmapPixelFormat.Bgra8,
                BitmapAlphaMode.Straight,
                transform,
                ExifOrientationMode.RespectExifOrientation,
                ColorManagementMode.DoNotColorManage);
            pixelData.DetachPixelData().CopyTo(thumbnail.PixelBuffer);

            return thumbnail;
        }
    }
}


2、演示如何通过 Share Contract 分享 WebView 中的被选中的内容
Controls/WebViewDemo/WebViewDemo6.xaml

<Page
    x:Class="Windows10.Controls.WebViewDemo.WebViewDemo6"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.WebViewDemo"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="Transparent">
        <StackPanel Margin="10 0 10 10">

            <Button Name="btnShare" Content="通过 Share Contract 分享 WebView 中的被选中的内容(如果没有选中任何内容,则分享网页地址)" Click="btnShare_Click" Margin="5" />

            <WebView Name="webView" Width="400" Height="300" Source="http://webabcd.cnblogs.com/" HorizontalAlignment="Left" Margin="5" />

        </StackPanel>
    </Grid>
</Page>

Controls/WebViewDemo/WebViewDemo6.xaml.cs

/*
 * WebView - 内嵌浏览器控件(继承自 FrameworkElement, 请参见 /Controls/BaseControl/FrameworkElementDemo/)
 *     CaptureSelectedContentToDataPackageAsync() - 将选中的内容转换为 DataPackage 对象
 *     DataRequested - 分享操作开始时触发的事件(事件参数 DataRequestedEventArgs)
 * 
 * DataRequestedEventArgs
 *     GetDeferral() - 获取异步操作对象,同时开始异步操作,之后通过 Complete() 通知完成异步操作
 * 
 * 
 * 本例用于演示如何通过 Share Contract 分享 WebView 中的被选中的内容(如果没有选中任何内容,则分享网页地址)
 */

using System;
using Windows.ApplicationModel.DataTransfer;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Windows10.Controls.WebViewDemo
{
    public sealed partial class WebViewDemo6 : Page
    {
        private DataTransferManager _dataTransferManager;

        public WebViewDemo6()
        {
            this.InitializeComponent();
        }

        private void btnShare_Click(object sender, RoutedEventArgs e)
        {
            _dataTransferManager = DataTransferManager.GetForCurrentView();
            _dataTransferManager.DataRequested += _dataTransferManager_DataRequested;

            DataTransferManager.ShowShareUI();
        }

        // 分享 WebView 中的被选中的内容
        async void _dataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            DataRequest request = args.Request;
            DataRequestDeferral deferral = args.Request.GetDeferral();

            // 如果 dataPackage 是 null 的话,则说明用户没有选择任何内容
            DataPackage dataPackage = await webView.CaptureSelectedContentToDataPackageAsync();

            // 用于判断用户是否选中了分享内容
            bool hasSelection = false;
            try
            {
                hasSelection = (dataPackage != null) && (dataPackage.GetView().AvailableFormats.Count > 0);
            }
            catch (Exception ex)
            {
                switch (ex.HResult)
                {
                    // 无法为选中的内容生成 data package
                    case unchecked((int)0x80070490):
                        hasSelection = false;
                        break;
                    default:
                        throw;
                }
            }

            if (hasSelection)
            {
                dataPackage.Properties.Title = "Title(hasSelection)";
            }
            else
            {
                // 用户没有选择任何内容的话,则分享网页地址
                dataPackage = new DataPackage();
                dataPackage.SetWebLink(webView.Source);
                dataPackage.Properties.Title = "Title";
            }

            dataPackage.Properties.Description = "Description";
            request.Data = dataPackage;

            _dataTransferManager.DataRequested -= _dataTransferManager_DataRequested;

            deferral.Complete();
        }
    }
}



OK
[源码下载]

posted @ 2017-09-21 07:43  webabcd  阅读(1613)  评论(2编辑  收藏  举报