protected override async void OnNavigatedTo(NavigationEventArgs e) { try { if (e.Parameter is ShareOperation operation) { //Get text data if (operation.Data.Contains(StandardDataFormats.Text)) { string textFromShare = await operation.Data.GetTextAsync(); //Pages.TextPage.Text = textFromShare; ContentFrame.Navigate(typeof(Pages.TextPage), textFromShare); } //Get web link else if (operation.Data.Contains(StandardDataFormats.WebLink)) { Uri uri = await operation.Data.GetWebLinkAsync(); //Pages.QRCodePage.Text = uri.ToString(); ContentFrame.Navigate(typeof(Pages.QRCodePage), uri.ToString()); } //Get image else if (operation.Data.Contains(StandardDataFormats.Bitmap)) { ContentFrame.Navigate(typeof(Pages.ImagePage)); //shareType.Text = "Bitmap"; //shareTitle.Text = operation.Data.Properties.Title; //imgShareImage.Visibility = Visibility.Visible; //tbShareData.Visibility = Visibility.Collapsed; RandomAccessStreamReference imageStreamRef = await operation.Data.GetBitmapAsync(); IRandomAccessStreamWithContentType streamWithContentType = await imageStreamRef.OpenReadAsync(); WriteableBitmap bmp = new WriteableBitmap(1, 1); await bmp.SetSourceAsync(streamWithContentType); //imgShareImage.Source = bmp; ContentFrame.Navigate(typeof(Pages.QRCodePage), bmp); } else if (operation.Data.Contains(StandardDataFormats.StorageItems)) { var files = await operation.Data.GetStorageItemsAsync(); if (files.Count > 0) { //StorageFile file = await StorageFile.GetFileFromPathAsync(files[0].Path); StorageFile storageFile = (StorageFile)files[0]; if (storageFile.IsOfType(StorageItemTypes.File)) { var ext = storageFile.FileType.ToLower(); if (Utils.image_ext.Contains(ext)) { if (ext.Equals(".svg")) { var svg = await SVG.CreateFromStorageFile(storageFile); ContentFrame.Navigate(typeof(Pages.SvgPage), svg); } else { var bitmapImage = new WriteableBitmap(1, 1); await bitmapImage.SetSourceAsync(await storageFile.OpenAsync(FileAccessMode.Read)); byte[] arr = WindowsRuntimeBufferExtensions.ToArray(bitmapImage.PixelBuffer, 0, (int)bitmapImage.PixelBuffer.Length); ContentFrame.Navigate(typeof(Pages.QRCodePage), bitmapImage); } } else if (ext.Equals(".txt", StringComparison.CurrentCultureIgnoreCase)) { var txt = await FileIO.ReadTextAsync(storageFile); ContentFrame.Navigate(typeof(Pages.TextPage), txt); } } } } //operation.ReportCompleted(); } else { ContentFrame.Navigate(typeof(Pages.TextPage), this); } } catch (Exception ex) { ex.Message.T().ShowMessage("ERROR".T()); } }
private async void OnDrop(object sender, DragEventArgs e) { // 需要异步拖放时记得获取Deferral对象 //var def = e.GetDeferral(); if (sender == imgSvg || sender == BackgroundCanvas) { if (e.DataView.Contains(StandardDataFormats.StorageItems)) { var items = await e.DataView.GetStorageItemsAsync(); if (items.Count > 0) { try { var storageFile = items[0] as StorageFile; SourceFileName = storageFile.Name; if (Utils.image_ext.Contains(storageFile.FileType.ToLower())) { if (storageFile.FileType.ToLower().Equals(".svg")) { var bitmapImage = new SvgImageSource(); if (e.DataView.Contains(StandardDataFormats.WebLink)) { var url = await e.DataView.GetWebLinkAsync(); var rms = await RandomAccessStreamReference.CreateFromUri(url).OpenReadAsync(); var svg = await SVG.CreateFromStream(rms); bitmapImage = svg.Source; imgSvg.Tag = svg.Bytes; } else if (e.DataView.Contains(StandardDataFormats.Text)) { var content = await e.DataView.GetTextAsync(); if (content.Length > 0) { var rms = await RandomAccessStreamReference.CreateFromUri(new Uri(content)).OpenReadAsync(); var svg = await SVG.CreateFromStream(rms); bitmapImage = svg.Source; imgSvg.Tag = svg.Bytes; } } else { var svg = await SVG.CreateFromStorageFile(storageFile); bitmapImage = svg.Source; imgSvg.Tag = svg.Bytes; } imgSvg.Source = bitmapImage; viewSvg.StretchDirection = StretchDirection.Both; } else if (storageFile.FileType.ToLower().Equals(".xaml")) { //var xaml = await FileIO.ReadTextAsync(storageFile); //var ui = xaml.LoadXAML(); } else { imgSvg.Source = await storageFile.ToWriteableBitmap(); viewSvg.StretchDirection = StretchDirection.DownOnly; } } } catch (Exception ex) { await new MessageDialog(ex.Message.T(), "ERROR".T()).ShowAsync(); } } } else if (e.DataView.Contains(StandardDataFormats.WebLink)) { var uri = await e.DataView.GetWebLinkAsync(); try { StorageFile storageFile = await StorageFile.GetFileFromApplicationUriAsync(uri); SourceFileName = storageFile.Name; if (Utils.image_ext.Contains(storageFile.FileType.ToLower())) { if (storageFile.FileType.ToLower().Equals(".svg")) { var bitmapImage = new SvgImageSource(); if (e.DataView.Contains(StandardDataFormats.WebLink)) { var url = await e.DataView.GetWebLinkAsync(); var rms = await RandomAccessStreamReference.CreateFromUri(url).OpenReadAsync(); var svg = await SVG.CreateFromStream(rms); bitmapImage = svg.Source; imgSvg.Tag = svg.Bytes; } else if (e.DataView.Contains(StandardDataFormats.Text)) { var content = await e.DataView.GetTextAsync(); if (content.Length > 0) { var rms = await RandomAccessStreamReference.CreateFromUri(new Uri(content)).OpenReadAsync(); var svg = await SVG.CreateFromStream(rms); bitmapImage = svg.Source; imgSvg.Tag = svg.Bytes; } } else { var svg = await SVG.CreateFromStorageFile(storageFile); bitmapImage = svg.Source; imgSvg.Tag = svg.Bytes; } imgSvg.Source = bitmapImage; } } } catch (Exception ex) { await new MessageDialog(ex.Message.T(), "ERROR".T()).ShowAsync(); } } } //def.Complete(); }
private async void OnDrop(object sender, DragEventArgs e) { // 记得获取Deferral对象 //var def = e.GetDeferral(); if (sender == imgQR || sender == rectDrop) { if (e.DataView.Contains(StandardDataFormats.StorageItems)) { var items = await e.DataView.GetStorageItemsAsync(); if (items.Count > 0) { var storageFile = items[0] as StorageFile; if (Utils.image_ext.Contains(storageFile.FileType.ToLower())) { if (storageFile.FileType.ToLower().Equals(".svg")) { var bitmapImage = new SvgImageSource(); if (e.DataView.Contains(StandardDataFormats.WebLink)) { var url = await e.DataView.GetWebLinkAsync(); var rms = await RandomAccessStreamReference.CreateFromUri(url).OpenReadAsync(); var svg = await SVG.CreateFromStream(rms); bitmapImage = svg.Source; imgQR.Tag = svg.Bytes; //await bitmapImage.SetSourceAsync(rms); //await rms.FlushAsync(); //var bytes = WindowsRuntimeStreamExtensions.AsStreamForRead(rms.GetInputStreamAt(0)); //imgQR.Tag = bytes; } else if (e.DataView.Contains(StandardDataFormats.Text)) { //var content = await e.DataView.GetHtmlFormatAsync(); var content = await e.DataView.GetTextAsync(); if (content.Length > 0) { var rms = await RandomAccessStreamReference.CreateFromUri(new Uri(content)).OpenReadAsync(); var svg = await SVG.CreateFromStream(rms); bitmapImage = svg.Source; imgQR.Tag = svg.Bytes; //await bitmapImage.SetSourceAsync(rms); //await rms.FlushAsync(); //var bytes = WindowsRuntimeStreamExtensions.AsStreamForRead(rms.GetInputStreamAt(0)); //imgQR.Tag = bytes; } } else { var svg = await SVG.CreateFromStorageFile(storageFile); bitmapImage = svg.Source; imgQR.Tag = svg.Bytes; //await bitmapImage.SetSourceAsync(await storageFile.OpenReadAsync()); //byte[] bytes = WindowsRuntimeBufferExtensions.ToArray(await FileIO.ReadBufferAsync(storageFile)); //imgQR.Tag = bytes; } imgQR.Source = bitmapImage; } else { var bitmapImage = new WriteableBitmap(1, 1); if (e.DataView.Contains(StandardDataFormats.WebLink)) { var url = await e.DataView.GetWebLinkAsync(); await bitmapImage.SetSourceAsync(await RandomAccessStreamReference.CreateFromUri(url).OpenReadAsync()); } else if (e.DataView.Contains(StandardDataFormats.Text)) { //var content = await e.DataView.GetHtmlFormatAsync(); var content = await e.DataView.GetTextAsync(); if (content.Length > 0) { await bitmapImage.SetSourceAsync(await RandomAccessStreamReference.CreateFromUri(new Uri(content)).OpenReadAsync()); } } else { await bitmapImage.SetSourceAsync(await storageFile.OpenReadAsync()); } byte[] arr = WindowsRuntimeBufferExtensions.ToArray(bitmapImage.PixelBuffer, 0, (int)bitmapImage.PixelBuffer.Length); imgQR.Source = bitmapImage; } } } } else if (e.DataView.Contains(StandardDataFormats.WebLink)) { var uri = await e.DataView.GetWebLinkAsync(); StorageFile storageFile = await StorageFile.GetFileFromApplicationUriAsync(uri); if (Utils.image_ext.Contains(storageFile.FileType.ToLower())) { if (storageFile.FileType.ToLower().Equals(".svg")) { var bitmapImage = new SvgImageSource(); if (e.DataView.Contains(StandardDataFormats.WebLink)) { var url = await e.DataView.GetWebLinkAsync(); var rms = await RandomAccessStreamReference.CreateFromUri(url).OpenReadAsync(); var svg = await SVG.CreateFromStream(rms); bitmapImage = svg.Source; imgQR.Tag = svg.Bytes; //await bitmapImage.SetSourceAsync(rms); //await rms.FlushAsync(); //var bytes = WindowsRuntimeStreamExtensions.AsStreamForRead(rms.GetInputStreamAt(0)); //imgQR.Tag = bytes; } else if (e.DataView.Contains(StandardDataFormats.Text)) { //var content = await e.DataView.GetHtmlFormatAsync(); var content = await e.DataView.GetTextAsync(); if (content.Length > 0) { var rms = await RandomAccessStreamReference.CreateFromUri(new Uri(content)).OpenReadAsync(); var svg = await SVG.CreateFromStream(rms); bitmapImage = svg.Source; imgQR.Tag = svg.Bytes; //await bitmapImage.SetSourceAsync(rms); //await rms.FlushAsync(); //var bytes = WindowsRuntimeStreamExtensions.AsStreamForRead(rms.GetInputStreamAt(0)); //imgQR.Tag = bytes; } } else { var svg = await SVG.CreateFromStorageFile(storageFile); bitmapImage = svg.Source; imgQR.Tag = svg.Bytes; //await bitmapImage.SetSourceAsync(await storageFile.OpenReadAsync()); //byte[] bytes = WindowsRuntimeBufferExtensions.ToArray(await FileIO.ReadBufferAsync(storageFile)); //imgQR.Tag = bytes; } imgQR.Source = bitmapImage; } else { var bitmapImage = new WriteableBitmap(1, 1); if (e.DataView.Contains(StandardDataFormats.WebLink)) { var url = await e.DataView.GetWebLinkAsync(); await bitmapImage.SetSourceAsync(await RandomAccessStreamReference.CreateFromUri(url).OpenReadAsync()); } else if (e.DataView.Contains(StandardDataFormats.Text)) { //var content = await e.DataView.GetHtmlFormatAsync(); var content = await e.DataView.GetTextAsync(); if (content.Length > 0) { await bitmapImage.SetSourceAsync(await RandomAccessStreamReference.CreateFromUri(new Uri(content)).OpenReadAsync()); } } else { await bitmapImage.SetSourceAsync(await storageFile.OpenReadAsync()); } byte[] arr = WindowsRuntimeBufferExtensions.ToArray(bitmapImage.PixelBuffer, 0, (int)bitmapImage.PixelBuffer.Length); imgQR.Source = bitmapImage; } } //var bitmapImage = new WriteableBitmap(1, 1); //await bitmapImage.SetSourceAsync(await file.OpenAsync(FileAccessMode.Read)); //// Set the image on the main page to the dropped image //byte[] arr = WindowsRuntimeBufferExtensions.ToArray(bitmapImage.PixelBuffer, 0, (int)bitmapImage.PixelBuffer.Length); //imgQR.Source = bitmapImage; } } else if (sender == edQR) { if (e.DataView.Contains(StandardDataFormats.StorageItems)) { var items = await e.DataView.GetStorageItemsAsync(); if (items.Count > 0) { var storageFile = items[0] as StorageFile; if (Utils.text_ext.Contains(storageFile.FileType.ToLower())) { edQR.Text = await FileIO.ReadTextAsync(storageFile); } } } else if (e.DataView.Contains(StandardDataFormats.Text) || e.DataView.Contains(StandardDataFormats.WebLink) || e.DataView.Contains(StandardDataFormats.Html) || e.DataView.Contains(StandardDataFormats.Rtf)) { var content = await e.DataView.GetTextAsync(); if (content.Length > 0) { edQR.Text = content; } } } //def.Complete(); }
private async void AppBarButton_Click(object sender, RoutedEventArgs e) { var btn = sender as AppBarButton; switch (btn.Name) { case "btnOpenFile": FileOpenPicker fop = new FileOpenPicker { SuggestedStartLocation = PickerLocationId.Desktop }; foreach (var ext in Utils.image_ext) { fop.FileTypeFilter.Add(ext); } var file = await fop.PickSingleFileAsync(); if (file != null) { SourceFileName = file.Name; if (file.FileType.ToLower().Equals(".svg")) { var svgData = await SVG.CreateFromStorageFile(file); imgSvg.Source = svgData.Source; imgSvg.Tag = svgData.Bytes; viewSvg.StretchDirection = StretchDirection.Both; } //else if (file.FileType.ToLower().Equals(".xaml")) //{ // var xamldoc = await FileIO.ReadTextAsync(file); // var xaml = XamlReader.Load(xamldoc); // //DrawingBrush db = new DrawingBrush(); //} else { imgSvg.Source = await file.ToWriteableBitmap(); viewSvg.StretchDirection = StretchDirection.DownOnly; } } break; case "btnMake": if (CURRENT_ICONS.Equals("win", StringComparison.CurrentCultureIgnoreCase)) { MakeImages(new List <int>() { 256, 128, 96, 72, 64, 48, 32, 24, 16 }); } else if (CURRENT_ICONS.Equals("uwp", StringComparison.CurrentCultureIgnoreCase)) { MakeImages(new List <int>() { 1024, 512 }); } break; case "btnCopy": Utils.SetClipboard(imgSvg, -1); break; case "btnPaste": var svgdoc = await Utils.GetClipboard("", imgSvg); var svg = await svgdoc.DecodeSvg(); if (svg.Source is SvgImageSource) { imgSvg.Source = svg.Source; } else if (svg.Image is BitmapSource || svg.Image is WriteableBitmap) { imgSvg.Source = svg.Image; } imgSvg.Tag = svg.Bytes is byte[] ? svg.Bytes.Clone() : null; break; case "btnSave": await Utils.ShowSaveDialog(imgSvg); break; case "btnShare": await Utils.Share(await imgSvg.ToWriteableBitmap()); break; default: break; } }
private async void OnDrop(object sender, DragEventArgs e) { // 需要异步拖放时记得获取Deferral对象 //var def = e.GetDeferral(); if (sender == imgBase64 || sender == rectDrop) { if (e.DataView.Contains(StandardDataFormats.StorageItems)) { var items = await e.DataView.GetStorageItemsAsync(); if (items.Count > 0) { var storageFile = items[0] as StorageFile; if (Utils.image_ext.Contains(storageFile.FileType.ToLower())) { if (storageFile.FileType.ToLower().Equals(".svg")) { var bitmapImage = new SvgImageSource(); if (e.DataView.Contains(StandardDataFormats.WebLink)) { var url = await e.DataView.GetWebLinkAsync(); var rms = await RandomAccessStreamReference.CreateFromUri(url).OpenReadAsync(); var svg = await SVG.CreateFromStream(rms); bitmapImage = svg.Source; imgBase64.Tag = svg.Bytes; } else if (e.DataView.Contains(StandardDataFormats.Text)) { var content = await e.DataView.GetTextAsync(); if (content.Length > 0) { var rms = await RandomAccessStreamReference.CreateFromUri(new Uri(content)).OpenReadAsync(); var svg = await SVG.CreateFromStream(rms); bitmapImage = svg.Source; imgBase64.Tag = svg.Bytes; } } else { var svg = await SVG.CreateFromStorageFile(storageFile); bitmapImage = svg.Source; imgBase64.Tag = svg.Bytes; } imgBase64.Source = bitmapImage; } else { var bitmapImage = new WriteableBitmap(1, 1); if (e.DataView.Contains(StandardDataFormats.WebLink)) { var url = await e.DataView.GetWebLinkAsync(); await bitmapImage.SetSourceAsync(await RandomAccessStreamReference.CreateFromUri(url).OpenReadAsync()); } else if (e.DataView.Contains(StandardDataFormats.Text)) { //var content = await e.DataView.GetHtmlFormatAsync(); var content = await e.DataView.GetTextAsync(); if (content.Length > 0) { await bitmapImage.SetSourceAsync(await RandomAccessStreamReference.CreateFromUri(new Uri(content)).OpenReadAsync()); } } else { await bitmapImage.SetSourceAsync(await storageFile.OpenReadAsync()); } byte[] arr = WindowsRuntimeBufferExtensions.ToArray(bitmapImage.PixelBuffer, 0, (int)bitmapImage.PixelBuffer.Length); imgBase64.Source = bitmapImage; } } } } else if (e.DataView.Contains(StandardDataFormats.WebLink)) { var uri = await e.DataView.GetWebLinkAsync(); StorageFile storageFile = await StorageFile.GetFileFromApplicationUriAsync(uri); if (Utils.image_ext.Contains(storageFile.FileType.ToLower())) { if (storageFile.FileType.ToLower().Equals(".svg")) { var bitmapImage = new SvgImageSource(); if (e.DataView.Contains(StandardDataFormats.WebLink)) { var url = await e.DataView.GetWebLinkAsync(); var rms = await RandomAccessStreamReference.CreateFromUri(url).OpenReadAsync(); var svg = await SVG.CreateFromStream(rms); bitmapImage = svg.Source; imgBase64.Tag = svg.Bytes; } else if (e.DataView.Contains(StandardDataFormats.Text)) { //var content = await e.DataView.GetHtmlFormatAsync(); var content = await e.DataView.GetTextAsync(); if (content.Length > 0) { var rms = await RandomAccessStreamReference.CreateFromUri(new Uri(content)).OpenReadAsync(); var svg = await SVG.CreateFromStream(rms); bitmapImage = svg.Source; imgBase64.Tag = svg.Bytes; } } else { await bitmapImage.SetSourceAsync(await storageFile.OpenReadAsync()); byte[] bytes = WindowsRuntimeBufferExtensions.ToArray(await FileIO.ReadBufferAsync(storageFile)); imgBase64.Tag = bytes; } imgBase64.Source = bitmapImage; } else { var bitmapImage = new WriteableBitmap(1, 1); if (e.DataView.Contains(StandardDataFormats.WebLink)) { var url = await e.DataView.GetWebLinkAsync(); await bitmapImage.SetSourceAsync(await RandomAccessStreamReference.CreateFromUri(url).OpenReadAsync()); } else if (e.DataView.Contains(StandardDataFormats.Text)) { var content = await e.DataView.GetTextAsync(); if (content.Length > 0) { await bitmapImage.SetSourceAsync(await RandomAccessStreamReference.CreateFromUri(new Uri(content)).OpenReadAsync()); } } else { await bitmapImage.SetSourceAsync(await storageFile.OpenReadAsync()); } byte[] arr = WindowsRuntimeBufferExtensions.ToArray(bitmapImage.PixelBuffer, 0, (int)bitmapImage.PixelBuffer.Length); imgBase64.Source = bitmapImage; } } } } else if (sender == edBase64) { if (e.DataView.Contains(StandardDataFormats.StorageItems)) { var items = await e.DataView.GetStorageItemsAsync(); if (items.Count > 0) { var storageFile = items[0] as StorageFile; if (Utils.url_ext.Contains(storageFile.FileType.ToLower())) { if (e.DataView.Contains(StandardDataFormats.WebLink)) { var url = await e.DataView.GetWebLinkAsync(); if (url.IsAbsoluteUri) { edBase64.Text = url.ToString(); } else if (url.IsFile || url.IsUnc) { edBase64.Text = await FileIO.ReadTextAsync(storageFile); } } else if (e.DataView.Contains(StandardDataFormats.Text)) { //var content = await e.DataView.GetHtmlFormatAsync(); var content = await e.DataView.GetTextAsync(); if (content.Length > 0) { edBase64.Text = content; } } } else if (Utils.text_ext.Contains(storageFile.FileType.ToLower())) { edBase64.Text = await FileIO.ReadTextAsync(storageFile); } } } else if (e.DataView.Contains(StandardDataFormats.WebLink)) { var uri = await e.DataView.GetWebLinkAsync(); StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri); if (!file.FileType.ToLower().Equals(".txt")) { return; } edBase64.Text = await FileIO.ReadTextAsync(file); } else if (e.DataView.Contains(StandardDataFormats.Text)) { var content = await e.DataView.GetTextAsync(); if (content.Length > 0) { edBase64.Text = content; } } } await IsSVG(imgBase64); //def.Complete(); }