个人博客同步CSDN
剪贴板类:Windows.ApplicationModel.DataTransfer.ClipBoard
剪贴板有两个事件,都是静态方法:
- 剪贴板内容改变事件:Clipboard.ContentChanged
- 剪贴板历史改变事件:Clipboard.HistoryChanged
但,历史改变事件无法监听文件复制操作
内容改变事件
具体操作代码如下:
//添加事件
Clipboard.ContentChanged += Clipboard_ContentChanged;
private void Clipboard_ContentChanged(object sender, object e)
{
Debug.WriteLine("ContentChanged");
if (Clipboard.GetContent().Contains(StandardDataFormats.ApplicationLink))
{
Debug.WriteLine("exists: ApplicationLink");
}
else if (Clipboard.GetContent().Contains(StandardDataFormats.Bitmap))
{
Debug.WriteLine("exists: Bitmap");
}
else if (Clipboard.GetContent().Contains(StandardDataFormats.Html))
{
Debug.WriteLine("exists: Html");
}
else if (Clipboard.GetContent().Contains(StandardDataFormats.Rtf))
{
Debug.WriteLine("exists: Rtf");
}
else if (Clipboard.GetContent().Contains(StandardDataFormats.StorageItems))
{
Debug.WriteLine("exists: StorageItems");
}
else if (Clipboard.GetContent().Contains(StandardDataFormats.Text))
{
Debug.WriteLine("exists: Text");
}
else if (Clipboard.GetContent().Contains(StandardDataFormats.Uri))
{
Debug.WriteLine("exists: Uri");
}
else if (Clipboard.GetContent().Contains(StandardDataFormats.UserActivityJsonArray))
{
Debug.WriteLine("exists: UserActivityJsonArray");
}
else if (Clipboard.GetContent().Contains(StandardDataFormats.WebLink))
{
Debug.WriteLine("exists: WebLink");
}
}
获取文件列表
StorageItems
就是存储的文件的内容了,如果要具体获得复制的文件的路径可以这样写:
Clipboard.GetContent()
是 DataPackageView
类型的,可以从这里
private async static void FileCopied(DataPackageView content)
{
var filelist = await content.GetStorageItemsAsync();
foreach (IStorageItem item in filelist)
{
if (item.IsOfType(StorageItemTypes.File))
{
StorageFile file = item as StorageFile;
Debug.WriteLine(file.Path + " " + file.Name);
}
else if (item.IsOfType(StorageItemTypes.Folder))
{
Debug.WriteLine("是文件夹");
}
}
}
图片操作
对于图片操作如下:
private async static void BitmapCopied(DataPackageView content)
{
RandomAccessStreamReference file = await content.GetBitmapAsync();
IRandomAccessStream stream = await file.OpenReadAsync();
//直接显示图片
//BitmapImage image = new BitmapImage();
//await image.SetSourceAsync(stream);
//Image img = new Image();
//img.Source = image;
//转为字节数组
byte[] bytes = StreamToBytes(stream.AsStream());
Debug.WriteLine(bytes.Length);
}
public static byte[] StreamToBytes(Stream stream)
{
byte[] buffer = null;
try
{
if (stream != null && stream.Length > 0)
{
//很重要,因为Position经常位于Stream的末尾,导致下面读取到的长度为0。
stream.Position = 0;
using (BinaryReader br = new BinaryReader(stream))
{
buffer = br.ReadBytes((int)stream.Length);
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
return buffer;
}
如果要把图片保存下来可以参考这个
if (con.Contains(StandardDataFormats.Bitmap))
{
RandomAccessStreamReference img = await con.GetBitmapAsync();
var imgstream = await img.OpenReadAsync();
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(imgstream);
Windows.UI.Xaml.Media.Imaging.WriteableBitmap src = new Windows.UI.Xaml.Media.Imaging.WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight);
src.SetSource(imgstream);
Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(imgstream);
Windows.Graphics.Imaging.PixelDataProvider pxprd = await decoder.GetPixelDataAsync(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, Windows.Graphics.Imaging.BitmapAlphaMode.Straight, new Windows.Graphics.Imaging.BitmapTransform(), Windows.Graphics.Imaging.ExifOrientationMode.RespectExifOrientation, Windows.Graphics.Imaging.ColorManagementMode.DoNotColorManage);
byte[] buffer = pxprd.DetachPixelData();
str = "image";
StorageFolder folder = await _folder.GetFolderAsync(str);
StorageFile file = await folder.CreateFileAsync(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + ".png", CreationCollisionOption.GenerateUniqueName);
using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.PngEncoderId, fileStream);
encoder.SetPixelData(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, Windows.Graphics.Imaging.BitmapAlphaMode.Straight, decoder.PixelWidth, decoder.PixelHeight, decoder.DpiX, decoder.DpiY, buffer);
await encoder.FlushAsync();
}
}