原文:背水一战 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

<Pagex: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

<Pagex: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 packagecase 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
[源码下载]

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

  1. 背水一战 Windows 10 (40) - 控件(导航类): AppBar, CommandBar

    原文:背水一战 Windows 10 (40) - 控件(导航类): AppBar, CommandBar [源码下载] 背水一战 Windows 10 (40) - 控件(导航类): AppBar, ...

  2. 背水一战 Windows 10 (47) - 控件(ScrollViewer 特性): Chaining, Rail, Inertia, Snap, Zoom

    原文:背水一战 Windows 10 (47) - 控件(ScrollViewer 特性): Chaining, Rail, Inertia, Snap, Zoom [源码下载] 背水一战 Windo ...

  3. 背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑

    背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑 原文:背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑 [源码 ...

  4. 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu

    原文:背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu [源码下载] 背水一战 Windows 10 (36) - 控件(弹出类): ...

  5. 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox

    背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox 原文:背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox [源码 ...

  6. 背水一战 Windows 10 (70) - 控件(控件基类): UIElement - Transform3D(3D变换), Projection(3D投影)...

    原文:背水一战 Windows 10 (70) - 控件(控件基类): UIElement - Transform3D(3D变换), Projection(3D投影) [源码下载] 背水一战 Wind ...

  7. 背水一战 Windows 10 (55) - 控件(集合类): SemanticZoom, ISemanticZoomInformation

    原文:背水一战 Windows 10 (55) - 控件(集合类): SemanticZoom, ISemanticZoomInformation [源码下载] 背水一战 Windows 10 (55 ...

  8. 背水一战 Windows 10 (41) - 控件(导航类): Frame

    原文:背水一战 Windows 10 (41) - 控件(导航类): Frame [源码下载] 背水一战 Windows 10 (41) - 控件(导航类): Frame 作者:webabcd 介绍 ...

  9. 背水一战 Windows 10 (46) - 控件(ScrollViewer 基础): ScrollViewer, ScrollBar, ScrollContentPresenter...

    原文:背水一战 Windows 10 (46) - 控件(ScrollViewer 基础): ScrollViewer, ScrollBar, ScrollContentPresenter [源码下载 ...

最新文章

  1. Java面向对象:覆写与重载
  2. [zz]正则表达式使用详解
  3. python 命令模式 设计模式_Python 设计模式——命令模式
  4. PAT乙级 1021 个位数统计
  5. 深度学习主流框架介绍(PyTorch、TensorFlow、Keras、Caffe、Theano、MXNET)
  6. 计算机累论文的数据字典怎么写,毕业论文中数据字典应该这样写
  7. CalHypso——用于计算面积高程积分(HI)曲线的ArcGis扩展工具
  8. 想自学单片机,各位有什么书和板子值得推荐的吗?
  9. 栅格模型数据编码方式
  10. 服务器主板如何连接无线网卡,无线网卡如何设置WiFi共享模式?
  11. 产品读书《Facebook效应:看Facebook如何打造无与伦比的社交帝国》
  12. 分布式系统中可用性及容错性的区别
  13. 第三方支付接口搜集(附下载)
  14. HDMI RGB_TO_DVI模块
  15. Linux中如何安装特定的gcc版本
  16. Qsetting本地缓存PC端数据
  17. MySQL为JSON字段创建索引(Multi-Valued Indexes 多值索引)
  18. 计算机英语 译文,计算机英语参考译文
  19. Pytorch(GPU)配环境原理:cuda+cudnn+pytorch配环境的每一步到底干了些什么?
  20. 二手前端入门React项目

热门文章

  1. 还在用if(obj!=null)做非空判断?带你快速上手Optional实战性理解!
  2. 彻底搞懂MySQL分区
  3. 优秀的代码都是如何分层的?
  4. 一次 Jar 包升级引发的血案 amp; 解决
  5. 两个小模型就能吊打大模型!北大校友、谷歌华人一作「模型集合」,CNN、Transformer都适用!...
  6. 时隔16年,Science再次发布“全世界最前沿的125个科学问题”!
  7. 程序员加班崩溃,过路外卖小哥主动帮忙改代码,网友直呼太暖了!
  8. 美团语音交互部招聘NLP/知识图谱/语音识别等算法职位(可实习)
  9. 还需要“attention”吗?一堆“前馈层”在ImageNet上表现得出奇得好
  10. 看着像学生,实为博导、教授!发过4篇CNS,刚刚又获重要大奖