private async void MyBtn_Click(object sender, RoutedEventArgs e) { using (HttpClient client = new HttpClient()) { var svg = new SvgImageSource(); try { var response = await client.GetAsync(new Uri("http://www.iconsvg.com/Home/downloadPicture?route=1333158662629220352&name=%E5%9C%86%E5%BD%A2%E7%9A%84_circular164")); if (response != null && response.StatusCode == HttpStatusCode.OK) { using (var stream = await response.Content.ReadAsStreamAsync()) { using (var memStream = new MemoryStream()) { await stream.CopyToAsync(memStream); memStream.Position = 0; await svg.SetSourceAsync(memStream.AsRandomAccessStream()); MyImageView.Source = svg; } } } } catch (Exception ex) { } } }
/// <inheritdoc /> public override async Task InvokeAsync(ILoadingContext <ImageSource> context, LoadingPipeDelegate <ImageSource> next, CancellationToken cancellationToken = default) { if (context.Current is Stream stream) { if (!stream.CanSeek) { var memoryStream = new MemoryStream(); await stream.CopyToAsync(memoryStream); if (!ReferenceEquals(stream, context.OriginSource)) { // if the stream generated by the pipe then dispose it. stream.Dispose(); } memoryStream.Seek(0, SeekOrigin.Begin); stream = memoryStream; } var isSvg = IsSvg(stream); var tcs = new TaskCompletionSource <object?>(); context.InvokeOnUIThread(async() => { try { if (isSvg) { var bitmap = new SvgImageSource(); context.AttachSource(bitmap); var svgImageLoadStatus = await bitmap.SetSourceAsync(stream.AsRandomAccessStream()); if (svgImageLoadStatus != SvgImageSourceLoadStatus.Success) { throw new SvgImageFailedStatusException(svgImageLoadStatus); } cancellationToken.ThrowIfCancellationRequested(); context.Current = bitmap; } else { // https://docs.microsoft.com/en-us/windows/uwp/debug-test-perf/optimize-animations-and-media?redirectedfrom=MSDN#right-sized-decoding // Set source after attached to the XAML tree var bitmap = new BitmapImage(); context.AttachSource(bitmap); await bitmap.SetSourceAsync(stream.AsRandomAccessStream()); cancellationToken.ThrowIfCancellationRequested(); context.Current = bitmap; } tcs.SetResult(null); } catch (Exception ex) { tcs.SetException(ex); } }); await tcs.Task; } await next(context, cancellationToken); }
public object Convert(object value, Type targetType, object parameter, string language) { var svg = new SvgImageSource(); using (HttpClient client = new HttpClient()) { try { var response = client.GetAsync(new Uri(value as string)).Result; if (response != null && response.StatusCode == HttpStatusCode.OK) { using (var stream = response.Content.ReadAsStreamAsync().Result) { using (var memStream = new MemoryStream()) { stream.CopyToAsync(memStream); memStream.Position = 0; svg.SetSourceAsync(memStream.AsRandomAccessStream()); } } } } catch (Exception ex) { } } return(svg); }
public async void Open(Image display) { try { FileOpenPicker picker = new FileOpenPicker() { SuggestedStartLocation = PickerLocationId.PicturesLibrary }; picker.FileTypeFilter.Add(".svg"); StorageFile open = await picker.PickSingleFileAsync(); if (open != null) { SvgImageSource source = new SvgImageSource() { RasterizePixelHeight = display.ActualHeight, RasterizePixelWidth = display.ActualWidth }; if (await source.SetSourceAsync(await open.OpenReadAsync()) == SvgImageSourceLoadStatus.Success) { display.Source = source; } } } finally { // Ignore Exceptions } }
private async void RenderFourierTransform() { var stream = await ImageListClass.FileSourceList[myID].OpenAsync(FileAccessMode.Read); SvgImageSource image = new SvgImageSource(); await image.SetSourceAsync(stream); NormalImageControl.Source = image; }
public static async Task <SvgImageSource> StorageFileToSvgImage(StorageFile savedStorageFile) { using (var fileStream = await savedStorageFile.OpenAsync(FileAccessMode.Read)) { var svgImage = new SvgImageSource(); await svgImage.SetSourceAsync(fileStream); return(svgImage); } }
private async void LoadSvg() { generator = new SVGGenerator(); await generator.LoadSvg(); var generationCancellationTokenSource = new CancellationTokenSource(); var souce = await generator.GetSourceAsync(generationCancellationTokenSource.Token); SvgSource = new SvgImageSource(); await SvgSource.SetSourceAsync(souce); }
private async Task RenderSvg() { using (var stream = new InMemoryRandomAccessStream()) { await RenderSvg(stream); var image = new SvgImageSource(); await image.SetSourceAsync(stream); ResultSvgImage.Source = image; } }
private async void OpenFile(StorageFile file) { ImageListClass.FileSourceList[myID] = file; var stream = await ImageListClass.FileSourceList[myID].OpenAsync(FileAccessMode.Read); SvgImageSource image = new SvgImageSource(); await image.SetSourceAsync(stream); ImageControl.Source = image; noImageText.Visibility = Visibility.Collapsed; MetadataButton.Visibility = Visibility.Visible; FourierButton.Visibility = Visibility.Visible; ClearMetadataButton.Visibility = Visibility.Visible; }
private async void SignIn_Click(object sender, RoutedEventArgs e) { viewModel.Loading = true; await Dispatcher.YieldAsync(); try { var result = await(string.IsNullOrEmpty(viewModel.Captcha) ? UserModel.SignInAsync(viewModel.UserName, viewModel.Password) : UserModel.SignInAsync(viewModel.UserName, viewModel.Password, viewModel.Captcha)); if (result?.Data == null) { throw new InvalidOperationException("Network Error"); } this.parameters?.ShowMessage?.Invoke(result.Message); this.parameters?.UpdateUserData?.Invoke(result.Data); if (!result.Data.Captcha) { return; } var captcha = await UserModel.FetchCaptchaAsync(); var stream = new MemoryStream(); using var writer = new StreamWriter(stream); await writer.WriteAsync(captcha.Data?.Captcha); await writer.FlushAsync(); stream.Position = 0; var svg = new SvgImageSource { RasterizePixelWidth = 150, RasterizePixelHeight = 50 }; await svg.SetSourceAsync(stream.AsRandomAccessStream()); viewModel.CaptchaData = svg; } catch (Exception ex) { App.AppConfiguration.SavedUserName = ""; App.AppConfiguration.SavedPassword = ""; this.parameters?.ShowMessage?.Invoke(ex.Message); } finally { viewModel.Loading = false; } }
private async Task LoadCaptcha() { string captcha = ""; try { captcha = await Model.MatrixRequest.GetCaptcha(); } catch (MatrixException.NetworkError) { ShowMessage("网络错误,无法获取验证码"); } // cast string to IRandomAccessStream var stream = new MemoryStream(); using (var writer = new StreamWriter(stream)) { writer.Write(captcha); writer.Flush(); stream.Position = 0; var svg = new SvgImageSource(); await svg.SetSourceAsync(stream.AsRandomAccessStream()); viewModel.CaptchaSource = svg; } }
public override async Task InvokeAsync(ILoadingContext <ImageSource> context, PipeDelegate <ImageSource> next, CancellationToken cancellationToken = default(CancellationToken)) { if (context.Current is Stream stream) { var isStartWithLessThanSign = false; if (stream.CanSeek) { stream.Seek(0, SeekOrigin.Begin); isStartWithLessThanSign = stream.ReadByte() == '<'; // svg start with < stream.Seek(0, SeekOrigin.Begin); } if (isStartWithLessThanSign) { var bitmap = new SvgImageSource(); var svgImageLoadStatus = await bitmap.SetSourceAsync(stream.AsRandomAccessStream()); if (svgImageLoadStatus != SvgImageSourceLoadStatus.Success) { throw new SvgImageFailedStatusException(svgImageLoadStatus); } cancellationToken.ThrowIfCancellationRequested(); context.Current = bitmap; } else { var bitmap = new BitmapImage(); await bitmap.SetSourceAsync(stream.AsRandomAccessStream()); cancellationToken.ThrowIfCancellationRequested(); context.Current = bitmap; } } await next(context, cancellationToken); }
private async void List_SelectionChanged(object sender, SelectionChangedEventArgs e) { _cancelGetDetails.Cancel(false); _cancelGetDetails.Dispose(); _cancelGetDetails = new CancellationTokenSource(); if (_list.SelectedItem is DriveItem driveItem && driveItem.File != null) { try { SelectedFile = driveItem; FileSize = driveItem.Size ?? 0; LastModified = driveItem.LastModifiedDateTime?.LocalDateTime.ToString() ?? string.Empty; if (FileSelected != null) { FileSelected.Invoke(this, new FileSelectedEventArgs(driveItem)); } ThumbnailImageSource = null; VisualStateManager.GoToState(this, NavStatesFileReadonly, false); GraphServiceClient graphClient = await GraphServiceHelper.GetGraphServiceClientAsync(); if (graphClient != null) { Task <IDriveItemPermissionsCollectionPage> taskPermissions = graphClient.Drives[_driveId].Items[driveItem.Id].Permissions.Request().GetAsync(_cancelGetDetails.Token); IDriveItemPermissionsCollectionPage permissions = await taskPermissions; if (!taskPermissions.IsCanceled) { foreach (Permission permission in permissions) { if (permission.Roles.Contains("write") || permission.Roles.Contains("owner")) { VisualStateManager.GoToState(this, NavStatesFileEdit, false); break; } } Task <IDriveItemThumbnailsCollectionPage> taskThumbnails = graphClient.Drives[_driveId].Items[driveItem.Id].Thumbnails.Request().GetAsync(_cancelGetDetails.Token); IDriveItemThumbnailsCollectionPage thumbnails = await taskThumbnails; if (!taskThumbnails.IsCanceled) { ThumbnailSet thumbnailsSet = thumbnails.FirstOrDefault(); if (thumbnailsSet != null) { Thumbnail thumbnail = thumbnailsSet.Large; if (thumbnail.Url.Contains("inputFormat=svg")) { SvgImageSource source = new SvgImageSource(); using (Stream inputStream = await graphClient.Drives[_driveId].Items[driveItem.Id].Content.Request().GetAsync()) { SvgImageSourceLoadStatus status = await source.SetSourceAsync(inputStream.AsRandomAccessStream()); if (status == SvgImageSourceLoadStatus.Success) { ThumbnailImageSource = source; } } } else { ThumbnailImageSource = new BitmapImage(new Uri(thumbnail.Url)); } } } IsDetailPaneVisible = true; ShowDetailsPane(); } } } catch (Exception exception) { MessageDialog messageDialog = new MessageDialog(exception.Message); await messageDialog.ShowAsync(); } }
private async void LoadClicked(object sender, RoutedEventArgs e) { img.Source = null; await Task.Yield(); log.Text = ""; isLoaded.IsChecked = false; isError.IsChecked = false; imgIsLoaded.IsChecked = false; imgIsError.IsChecked = false; var uri = new Uri(url.Text); ImageSource source = null; await Task.Yield(); var stream = (streamMode.IsChecked ?? false) ? await GetStream() : null; var mode = (sender as FrameworkElement)?.Tag as string; Log($"Loading {url.Text} using {mode}..."); switch (mode) { case "BitmapImage": { BitmapImage bitmapSource; if (stream == null) { bitmapSource = new BitmapImage(uri); } else { bitmapSource = new BitmapImage(); await bitmapSource.SetSourceAsync(stream); } bitmapSource.DownloadProgress += (snd, evt) => Log($"Downloadind... {evt.Progress}%"); bitmapSource.ImageFailed += (snd, evt) => { isError.IsChecked = true; Log($"ERROR: {evt.ErrorMessage}"); }; bitmapSource.ImageOpened += (snd, evt) => { isLoaded.IsChecked = true; Log($"LOADED from {evt.OriginalSource}"); }; source = bitmapSource; break; } case "SvgImageSource": { SvgImageSource svgSource; if (stream == null) { svgSource = new SvgImageSource(uri); } else { svgSource = new SvgImageSource(); await svgSource.SetSourceAsync(stream); } svgSource.OpenFailed += (snd, evt) => { isError.IsChecked = true; Log($"ERROR: {evt.Status}"); }; svgSource.Opened += (snd, evt) => { isLoaded.IsChecked = true; Log("LOADED"); }; source = svgSource; break; } case "SvgImageSource2": { var border = img.Parent as FrameworkElement; SvgImageSource svgSource; if (stream == null) { svgSource = new SvgImageSource(uri); } else { svgSource = new SvgImageSource(); await svgSource.SetSourceAsync(stream); } source = svgSource; Log($"RasterizePixelWidth/Height: {border.ActualWidth}x{border.ActualHeight}"); svgSource.RasterizePixelWidth = border.ActualWidth; svgSource.RasterizePixelHeight = border.ActualHeight; svgSource.OpenFailed += (snd, evt) => { isError.IsChecked = true; Log($"ERROR: {evt.Status}"); }; svgSource.Opened += (snd, evt) => { isLoaded.IsChecked = true; Log("LOADED"); }; break; } } img.Source = source; void Log(string msg) { log.Text += msg + "\n"; } }
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(); }