public static void RetrieveText(string uriToLoad, Action <string> updateResult) { var longHash = uriToLoad.GetLongHashCode(); var textFileName = ApplicationController.CacheablePath("Text", longHash.ToString() + ".txt"); string fileText = null; if (File.Exists(textFileName)) { try { fileText = File.ReadAllText(textFileName); updateResult?.Invoke(fileText); } catch { } } Task.Run(async() => { var client = new HttpClient(); var text = await client.GetStringAsync(uriToLoad); if (text != fileText) { File.WriteAllText(textFileName, text); updateResult?.Invoke(text); } }); }
// Download an image from the web into the specified ImageBuffer public static void RetrieveImageAsync(ImageBuffer imageToLoadInto, string uriToLoad, bool scaleToImageX) { var longHash = uriToLoad.GetLongHashCode(); if (scaleToImageX) { longHash = imageToLoadInto.Width.GetLongHashCode(longHash); } var imageFileName = ApplicationController.CacheablePath("Images", longHash.ToString() + ".png"); if (File.Exists(imageFileName)) { try { LoadImageInto(imageToLoadInto, scaleToImageX, null, new StreamReader(imageFileName).BaseStream); return; } catch { } } WebClient client = new WebClient(); client.DownloadDataCompleted += (sender, e) => { try // if we get a bad result we can get a target invocation exception. In that case just don't show anything { Stream stream = new MemoryStream(e.Result); LoadImageInto(imageToLoadInto, scaleToImageX, new BlenderPreMultBGRA(), stream); if (imageToLoadInto.Width > 0 && imageToLoadInto.Height > 0 && !savedImages.Contains(imageFileName)) { savedImages.Add(imageFileName); ImageIO.SaveImageData(imageFileName, imageToLoadInto); } } catch { } }; try { client.DownloadDataAsync(new Uri(uriToLoad)); } catch { } }
public static void RetrieveText(string uriToLoad, Action <string> updateResult) { var longHash = uriToLoad.GetLongHashCode(); var appDataFileName = ApplicationController.CacheablePath("TextWebCache", longHash.ToString() + ".txt"); string fileText = null; // first try the cache in the users applications folder if (File.Exists(appDataFileName)) { try { fileText = File.ReadAllText(appDataFileName); updateResult?.Invoke(fileText); } catch { } } else // We could not find it in the application cache. Check if it is in static data. { var staticDataPath = Path.Combine("TextWebCache", longHash.ToString() + ".txt"); if (AggContext.StaticData.FileExists(staticDataPath)) { try { fileText = AggContext.StaticData.ReadAllText(staticDataPath); updateResult?.Invoke(fileText); } catch { } } } // whether we find it or not check the web for the latest version Task.Run(async() => { var client = new HttpClient(); var text = await client.GetStringAsync(uriToLoad); if (!string.IsNullOrEmpty(text) && text != fileText) { File.WriteAllText(appDataFileName, text); updateResult?.Invoke(text); } }); }
public string CachePath(ILibraryItem libraryItem, int width, int height) { return(ApplicationController.CacheablePath( Path.Combine("Thumbnails", "Library"), CacheFilename(libraryItem, width, height))); }
public string CachePath(ILibraryItem libraryItem) { return(ApplicationController.CacheablePath( Path.Combine("Thumbnails", "Library"), $"{libraryItem.ID}.png")); }
public string CachePath(string cacheId, int width, int height) { return(ApplicationController.CacheablePath( Path.Combine("Thumbnails", "Content"), $"{cacheId}-{width}x{height}.png")); }
/// <summary> /// Download an image from the web into the specified ImageSequence /// </summary> /// <param name="uri"></param> public static void RetrieveImageSquenceAsync(ImageSequence imageSequenceToLoadInto, string uriToLoad, Action doneLoading = null) { var asyncImageSequence = new ImageSequence(); var longHash = uriToLoad.GetLongHashCode(); var pngFileName = ApplicationController.CacheablePath("Images", longHash.ToString() + ".png"); var gifFileName = ApplicationController.CacheablePath("Images", longHash.ToString() + ".gif"); if (File.Exists(pngFileName)) { try { Task.Run(() => { AggContext.StaticData.LoadImageSequenceData(new StreamReader(pngFileName).BaseStream, asyncImageSequence); UiThread.RunOnIdle(() => { imageSequenceToLoadInto.Copy(asyncImageSequence); imageSequenceToLoadInto.Invalidate(); doneLoading?.Invoke(); }); }); return; } catch { } } else if (File.Exists(gifFileName)) { Task.Run(() => { try { AggContext.StaticData.LoadImageSequenceData(new StreamReader(gifFileName).BaseStream, asyncImageSequence); if (asyncImageSequence.NumFrames > 0) { UiThread.RunOnIdle(() => { imageSequenceToLoadInto.Copy(asyncImageSequence); imageSequenceToLoadInto.Invalidate(); doneLoading?.Invoke(); }); } else { DownloadImageAsync(imageSequenceToLoadInto, uriToLoad, doneLoading, asyncImageSequence, pngFileName, gifFileName); } } catch { DownloadImageAsync(imageSequenceToLoadInto, uriToLoad, doneLoading, asyncImageSequence, pngFileName, gifFileName); } }); return; } DownloadImageAsync(imageSequenceToLoadInto, uriToLoad, doneLoading, asyncImageSequence, pngFileName, gifFileName); }
private void AddGeneralPannel(GuiWidget settingsColumn) { var generalPanel = new FlowLayoutWidget(FlowDirection.TopToBottom) { HAnchor = HAnchor.Stretch, VAnchor = VAnchor.Fit, }; var configureIcon = StaticData.Instance.LoadIcon("fa-cog_16.png", 16, 16).SetToColor(theme.TextColor); var generalSection = new SectionWidget("General".Localize(), generalPanel, theme, expandingContent: false) { Name = "General Section", HAnchor = HAnchor.Stretch, VAnchor = VAnchor.Fit, }; settingsColumn.AddChild(generalSection); theme.ApplyBoxStyle(generalSection); #if __ANDROID__ // Camera Monitoring bool hasCamera = true || ApplicationSettings.Instance.get(ApplicationSettingsKey.HardwareHasCamera) == "true"; var previewButton = new IconButton(configureIcon, theme) { ToolTipText = "Preview".Localize() }; previewButton.Click += (s, e) => { AppContext.Platform.OpenCameraPreview(); }; var printer = ApplicationController.Instance.ActivePrinters.FirstOrDefault(); // TODO: Sort out how handle this better on Android and in a multi-printer setup if (printer != null) { this.AddSettingsRow( new SettingsItem( "Camera Monitoring".Localize(), theme, new SettingsItem.ToggleSwitchConfig() { Checked = printer.Settings.GetValue <bool>(SettingsKey.publish_bed_image), ToggleAction = (itemChecked) => { printer.Settings.SetValue(SettingsKey.publish_bed_image, itemChecked ? "1" : "0"); } }, previewButton, StaticData.Instance.LoadIcon("camera-24x24.png", 24, 24)) { Enabled = printer.Settings.PrinterSelected }, generalPanel ); } #endif // Print Notifications var configureNotificationsButton = new IconButton(configureIcon, theme) { Name = "Configure Notification Settings Button", ToolTipText = "Configure Notifications".Localize(), Margin = new BorderDouble(left: 6), VAnchor = VAnchor.Center }; configureNotificationsButton.Click += (s, e) => { if (ApplicationController.ChangeToPrintNotification != null) { UiThread.RunOnIdle(() => { ApplicationController.ChangeToPrintNotification(this.DialogWindow); }); } }; this.AddSettingsRow( new SettingsItem( "Notifications".Localize(), theme, new SettingsItem.ToggleSwitchConfig() { Checked = UserSettings.Instance.get(UserSettingsKey.PrintNotificationsEnabled) == "true", ToggleAction = (itemChecked) => { UserSettings.Instance.set(UserSettingsKey.PrintNotificationsEnabled, itemChecked ? "true" : "false"); } }, configureNotificationsButton, StaticData.Instance.LoadIcon("notify-24x24.png", 16, 16).SetToColor(theme.TextColor)), generalPanel); // LanguageControl var languageSelector = new LanguageSelector(theme); languageSelector.SelectionChanged += (s, e) => { UiThread.RunOnIdle(() => { string languageCode = languageSelector.SelectedValue; if (languageCode != UserSettings.Instance.get(UserSettingsKey.Language)) { UserSettings.Instance.set(UserSettingsKey.Language, languageCode); if (languageCode == "L10N") { #if DEBUG AppContext.Platform.GenerateLocalizationValidationFile(); #endif } ApplicationController.Instance.ResetTranslationMap(); ApplicationController.Instance.ReloadAll().ConfigureAwait(false); } }); }; this.AddSettingsRow(new SettingsItem("Language".Localize(), languageSelector, theme), generalPanel); #if !__ANDROID__ // ThumbnailRendering var thumbnailsModeDropList = new MHDropDownList("", theme, maxHeight: 200 * GuiWidget.DeviceScale); thumbnailsModeDropList.AddItem("Flat".Localize(), "orthographic"); thumbnailsModeDropList.AddItem("3D".Localize(), "raytraced"); thumbnailsModeDropList.SelectedValue = UserSettings.Instance.ThumbnailRenderingMode; thumbnailsModeDropList.SelectionChanged += (s, e) => { string thumbnailRenderingMode = thumbnailsModeDropList.SelectedValue; if (thumbnailRenderingMode != UserSettings.Instance.ThumbnailRenderingMode) { UserSettings.Instance.ThumbnailRenderingMode = thumbnailRenderingMode; UiThread.RunOnIdle(() => { // Ask if the user they would like to rebuild their thumbnails StyledMessageBox.ShowMessageBox( (bool rebuildThumbnails) => { if (rebuildThumbnails) { string[] thumbnails = new string[] { ApplicationController.CacheablePath( Path.Combine("Thumbnails", "Content"), ""), ApplicationController.CacheablePath( Path.Combine("Thumbnails", "Library"), "") }; foreach (var directoryToRemove in thumbnails) { try { if (Directory.Exists(directoryToRemove)) { Directory.Delete(directoryToRemove, true); } } catch (Exception) { GuiWidget.BreakInDebugger(); } Directory.CreateDirectory(directoryToRemove); } ApplicationController.Instance.Library.NotifyContainerChanged(); } }, "You are switching to a different thumbnail rendering mode. If you want, your current thumbnails can be removed and recreated in the new style. You can switch back and forth at any time. There will be some processing overhead while the new thumbnails are created.\n\nDo you want to rebuild your existing thumbnails now?".Localize(), "Rebuild Thumbnails Now".Localize(), StyledMessageBox.MessageType.YES_NO, "Rebuild".Localize()); }); } }; this.AddSettingsRow( new SettingsItem( "Thumbnails".Localize(), thumbnailsModeDropList, theme), generalPanel); #endif // TextSize if (!double.TryParse(UserSettings.Instance.get(UserSettingsKey.ApplicationTextSize), out double currentTextSize)) { currentTextSize = 1.0; } double sliderThumbWidth = 10 * GuiWidget.DeviceScale; double sliderWidth = 100 * GuiWidget.DeviceScale; var textSizeSlider = new SolidSlider(default(Vector2), sliderThumbWidth, theme, .7, 2.5) { Name = "Text Size Slider", Margin = new BorderDouble(5, 0), Value = currentTextSize, HAnchor = HAnchor.Stretch, VAnchor = VAnchor.Center, TotalWidthInPixels = sliderWidth, }; theme.ApplySliderStyle(textSizeSlider); var optionalContainer = new FlowLayoutWidget() { VAnchor = VAnchor.Center | VAnchor.Fit, HAnchor = HAnchor.Fit }; TextWidget sectionLabel = null; var textSizeApplyButton = new TextButton("Apply".Localize(), theme) { VAnchor = VAnchor.Center, BackgroundColor = theme.SlightShade, Visible = false, Margin = new BorderDouble(right: 6) }; textSizeApplyButton.Click += (s, e) => UiThread.RunOnIdle(() => { GuiWidget.DeviceScale = textSizeSlider.Value; ApplicationController.Instance.ReloadAll().ConfigureAwait(false); }); optionalContainer.AddChild(textSizeApplyButton); textSizeSlider.ValueChanged += (s, e) => { double textSizeNew = textSizeSlider.Value; UserSettings.Instance.set(UserSettingsKey.ApplicationTextSize, textSizeNew.ToString("0.0")); sectionLabel.Text = "Text Size".Localize() + $" : {textSizeNew:0.0}"; textSizeApplyButton.Visible = textSizeNew != currentTextSize; }; var textSizeRow = new SettingsItem( "Text Size".Localize() + $" : {currentTextSize:0.0}", textSizeSlider, theme, optionalContainer); sectionLabel = textSizeRow.Children <TextWidget>().FirstOrDefault(); this.AddSettingsRow(textSizeRow, generalPanel); var themeSection = CreateThemePanel(theme); settingsColumn.AddChild(themeSection); theme.ApplyBoxStyle(themeSection); }
public ApplicationSettingsPage() { this.AlwaysOnTopOfMain = true; this.WindowTitle = this.HeaderText = "MatterControl " + "Settings".Localize(); this.WindowSize = new Vector2(700 * GuiWidget.DeviceScale, 600 * GuiWidget.DeviceScale); contentRow.Padding = contentRow.Padding.Clone(top: 0); var generalPanel = new FlowLayoutWidget(FlowDirection.TopToBottom) { HAnchor = HAnchor.Stretch, VAnchor = VAnchor.Fit, }; contentRow.AddChild(generalPanel); var configureIcon = AggContext.StaticData.LoadIcon("fa-cog_16.png", 16, 16, theme.InvertIcons); #if __ANDROID__ // Camera Monitoring bool hasCamera = true || ApplicationSettings.Instance.get(ApplicationSettingsKey.HardwareHasCamera) == "true"; var previewButton = new IconButton(configureIcon, theme) { ToolTipText = "Configure Camera View".Localize() }; previewButton.Click += (s, e) => { AppContext.Platform.OpenCameraPreview(); }; var printer = ApplicationController.Instance.ActivePrinter; this.AddSettingsRow( new SettingsItem( "Camera Monitoring".Localize(), theme, new SettingsItem.ToggleSwitchConfig() { Checked = printer.Settings.GetValue <bool>(SettingsKey.publish_bed_image), ToggleAction = (itemChecked) => { printer.Settings.SetValue(SettingsKey.publish_bed_image, itemChecked ? "1" : "0"); } }, previewButton, AggContext.StaticData.LoadIcon("camera-24x24.png", 24, 24)), generalPanel ); #endif // Print Notifications var configureNotificationsButton = new IconButton(configureIcon, theme) { Name = "Configure Notification Settings Button", ToolTipText = "Configure Notifications".Localize(), Margin = new BorderDouble(left: 6), VAnchor = VAnchor.Center }; configureNotificationsButton.Click += (s, e) => { if (ApplicationController.ChangeToPrintNotification != null) { UiThread.RunOnIdle(() => { ApplicationController.ChangeToPrintNotification(this.DialogWindow); }); } }; this.AddSettingsRow( new SettingsItem( "Notifications".Localize(), theme, new SettingsItem.ToggleSwitchConfig() { Checked = UserSettings.Instance.get(UserSettingsKey.PrintNotificationsEnabled) == "true", ToggleAction = (itemChecked) => { UserSettings.Instance.set(UserSettingsKey.PrintNotificationsEnabled, itemChecked ? "true" : "false"); } }, configureNotificationsButton, AggContext.StaticData.LoadIcon("notify-24x24.png", 16, 16, theme.InvertIcons)), generalPanel); // LanguageControl var languageSelector = new LanguageSelector(theme); languageSelector.SelectionChanged += (s, e) => { UiThread.RunOnIdle(() => { string languageCode = languageSelector.SelectedValue; if (languageCode != UserSettings.Instance.get(UserSettingsKey.Language)) { UserSettings.Instance.set(UserSettingsKey.Language, languageCode); if (languageCode == "L10N") { #if DEBUG AppContext.Platform.GenerateLocalizationValidationFile(); #endif } ApplicationController.Instance.ResetTranslationMap(); ApplicationController.Instance.ReloadAll(); } }); }; this.AddSettingsRow(new SettingsItem("Language".Localize(), languageSelector, theme), generalPanel); #if !__ANDROID__ // ThumbnailRendering var thumbnailsModeDropList = new DropDownList("", theme.Colors.PrimaryTextColor, maxHeight: 200, pointSize: theme.DefaultFontSize) { BorderColor = theme.GetBorderColor(75) }; thumbnailsModeDropList.AddItem("Flat".Localize(), "orthographic"); thumbnailsModeDropList.AddItem("3D".Localize(), "raytraced"); thumbnailsModeDropList.SelectedValue = UserSettings.Instance.ThumbnailRenderingMode; thumbnailsModeDropList.SelectionChanged += (s, e) => { string thumbnailRenderingMode = thumbnailsModeDropList.SelectedValue; if (thumbnailRenderingMode != UserSettings.Instance.ThumbnailRenderingMode) { UserSettings.Instance.ThumbnailRenderingMode = thumbnailRenderingMode; UiThread.RunOnIdle(() => { // Ask if the user they would like to rebuild their thumbnails StyledMessageBox.ShowMessageBox( (bool rebuildThumbnails) => { if (rebuildThumbnails) { string[] thumbnails = new string[] { ApplicationController.CacheablePath( Path.Combine("Thumbnails", "Content"), ""), ApplicationController.CacheablePath( Path.Combine("Thumbnails", "Library"), "") }; foreach (var directoryToRemove in thumbnails) { try { if (Directory.Exists(directoryToRemove)) { Directory.Delete(directoryToRemove, true); } } catch (Exception) { GuiWidget.BreakInDebugger(); } Directory.CreateDirectory(directoryToRemove); } ApplicationController.Instance.Library.NotifyContainerChanged(); } }, "You are switching to a different thumbnail rendering mode. If you want, your current thumbnails can be removed and recreated in the new style. You can switch back and forth at any time. There will be some processing overhead while the new thumbnails are created.\n\nDo you want to rebuild your existing thumbnails now?".Localize(), "Rebuild Thumbnails Now".Localize(), StyledMessageBox.MessageType.YES_NO, "Rebuild".Localize()); }); } }; this.AddSettingsRow( new SettingsItem( "Thumbnails".Localize(), thumbnailsModeDropList, theme), generalPanel); #endif // TextSize if (!double.TryParse(UserSettings.Instance.get(UserSettingsKey.ApplicationTextSize), out double currentTextSize)) { currentTextSize = 1.0; } double sliderThumbWidth = 10 * GuiWidget.DeviceScale; double sliderWidth = 100 * GuiWidget.DeviceScale; var textSizeSlider = new SolidSlider(new Vector2(), sliderThumbWidth, .7, 1.4) { Name = "Text Size Slider", Margin = new BorderDouble(5, 0), Value = currentTextSize, HAnchor = HAnchor.Stretch, VAnchor = VAnchor.Center, TotalWidthInPixels = sliderWidth, }; var optionalContainer = new FlowLayoutWidget() { VAnchor = VAnchor.Center | VAnchor.Fit, HAnchor = HAnchor.Fit }; TextWidget sectionLabel = null; var textSizeApplyButton = new TextButton("Apply".Localize(), theme) { VAnchor = VAnchor.Center, BackgroundColor = theme.SlightShade, Visible = false, Margin = new BorderDouble(right: 6) }; textSizeApplyButton.Click += (s, e) => { GuiWidget.DeviceScale = textSizeSlider.Value; ApplicationController.Instance.ReloadAll(); }; optionalContainer.AddChild(textSizeApplyButton); textSizeSlider.ValueChanged += (s, e) => { double textSizeNew = textSizeSlider.Value; UserSettings.Instance.set(UserSettingsKey.ApplicationTextSize, textSizeNew.ToString("0.0")); sectionLabel.Text = "Text Size".Localize() + $" : {textSizeNew:0.0}"; textSizeApplyButton.Visible = textSizeNew != currentTextSize; }; var section = new SettingsItem( "Text Size".Localize() + $" : {currentTextSize:0.0}", textSizeSlider, theme, optionalContainer); sectionLabel = section.Children <TextWidget>().FirstOrDefault(); this.AddSettingsRow(section, generalPanel); themeColorPanel = new ThemeColorPanel(theme) { HAnchor = HAnchor.Stretch }; var droplist = new DropDownList("Custom", theme.Colors.PrimaryTextColor, maxHeight: 200, pointSize: theme.DefaultFontSize) { BorderColor = theme.GetBorderColor(75), Margin = new BorderDouble(0, 0, 10, 0) }; int i = 0; foreach (var item in AppContext.ThemeProviders) { var newItem = droplist.AddItem(item.Key); if (item.Value == themeColorPanel.ThemeProvider) { droplist.SelectedIndex = i; } i++; } droplist.SelectionChanged += (s, e) => { if (AppContext.ThemeProviders.TryGetValue(droplist.SelectedValue, out IColorTheme provider)) { themeColorPanel.ThemeProvider = provider; UserSettings.Instance.set(UserSettingsKey.ThemeName, droplist.SelectedValue); } }; var themeRow = new SettingsItem("Theme".Localize(), droplist, theme); generalPanel.AddChild(themeRow); generalPanel.AddChild(themeColorPanel); themeColorPanel.Border = themeRow.Border; themeColorPanel.BorderColor = themeRow.BorderColor; themeRow.Border = 0; var advancedPanel = new FlowLayoutWidget(FlowDirection.TopToBottom) { Margin = new BorderDouble(2, 0) }; var sectionWidget = new SectionWidget("Advanced".Localize(), advancedPanel, theme, serializationKey: "ApplicationSettings-Advanced", expanded: false) { Name = "Advanced Section", HAnchor = HAnchor.Stretch, VAnchor = VAnchor.Fit, Margin = 0 }; contentRow.AddChild(sectionWidget); theme.ApplyBoxStyle(sectionWidget); sectionWidget.Margin = new BorderDouble(0, 10); // Touch Screen Mode this.AddSettingsRow( new SettingsItem( "Touch Screen Mode".Localize(), theme, new SettingsItem.ToggleSwitchConfig() { Checked = UserSettings.Instance.get(UserSettingsKey.ApplicationDisplayMode) == "touchscreen", ToggleAction = (itemChecked) => { string displayMode = itemChecked ? "touchscreen" : "responsive"; if (displayMode != UserSettings.Instance.get(UserSettingsKey.ApplicationDisplayMode)) { UserSettings.Instance.set(UserSettingsKey.ApplicationDisplayMode, displayMode); ApplicationController.Instance.ReloadAll(); } } }), advancedPanel); var openCacheButton = new IconButton(AggContext.StaticData.LoadIcon("fa-link_16.png", 16, 16, theme.InvertIcons), theme) { ToolTipText = "Open Folder".Localize(), }; openCacheButton.Click += (s, e) => UiThread.RunOnIdle(() => { Process.Start(ApplicationDataStorage.ApplicationUserDataPath); }); this.AddSettingsRow( new SettingsItem( "Application Storage".Localize(), openCacheButton, theme), advancedPanel); var clearCacheButton = new HoverIconButton(AggContext.StaticData.LoadIcon("remove.png", 16, 16, theme.InvertIcons), theme) { ToolTipText = "Clear Cache".Localize(), }; clearCacheButton.Click += (s, e) => UiThread.RunOnIdle(() => { CacheDirectory.DeleteCacheData(); }); this.AddSettingsRow( new SettingsItem( "Application Cache".Localize(), clearCacheButton, theme), advancedPanel); advancedPanel.Children <SettingsItem>().First().Border = new BorderDouble(0, 1); }
private static void RetrieveText(string uriToLoad, string cacheFolder, Action <string> updateResult, Action <HttpRequestMessage> addHeaders = null) { var longHash = uriToLoad.GetLongHashCode(); var appDataFileName = ApplicationController.CacheablePath(cacheFolder, longHash.ToString() + ".txt"); string fileText = null; // first try the cache in the users applications folder if (File.Exists(appDataFileName)) { try { lock (locker) { fileText = File.ReadAllText(appDataFileName); } updateResult?.Invoke(fileText); } catch { } } else // We could not find it in the application cache. Check if it is in static data. { var staticDataPath = Path.Combine(cacheFolder, longHash.ToString() + ".txt"); if (StaticData.Instance.FileExists(staticDataPath)) { try { lock (locker) { fileText = StaticData.Instance.ReadAllText(staticDataPath); } updateResult?.Invoke(fileText); } catch { } } } // whether we find it or not check the web for the latest version Task.Run(async() => { var requestMessage = new HttpRequestMessage(HttpMethod.Get, uriToLoad); addHeaders?.Invoke(requestMessage); using (var client = new HttpClient()) { using (HttpResponseMessage response = await client.SendAsync(requestMessage)) { var text = await response.Content.ReadAsStringAsync(); if (!string.IsNullOrEmpty(text) && text != fileText) { File.WriteAllText(appDataFileName, text); updateResult?.Invoke(text); } } } }); }
/// <summary> /// Download an image from the web into the specified ImageSequence /// </summary> /// <param name="uri"></param> public static void RetrieveImageSquenceAsync(ImageSequence imageSequenceToLoadInto, string uriToLoad) { var asyncImageSequence = new ImageSequence(); var longHash = uriToLoad.GetLongHashCode(); var pngFileName = ApplicationController.CacheablePath("Images", longHash.ToString() + ".png"); var gifFileName = ApplicationController.CacheablePath("Images", longHash.ToString() + ".gif"); if (File.Exists(pngFileName)) { try { Task.Run(() => { AggContext.StaticData.LoadImageSequenceData(new StreamReader(pngFileName).BaseStream, asyncImageSequence); UiThread.RunOnIdle(() => { imageSequenceToLoadInto.Copy(asyncImageSequence); imageSequenceToLoadInto.Invalidate(); }); }); return; } catch { } } else if (File.Exists(gifFileName)) { try { Task.Run(() => { AggContext.StaticData.LoadImageSequenceData(new StreamReader(gifFileName).BaseStream, asyncImageSequence); UiThread.RunOnIdle(() => { imageSequenceToLoadInto.Copy(asyncImageSequence); imageSequenceToLoadInto.Invalidate(); }); }); return; } catch { } } WebClient client = new WebClient(); client.DownloadDataCompleted += (object sender, DownloadDataCompletedEventArgs e) => { try // if we get a bad result we can get a target invocation exception. In that case just don't show anything { Task.Run(() => { // scale the loaded image to the size of the target image byte[] raw = e.Result; Stream stream = new MemoryStream(raw); AggContext.StaticData.LoadImageSequenceData(stream, asyncImageSequence); if (asyncImageSequence.Frames.Count == 1) { // save the as png AggContext.ImageIO.SaveImageData(pngFileName, asyncImageSequence.Frames[0]); } else // save original stream as gif { using (var writter = new FileStream(gifFileName, FileMode.Create)) { stream.Position = 0; stream.CopyTo(writter); } } UiThread.RunOnIdle(() => { imageSequenceToLoadInto.Copy(asyncImageSequence); imageSequenceToLoadInto.Invalidate(); }); }); } catch { } }; try { client.DownloadDataAsync(new Uri(uriToLoad)); } catch { } }
public string CachePath(string cacheId) { return(ApplicationController.CacheablePath( Path.Combine("Thumbnails", "Content"), $"{cacheId}.png")); }
public string CachePath(ILibraryItem libraryItem, int width, int height) { return(ApplicationController.CacheablePath( Path.Combine("Thumbnails", "Library"), $"{libraryItem.ID}-{width}x{height}.png")); }