首页 技术 正文
技术 2022年11月18日
0 收藏 932 点赞 4,277 浏览 5615 个字

[源码下载]

背水一战 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 = , width = , height = ;
double srcwidth = webView.ActualWidth, srcheight = webView.ActualHeight;
double factor = srcwidth / srcheight;
if (factor < )
{
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 > );
}
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
[源码下载]

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,816
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,899