public void Dispose() { if (!disposedValue) { Disconnect(); LogoStream?.Dispose(); LogoStream = null; disposedValue = true; } GC.SuppressFinalize(this); }
private async void UWPPickerTip_ActionButtonClick(Microsoft.UI.Xaml.Controls.TeachingTip sender, object args) { try { if (PackageListView.SelectedItem is InstalledApplication Package) { sender.IsOpen = false; PickAppFlyout.Hide(); DisplayName.Text = Package.AppName; Protocol.Text = Package.AppFamilyName; Icon.Source = Package.Logo; StorageFile FileThumbnail = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("FileThumbnail.png", CreationCollisionOption.ReplaceExisting); if (Package.CreateStreamFromLogoData() is Stream LogoStream) { try { BitmapDecoder Decoder = await BitmapDecoder.CreateAsync(LogoStream.AsRandomAccessStream()); using (SoftwareBitmap SBitmap = await Decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied)) using (SoftwareBitmap ResizeBitmap = ComputerVisionProvider.ResizeToActual(SBitmap)) using (InMemoryRandomAccessStream ResizeBitmapStream = new InMemoryRandomAccessStream()) { BitmapEncoder Encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ResizeBitmapStream); Encoder.SetSoftwareBitmap(ResizeBitmap); await Encoder.FlushAsync(); BitmapImage Source = new BitmapImage(); Icon.Source = Source; await Source.SetSourceAsync(ResizeBitmapStream); ResizeBitmapStream.Seek(0); using (Stream FileStream = await FileThumbnail.OpenStreamForWriteAsync()) { await ResizeBitmapStream.AsStreamForRead().CopyToAsync(FileStream); } } } finally { LogoStream.Dispose(); } } else { Uri PageUri = AppThemeController.Current.Theme == ElementTheme.Dark ? new Uri("ms-appx:///Assets/Page_Solid_White.png") : new Uri("ms-appx:///Assets/Page_Solid_Black.png"); StorageFile PageFile = await StorageFile.GetFileFromApplicationUriAsync(PageUri); using (IRandomAccessStream PageStream = await PageFile.OpenAsync(FileAccessMode.Read)) { BitmapImage Image = new BitmapImage(); Icon.Source = Image; await Image.SetSourceAsync(PageStream); PageStream.Seek(0); using (Stream TransformStream = PageStream.AsStreamForRead()) using (Stream FileStream = await FileThumbnail.OpenStreamForWriteAsync()) { await TransformStream.CopyToAsync(FileStream); } } } ImageFile = FileThumbnail; } } catch (Exception ex) { LogTracer.Log(ex); FailureTips.IsOpen = true; } }
private async void PickWebLogo(object sender, RoutedEventArgs e) { try { FileOpenPicker Picker = new FileOpenPicker { SuggestedStartLocation = PickerLocationId.ComputerFolder, ViewMode = PickerViewMode.List }; Picker.FileTypeFilter.Add(".ico"); Picker.FileTypeFilter.Add(".png"); Picker.FileTypeFilter.Add(".jpg"); Picker.FileTypeFilter.Add(".bmp"); if (await Picker.PickSingleFileAsync() is StorageFile ExecuteFile) { StorageFile FileThumbnail = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("FileThumbnail.png", CreationCollisionOption.ReplaceExisting); if (await ExecuteFile.OpenReadAsync() is IRandomAccessStream LogoStream) { try { BitmapImage Image = new BitmapImage(); Icon.Source = Image; await Image.SetSourceAsync(LogoStream); LogoStream.Seek(0); using (Stream FileStream = await FileThumbnail.OpenStreamForWriteAsync()) { await LogoStream.AsStreamForRead().CopyToAsync(FileStream); } } finally { LogoStream.Dispose(); } } else { Uri PageUri = AppThemeController.Current.Theme == ElementTheme.Dark ? new Uri("ms-appx:///Assets/Page_Solid_White.png") : new Uri("ms-appx:///Assets/Page_Solid_Black.png"); StorageFile PageFile = await StorageFile.GetFileFromApplicationUriAsync(PageUri); using (IRandomAccessStream PageStream = await PageFile.OpenAsync(FileAccessMode.Read)) { BitmapImage Image = new BitmapImage(); Icon.Source = Image; await Image.SetSourceAsync(PageStream); PageStream.Seek(0); using (Stream TransformStream = PageStream.AsStreamForRead()) using (Stream FileStream = await FileThumbnail.OpenStreamForWriteAsync()) { await TransformStream.CopyToAsync(FileStream); } } } ImageFile = FileThumbnail; } } catch (Exception ex) { LogTracer.Log(ex); FailureTips.IsOpen = true; } }
private async void GetImageAutomatic_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrWhiteSpace(Protocol.Text)) { EmptyTip.Target = Protocol; EmptyTip.IsOpen = true; return; } switch (Type) { case QuickStartType.Application: { if (Uri.TryCreate(Protocol.Text, UriKind.Absolute, out Uri Result)) { if (Result.IsFile) { if (await FileSystemStorageItemBase.CheckExistAsync(Protocol.Text)) { try { StorageFile ExecuteFile = await StorageFile.GetFileFromPathAsync(Protocol.Text); IDictionary <string, object> PropertiesDictionary = await ExecuteFile.Properties.RetrievePropertiesAsync(new string[] { "System.FileDescription" }); string ExtraAppName = string.Empty; if (PropertiesDictionary.TryGetValue("System.FileDescription", out object DescriptionRaw)) { ExtraAppName = Convert.ToString(DescriptionRaw); } DisplayName.Text = string.IsNullOrEmpty(ExtraAppName) ? ExecuteFile.DisplayName : ExtraAppName; StorageFile FileThumbnail = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("FileThumbnail.png", CreationCollisionOption.ReplaceExisting); if (await ExecuteFile.GetThumbnailRawStreamAsync() is IRandomAccessStream ThumbnailStream) { BitmapDecoder Decoder = await BitmapDecoder.CreateAsync(ThumbnailStream); using (SoftwareBitmap SBitmap = await Decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied)) using (SoftwareBitmap ResizeBitmap = ComputerVisionProvider.ResizeToActual(SBitmap)) using (InMemoryRandomAccessStream ResizeBitmapStream = new InMemoryRandomAccessStream()) { BitmapEncoder Encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ResizeBitmapStream); Encoder.SetSoftwareBitmap(ResizeBitmap); await Encoder.FlushAsync(); BitmapImage Image = new BitmapImage(); Icon.Source = Image; await Image.SetSourceAsync(ResizeBitmapStream); ResizeBitmapStream.Seek(0); using (Stream TransformStream = ResizeBitmapStream.AsStreamForRead()) using (Stream FileStream = await FileThumbnail.OpenStreamForWriteAsync()) { await TransformStream.CopyToAsync(FileStream); } } } else { Uri PageUri = AppThemeController.Current.Theme == ElementTheme.Dark ? new Uri("ms-appx:///Assets/Page_Solid_White.png") : new Uri("ms-appx:///Assets/Page_Solid_Black.png"); StorageFile PageFile = await StorageFile.GetFileFromApplicationUriAsync(PageUri); using (IRandomAccessStream PageStream = await PageFile.OpenAsync(FileAccessMode.Read)) { BitmapImage Image = new BitmapImage(); Icon.Source = Image; await Image.SetSourceAsync(PageStream); PageStream.Seek(0); using (Stream TransformStream = PageStream.AsStreamForRead()) using (Stream FileStream = await FileThumbnail.OpenStreamForWriteAsync()) { await TransformStream.CopyToAsync(FileStream); } } } ImageFile = FileThumbnail; } catch { FailureTips.IsOpen = true; } } else { FailureTips.IsOpen = true; } } else { if ((await Launcher.FindUriSchemeHandlersAsync(Result.Scheme)).FirstOrDefault() is AppInfo App) { DisplayName.Text = App.DisplayInfo.DisplayName; StorageFile FileThumbnail = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("FileThumbnail.png", CreationCollisionOption.ReplaceExisting); try { using (IRandomAccessStreamWithContentType LogoStream = await App.DisplayInfo.GetLogo(new Windows.Foundation.Size(120, 120)).OpenReadAsync()) { BitmapDecoder Decoder = await BitmapDecoder.CreateAsync(LogoStream); using (SoftwareBitmap SBitmap = await Decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied)) using (SoftwareBitmap ResizeBitmap = ComputerVisionProvider.ResizeToActual(SBitmap)) using (InMemoryRandomAccessStream ResizeBitmapStream = new InMemoryRandomAccessStream()) { BitmapEncoder Encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ResizeBitmapStream); Encoder.SetSoftwareBitmap(ResizeBitmap); await Encoder.FlushAsync(); BitmapImage Source = new BitmapImage(); Icon.Source = Source; await Source.SetSourceAsync(ResizeBitmapStream); ResizeBitmapStream.Seek(0); using (Stream FileStream = await FileThumbnail.OpenStreamForWriteAsync()) { await ResizeBitmapStream.AsStreamForRead().CopyToAsync(FileStream); } } } } catch { Uri PageUri = AppThemeController.Current.Theme == ElementTheme.Dark ? new Uri("ms-appx:///Assets/Page_Solid_White.png") : new Uri("ms-appx:///Assets/Page_Solid_Black.png"); StorageFile PageFile = await StorageFile.GetFileFromApplicationUriAsync(PageUri); using (IRandomAccessStream PageStream = await PageFile.OpenAsync(FileAccessMode.Read)) { BitmapImage Image = new BitmapImage(); Icon.Source = Image; await Image.SetSourceAsync(PageStream); PageStream.Seek(0); using (Stream TransformStream = PageStream.AsStreamForRead()) using (Stream FileStream = await FileThumbnail.OpenStreamForWriteAsync()) { await TransformStream.CopyToAsync(FileStream); } } } ImageFile = FileThumbnail; } else { FormatErrorTip.IsOpen = true; } } } else { using (FullTrustProcessController.ExclusiveUsage Exclusive = await FullTrustProcessController.GetAvailableController()) { if (await Exclusive.Controller.GetInstalledApplicationAsync(Protocol.Text) is InstalledApplication Pack) { StorageFile FileThumbnail = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("FileThumbnail.png", CreationCollisionOption.ReplaceExisting); if (Pack.CreateStreamFromLogoData() is Stream LogoStream) { try { BitmapDecoder Decoder = await BitmapDecoder.CreateAsync(LogoStream.AsRandomAccessStream()); using (SoftwareBitmap SBitmap = await Decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied)) using (SoftwareBitmap ResizeBitmap = ComputerVisionProvider.ResizeToActual(SBitmap)) using (InMemoryRandomAccessStream ResizeBitmapStream = new InMemoryRandomAccessStream()) { BitmapEncoder Encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ResizeBitmapStream); Encoder.SetSoftwareBitmap(ResizeBitmap); await Encoder.FlushAsync(); BitmapImage Image = new BitmapImage(); Icon.Source = Image; await Image.SetSourceAsync(ResizeBitmapStream); ResizeBitmapStream.Seek(0); using (Stream FileStream = await FileThumbnail.OpenStreamForWriteAsync()) { await ResizeBitmapStream.AsStreamForRead().CopyToAsync(FileStream); } } } catch { Uri PageUri = AppThemeController.Current.Theme == ElementTheme.Dark ? new Uri("ms-appx:///Assets/Page_Solid_White.png") : new Uri("ms-appx:///Assets/Page_Solid_Black.png"); StorageFile PageFile = await StorageFile.GetFileFromApplicationUriAsync(PageUri); using (IRandomAccessStream PageStream = await PageFile.OpenAsync(FileAccessMode.Read)) { BitmapImage Image = new BitmapImage(); Icon.Source = Image; await Image.SetSourceAsync(PageStream); PageStream.Seek(0); using (Stream TransformStream = PageStream.AsStreamForRead()) using (Stream FileStream = await FileThumbnail.OpenStreamForWriteAsync()) { await TransformStream.CopyToAsync(FileStream); } } } finally { LogoStream.Dispose(); } } else { Uri PageUri = AppThemeController.Current.Theme == ElementTheme.Dark ? new Uri("ms-appx:///Assets/Page_Solid_White.png") : new Uri("ms-appx:///Assets/Page_Solid_Black.png"); StorageFile PageFile = await StorageFile.GetFileFromApplicationUriAsync(PageUri); using (IRandomAccessStream PageStream = await PageFile.OpenAsync(FileAccessMode.Read)) { BitmapImage Image = new BitmapImage(); Icon.Source = Image; await Image.SetSourceAsync(PageStream); PageStream.Seek(0); using (Stream TransformStream = PageStream.AsStreamForRead()) using (Stream FileStream = await FileThumbnail.OpenStreamForWriteAsync()) { await TransformStream.CopyToAsync(FileStream); } } } ImageFile = FileThumbnail; } else { FormatErrorTip.IsOpen = true; } } } break; } case QuickStartType.WebSite: { try { if (Uri.TryCreate(Protocol.Text, UriKind.Absolute, out Uri Result)) { Uri ImageUri = new Uri($"{Result.Scheme}://{Result.Host}/favicon.ico"); HttpWebRequest Request = WebRequest.CreateHttp(ImageUri); using (WebResponse Response = await Request.GetResponseAsync()) using (Stream WebImageStream = Response.GetResponseStream()) using (MemoryStream TemplateStream = new MemoryStream()) { await WebImageStream.CopyToAsync(TemplateStream); TemplateStream.Seek(0, SeekOrigin.Begin); if (TemplateStream.Length > 0) { BitmapImage Bitmap = new BitmapImage(); Icon.Source = Bitmap; await Bitmap.SetSourceAsync(TemplateStream.AsRandomAccessStream()); TemplateStream.Seek(0, SeekOrigin.Begin); StorageFile DownloadImage = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("DownloadFile.ico", CreationCollisionOption.ReplaceExisting); using (Stream LocalFileStream = await DownloadImage.OpenStreamForWriteAsync()) { await TemplateStream.CopyToAsync(LocalFileStream); } ImageFile = DownloadImage; } else { FailureTips.IsOpen = true; } } } else { FailureTips.IsOpen = true; } } catch { try { Uri QueryUrl1 = new Uri($"http://statics.dnspod.cn/proxy_favicon/_/favicon?domain={new Uri(Protocol.Text).Host}"); Uri QueryUrl2 = new Uri($"http://www.google.com/s2/favicons?domain={new Uri(Protocol.Text).Host}"); WebResponse Response; HttpWebRequest Request; if (Globalization.CurrentLanguage == LanguageEnum.Chinese_Simplified) { Request = WebRequest.CreateHttp(QueryUrl1); Response = await Request.GetResponseAsync(); } else { try { Request = WebRequest.CreateHttp(QueryUrl2); Response = await Request.GetResponseAsync(); } catch { Request = WebRequest.CreateHttp(QueryUrl1); Response = await Request.GetResponseAsync(); } } try { using (Stream WebImageStream = Response.GetResponseStream()) using (MemoryStream TemplateStream = new MemoryStream()) { await WebImageStream.CopyToAsync(TemplateStream); TemplateStream.Seek(0, SeekOrigin.Begin); if (TemplateStream.Length > 0) { BitmapImage Bitmap = new BitmapImage(); Icon.Source = Bitmap; await Bitmap.SetSourceAsync(TemplateStream.AsRandomAccessStream()); TemplateStream.Seek(0, SeekOrigin.Begin); StorageFile DownloadImage = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("DownloadFile.ico", CreationCollisionOption.ReplaceExisting); using (Stream LocalFileStream = await DownloadImage.OpenStreamForWriteAsync()) { await TemplateStream.CopyToAsync(LocalFileStream); } ImageFile = DownloadImage; } else { FailureTips.IsOpen = true; } } } finally { Response?.Dispose(); } } catch { FailureTips.IsOpen = true; } } break; } } }