LoadPic.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using System.Windows;
  5. using System.Windows.Media.Imaging;
  6. using System.Windows.Threading;
  7. using PicView.FileHandling;
  8. using PicView.ImageHandling;
  9. using PicView.PicGallery;
  10. using PicView.Properties;
  11. using PicView.SystemIntegration;
  12. using PicView.UILogic;
  13. using PicView.UILogic.Sizing;
  14. using XamlAnimatedGif;
  15. using static PicView.ChangeImage.ErrorHandling;
  16. using static PicView.ChangeImage.Navigation;
  17. using static PicView.FileHandling.ArchiveExtraction;
  18. using static PicView.FileHandling.FileLists;
  19. using static PicView.ImageHandling.Thumbnails;
  20. using static PicView.ChangeTitlebar.SetTitle;
  21. using static PicView.UILogic.Sizing.ScaleImage;
  22. using static PicView.UILogic.Tooltip;
  23. using static PicView.UILogic.UC;
  24. using Rotation = PicView.UILogic.TransformImage.Rotation;
  25. namespace PicView.ChangeImage
  26. {
  27. internal static class LoadPic
  28. {
  29. #region QuickLoad
  30. /// <summary>
  31. /// Quickly load image and then update values
  32. /// </summary>
  33. /// <param name="file"></param>
  34. /// <returns></returns>
  35. internal static async Task QuickLoadAsync(string file)
  36. {
  37. if (File.Exists(file) == false)
  38. {
  39. if (Settings.Default.AutoFitWindow)
  40. {
  41. await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
  42. {
  43. WindowSizing.SetWindowBehavior();
  44. });
  45. }
  46. await LoadPicFromStringAsync(file, false).ConfigureAwait(false);
  47. return;
  48. }
  49. await QuickLoadAsync(new FileInfo(file)).ConfigureAwait(false);
  50. InitialPath = file;
  51. }
  52. /// <summary>
  53. /// Quickly load image and then update values
  54. /// </summary>
  55. /// <param name="file"></param>
  56. /// <returns></returns>
  57. internal static async Task QuickLoadAsync(FileInfo fileInfo)
  58. {
  59. ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Render, () =>
  60. {
  61. SetLoadingString();
  62. });
  63. bool archive = SupportedFiles.IsSupportedArchives(fileInfo);
  64. BitmapSource? pic = null;
  65. if (archive is false)
  66. {
  67. pic = await ImageDecoder.ReturnBitmapSourceAsync(fileInfo).ConfigureAwait(false);
  68. if (pic is null)
  69. {
  70. pic = ImageFunctions.ImageErrorMessage();
  71. }
  72. else
  73. {
  74. ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Render, () =>
  75. {
  76. if (fileInfo.Extension == ".gif")
  77. {
  78. AnimationBehavior.SetSourceUri(ConfigureWindows.GetMainWindow.MainImage, new Uri(fileInfo.FullName));
  79. }
  80. else
  81. {
  82. ConfigureWindows.GetMainWindow.MainImage.Source = pic;
  83. }
  84. FitImage(pic.PixelWidth, pic.PixelHeight);
  85. });
  86. }
  87. }
  88. await RetrieveFilelistAsync(fileInfo).ConfigureAwait(false);
  89. FolderIndex = Pics.Count > 0 ? Pics.IndexOf(fileInfo.FullName) : 0;
  90. if (pic is not null)
  91. {
  92. ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Render, () =>
  93. {
  94. SetTitleString(pic.PixelWidth, pic.PixelHeight, FolderIndex, fileInfo);
  95. });
  96. }
  97. if (archive == false)
  98. {
  99. _ = Preloader.PreLoad(FolderIndex).ConfigureAwait(false);
  100. _ = Preloader.AddAsync(FolderIndex, fileInfo, pic).ConfigureAwait(false);
  101. }
  102. if (FolderIndex > 0)
  103. {
  104. _ = Taskbar.Progress((double)FolderIndex / Pics.Count).ConfigureAwait(false);
  105. }
  106. if (GalleryFunctions.IsVerticalFullscreenOpen || GalleryFunctions.IsHorizontalFullscreenOpen)
  107. {
  108. _ = GalleryLoad.Load().ConfigureAwait(false);
  109. }
  110. FreshStartup = false;
  111. // Add recent files, except when browing archive
  112. if (string.IsNullOrWhiteSpace(TempZipFile) && Pics?.Count > FolderIndex)
  113. {
  114. History.Add(Pics?[FolderIndex]);
  115. }
  116. }
  117. #endregion
  118. #region LoadPicAtValue
  119. /// <summary>
  120. /// Determine proper path from given string value
  121. /// </summary>
  122. /// <param name="path"></param>
  123. /// <returns></returns>
  124. internal static async Task LoadPicFromStringAsync(string path, bool checkExists = true, FileInfo? fileInfo = null)
  125. {
  126. ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Render, () =>
  127. {
  128. ToggleStartUpUC(true);
  129. });
  130. if (checkExists && File.Exists(path))
  131. {
  132. if (fileInfo is null)
  133. {
  134. fileInfo = new FileInfo(path);
  135. }
  136. LoadingPreview(fileInfo);
  137. await LoadPiFromFileAsync(fileInfo).ConfigureAwait(false);
  138. }
  139. else
  140. {
  141. ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Render, () =>
  142. {
  143. SetLoadingString();
  144. });
  145. string check = CheckIfLoadableString(path);
  146. switch (check)
  147. {
  148. default: await LoadPiFromFileAsync(check).ConfigureAwait(false); return;
  149. case "web": await WebFunctions.PicWeb(path).ConfigureAwait(false); return;
  150. case "base64": await LoadBase64PicAsync(path).ConfigureAwait(false); return;
  151. case "directory": await LoadPicFromFolderAsync(path).ConfigureAwait(false); return;
  152. case "": ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Render, () => { Unload(true); }); return;
  153. }
  154. }
  155. }
  156. /// <summary>
  157. /// Loads a picture from a given file path and does extra error checking
  158. /// </summary>
  159. /// <param name="path"></param>
  160. internal static async Task LoadPiFromFileAsync(string path)
  161. {
  162. var fileInfo = new FileInfo(path);
  163. await LoadPiFromFileAsync(fileInfo).ConfigureAwait(false);
  164. }
  165. /// <summary>
  166. /// Loads a picture from a given file path and does extra error checking
  167. /// </summary>
  168. /// <param name="path"></param>
  169. internal static async Task LoadPiFromFileAsync(FileInfo fileInfo)
  170. {
  171. await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Render, () =>
  172. {
  173. ToggleStartUpUC(true);
  174. });
  175. if (fileInfo.Exists == false)
  176. {
  177. await LoadPicFromStringAsync(fileInfo.FullName, false, fileInfo).ConfigureAwait(false);
  178. return;
  179. }
  180. if (Pics.Count > FolderIndex && fileInfo.DirectoryName == Path.GetDirectoryName(Pics[FolderIndex]))
  181. {
  182. if (Pics.Contains(fileInfo.FullName) == false)
  183. {
  184. await RetrieveFilelistAsync(fileInfo).ConfigureAwait(false);
  185. }
  186. await LoadPicAtIndexAsync(Pics.IndexOf(fileInfo.FullName), fileInfo).ConfigureAwait(false);
  187. return;
  188. }
  189. LoadingPreview(fileInfo);
  190. bool folderChanged = await CheckDirectoryChangeAndPicGallery(fileInfo).ConfigureAwait(false);
  191. await RetrieveFilelistAsync(fileInfo).ConfigureAwait(false);
  192. if (Pics?.Count > 0)
  193. {
  194. FolderIndex = Pics.IndexOf(fileInfo.FullName);
  195. }
  196. if (FolderIndex < 0)
  197. {
  198. FolderIndex = 0;
  199. }
  200. if (FreshStartup is false || folderChanged)
  201. {
  202. Preloader.Clear();
  203. }
  204. if (FolderIndex >= 0 && Pics?.Count > 0) // check if being extracted and need to wait for it instead
  205. {
  206. // Navigate to picture using obtained index
  207. await LoadPicAtIndexAsync(FolderIndex, fileInfo).ConfigureAwait(false);
  208. }
  209. FreshStartup = false;
  210. if (GalleryFunctions.IsVerticalFullscreenOpen || GalleryFunctions.IsHorizontalFullscreenOpen)
  211. {
  212. await GalleryLoad.Load().ConfigureAwait(false);
  213. GalleryNavigation.SetSelected(FolderIndex, true);
  214. }
  215. if (string.IsNullOrWhiteSpace(InitialPath) || folderChanged)
  216. {
  217. InitialPath = fileInfo.FullName;
  218. }
  219. }
  220. /// <summary>
  221. /// Handle logic if user wants to load from a folder
  222. /// </summary>
  223. /// <param name="folder"></param>
  224. internal static async Task LoadPicFromFolderAsync(string folder)
  225. {
  226. var fileInfo = new FileInfo(folder);
  227. if (fileInfo is null)
  228. {
  229. UnexpectedError();
  230. return;
  231. }
  232. await LoadPicFromFolderAsync(fileInfo).ConfigureAwait(false);
  233. }
  234. /// <summary>
  235. /// Handle logic if user wants to load from a folder
  236. /// </summary>
  237. /// <param name="folder"></param>
  238. internal static async Task LoadPicFromFolderAsync(FileInfo fileInfo, int index = -1)
  239. {
  240. // TODO add new function that can go to next/prev folder
  241. await ConfigureWindows.GetMainWindow.Dispatcher.InvokeAsync(() =>
  242. {
  243. SetLoadingString();
  244. ToggleStartUpUC(true);
  245. });
  246. if (CheckOutOfRange() == false)
  247. {
  248. BackupPath = Pics[FolderIndex];
  249. }
  250. bool folderChanged = await CheckDirectoryChangeAndPicGallery(fileInfo).ConfigureAwait(false);
  251. if (FreshStartup is false || folderChanged)
  252. {
  253. Preloader.Clear();
  254. }
  255. await RetrieveFilelistAsync(fileInfo).ConfigureAwait(false);
  256. if (Pics.Count < 0) // TODO make function to find first folder with pics, when not browsing recursively
  257. {
  258. await ReloadAsync(true).ConfigureAwait(false);
  259. return;
  260. }
  261. if (index >= 0)
  262. {
  263. await LoadPicAtIndexAsync(index).ConfigureAwait(false);
  264. }
  265. else
  266. {
  267. await LoadPicAtIndexAsync(0).ConfigureAwait(false);
  268. }
  269. if (GalleryFunctions.IsVerticalFullscreenOpen || GalleryFunctions.IsHorizontalFullscreenOpen)
  270. {
  271. await GalleryLoad.Load().ConfigureAwait(false);
  272. }
  273. if (folderChanged || string.IsNullOrWhiteSpace(InitialPath))
  274. {
  275. InitialPath = fileInfo.FullName;
  276. }
  277. }
  278. /// <summary>
  279. /// Loads image at specified index
  280. /// </summary>
  281. /// <param name="index">The index of file to load from Pics</param>
  282. internal static async Task LoadPicAtIndexAsync(int index, FileInfo? fileInfo = null)
  283. {
  284. if (Pics?.Count < index || Pics?.Count < 1)
  285. {
  286. // Prevent infinite loading when dropping folder and can't find file
  287. await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, async () =>
  288. {
  289. if (ConfigureWindows.GetMainWindow.TitleText.Text == (string)Application.Current.Resources["Loading"])
  290. {
  291. await ReloadAsync(true).ConfigureAwait(false);
  292. }
  293. });
  294. return;
  295. }
  296. FolderIndex = index;
  297. var preloadValue = Preloader.Get(Pics[index]);
  298. // Initate loading behavior, if needed
  299. if (preloadValue == null || preloadValue.IsLoading)
  300. {
  301. // Show a thumbnail while loading
  302. BitmapSource? thumb = null;
  303. if (GalleryFunctions.IsHorizontalFullscreenOpen == false || GalleryFunctions.IsVerticalFullscreenOpen == false)
  304. {
  305. if (fileInfo is null)
  306. {
  307. fileInfo = new FileInfo(Pics[FolderIndex]);
  308. }
  309. if (fileInfo.Exists)
  310. {
  311. thumb = GetBitmapSourceThumb(fileInfo);
  312. }
  313. else
  314. {
  315. try // Fix deleting files outside application
  316. {
  317. var x = index - 1 >= 0 ? index - 1 : 0;
  318. fileInfo = new FileInfo(Pics[x]);
  319. await RetrieveFilelistAsync(fileInfo).ConfigureAwait(false);
  320. await LoadPiFromFileAsync(fileInfo).ConfigureAwait(false);
  321. return;
  322. }
  323. catch (Exception)
  324. {
  325. UnexpectedError();
  326. return;
  327. }
  328. }
  329. }
  330. ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Render, () =>
  331. {
  332. if (GalleryFunctions.IsHorizontalFullscreenOpen || GalleryFunctions.IsVerticalFullscreenOpen)
  333. {
  334. thumb = GetThumb(index, fileInfo);
  335. GalleryNavigation.FullscreenGalleryNavigation();
  336. }
  337. if (FreshStartup)
  338. {
  339. // Set loading from translation service
  340. SetLoadingString();
  341. FreshStartup = false;
  342. }
  343. if (thumb != null)
  344. {
  345. ConfigureWindows.GetMainWindow.MainImage.Source = thumb;
  346. }
  347. // Don't allow image size to stretch the whole screen
  348. if (XWidth == 0)
  349. {
  350. ConfigureWindows.GetMainWindow.MainImage.Width = ConfigureWindows.GetMainWindow.ParentContainer.ActualWidth;
  351. ConfigureWindows.GetMainWindow.MainImage.Height = ConfigureWindows.GetMainWindow.ParentContainer.ActualHeight;
  352. }
  353. });
  354. if (preloadValue is null)
  355. {
  356. bool added = await Preloader.AddAsync(index, fileInfo).ConfigureAwait(false);
  357. if (added)
  358. {
  359. preloadValue = Preloader.Get(Pics[index]);
  360. }
  361. if (preloadValue is null)
  362. {
  363. Preloader.Remove(index);
  364. return;
  365. }
  366. if (preloadValue.BitmapSource is null)
  367. {
  368. preloadValue.BitmapSource = ImageFunctions.ImageErrorMessage();
  369. }
  370. }
  371. else
  372. {
  373. while (preloadValue.IsLoading)
  374. {
  375. // Make loading skippable
  376. if (FolderIndex != index)
  377. {
  378. await Preloader.PreLoad(index).ConfigureAwait(false);
  379. if (GalleryFunctions.IsHorizontalFullscreenOpen || GalleryFunctions.IsVerticalFullscreenOpen)
  380. {
  381. await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
  382. {
  383. GalleryNavigation.FullscreenGalleryNavigation();
  384. });
  385. }
  386. return;
  387. }
  388. // Wait for finnished result
  389. await Task.Delay(20).ConfigureAwait(false); // Using task delay makes it responsive and enables showing thumb whilst loading
  390. }
  391. if (preloadValue.BitmapSource == null) // Show image error, unload if showing image error somehow fails
  392. {
  393. preloadValue = new Preloader.PreloadValue(ImageFunctions.ImageErrorMessage(), false, null);
  394. if (preloadValue == null || preloadValue.BitmapSource == null)
  395. {
  396. await Preloader.PreLoad(index).ConfigureAwait(false);
  397. return;
  398. }
  399. }
  400. }
  401. }
  402. // Make loading skippable
  403. if (FolderIndex != index)
  404. {
  405. if (GalleryFunctions.IsHorizontalFullscreenOpen || GalleryFunctions.IsVerticalFullscreenOpen)
  406. {
  407. await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
  408. {
  409. GalleryNavigation.FullscreenGalleryNavigation();
  410. });
  411. }
  412. await Preloader.PreLoad(index).ConfigureAwait(false);
  413. return;
  414. }
  415. UpdatePic(index, preloadValue.BitmapSource, preloadValue.FileInfo);
  416. // Update PicGallery selected item, if needed
  417. if (GalleryFunctions.IsHorizontalFullscreenOpen || GalleryFunctions.IsVerticalFullscreenOpen)
  418. {
  419. await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
  420. {
  421. GalleryNavigation.FullscreenGalleryNavigation();
  422. });
  423. }
  424. else if (GetToolTipMessage is not null && GetToolTipMessage.IsVisible)
  425. {
  426. ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Render, () =>
  427. {
  428. GetToolTipMessage.Visibility = Visibility.Hidden;
  429. });
  430. }
  431. if (Pics?.Count > 1)
  432. {
  433. _ = Preloader.PreLoad(index).ConfigureAwait(false);
  434. if (FolderIndex == index)
  435. {
  436. _ = Taskbar.Progress((double)index / Pics.Count).ConfigureAwait(false);
  437. }
  438. }
  439. if (ConfigureWindows.GetImageInfoWindow is not null)
  440. {
  441. _ = ImageInfo.UpdateValuesAsync(preloadValue.FileInfo).ConfigureAwait(false);
  442. }
  443. // Add recent files, except when browing archive
  444. if (string.IsNullOrWhiteSpace(TempZipFile) && Pics?.Count > index)
  445. {
  446. History.Add(Pics?[index]);
  447. }
  448. }
  449. #endregion
  450. #region UpdatePic
  451. /// <summary>
  452. /// Update picture, size it and set the title from index
  453. /// </summary>
  454. /// <param name="index"></param>
  455. /// <param name="bitmapSource"></param>
  456. internal static void UpdatePic(int index, BitmapSource? bitmapSource, FileInfo? fileInfo = null)
  457. {
  458. ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Render, () =>
  459. {
  460. if (bitmapSource is null)
  461. {
  462. bitmapSource = ImageFunctions.ImageErrorMessage();
  463. if (bitmapSource is null)
  464. {
  465. UnexpectedError();
  466. return;
  467. }
  468. }
  469. // Scroll to top if scroll enabled
  470. if (Settings.Default.ScrollEnabled)
  471. {
  472. ConfigureWindows.GetMainWindow.Scroller.ScrollToTop();
  473. }
  474. // Reset transforms if needed
  475. if (Rotation.Flipped || Rotation.Rotateint != 0)
  476. {
  477. Rotation.Flipped = false;
  478. Rotation.Rotateint = 0;
  479. if (GetQuickSettingsMenu is not null && GetQuickSettingsMenu.FlipButton is not null)
  480. {
  481. GetQuickSettingsMenu.FlipButton.TheButton.IsChecked = false;
  482. }
  483. ConfigureWindows.GetMainWindow.MainImage.LayoutTransform = null;
  484. }
  485. // Loads gif from XamlAnimatedGif if neccesary
  486. string? ext = fileInfo is null ? Path.GetExtension(Pics?[index]) : fileInfo.Extension;
  487. if (ext is not null && ext.Equals(".gif", StringComparison.OrdinalIgnoreCase))
  488. {
  489. AnimationBehavior.SetSourceUri(ConfigureWindows.GetMainWindow.MainImage, new Uri(Pics?[index]));
  490. }
  491. else
  492. {
  493. ConfigureWindows.GetMainWindow.MainImage.Source = bitmapSource;
  494. }
  495. FitImage(bitmapSource.PixelWidth, bitmapSource.PixelHeight);
  496. SetTitleString(bitmapSource.PixelWidth, bitmapSource.PixelHeight, index, fileInfo);
  497. });
  498. }
  499. /// <summary>
  500. /// Update picture, size it and set the title from string
  501. /// </summary>
  502. /// <param name="imageName"></param>
  503. /// <param name="bitmapSource"></param>
  504. internal static void UpdatePic(string imageName, BitmapSource bitmapSource)
  505. {
  506. ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Render, () =>
  507. {
  508. Unload(false);
  509. if (Settings.Default.ScrollEnabled)
  510. {
  511. ConfigureWindows.GetMainWindow.Scroller.ScrollToTop();
  512. }
  513. ConfigureWindows.GetMainWindow.MainImage.Source = bitmapSource;
  514. FitImage(bitmapSource.PixelWidth, bitmapSource.PixelHeight);
  515. SetTitleString(bitmapSource.PixelWidth, bitmapSource.PixelHeight, imageName);
  516. CloseToolTipMessage();
  517. ToggleStartUpUC(true);
  518. });
  519. _ = Taskbar.NoProgress().ConfigureAwait(false);
  520. FolderIndex = 0;
  521. _ = ImageInfo.UpdateValuesAsync(null).ConfigureAwait(false);
  522. }
  523. /// <summary>
  524. /// Load a picture from a prepared bitmap
  525. /// </summary>
  526. /// <param name="pic"></param>
  527. /// <param name="imageName"></param>
  528. internal static void LoadPicFromBitmap(BitmapSource bitmap, string imageName)
  529. {
  530. ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Render, () =>
  531. {
  532. SetLoadingString();
  533. });
  534. UpdatePic(imageName, bitmap);
  535. ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Render, () =>
  536. {
  537. ToggleStartUpUC(true);
  538. });
  539. }
  540. /// <summary>
  541. /// Load a picture from a prepared string
  542. /// </summary>
  543. /// <param name="pic"></param>
  544. /// <param name="imageName"></param>
  545. internal static async Task LoadPreparedPicAsync(string file, string imageName, bool isGif)
  546. {
  547. FileInfo fileInfo = new FileInfo(file);
  548. BitmapSource? bitmapSource = isGif ? null : await ImageDecoder.ReturnBitmapSourceAsync(fileInfo).ConfigureAwait(false);
  549. await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, async () =>
  550. {
  551. ToggleStartUpUC(true);
  552. if (Settings.Default.ScrollEnabled)
  553. {
  554. ConfigureWindows.GetMainWindow.Scroller.ScrollToTop();
  555. }
  556. if (isGif)
  557. {
  558. Size? imageSize = await ImageSizeFunctions.GetImageSizeAsync(file).ConfigureAwait(true);
  559. if (imageSize.HasValue)
  560. {
  561. FitImage(imageSize.Value.Width, imageSize.Value.Height);
  562. SetTitleString((int)imageSize.Value.Width, (int)imageSize.Value.Height, imageName);
  563. }
  564. AnimationBehavior.SetSourceUri(ConfigureWindows.GetMainWindow.MainImage, new Uri(file));
  565. }
  566. else if (bitmapSource != null)
  567. {
  568. ConfigureWindows.GetMainWindow.MainImage.Source = bitmapSource;
  569. SetTitleString(bitmapSource.PixelWidth, bitmapSource.PixelHeight, imageName);
  570. FitImage(bitmapSource.PixelWidth, bitmapSource.PixelHeight);
  571. }
  572. else
  573. {
  574. UnexpectedError();
  575. return;
  576. }
  577. CloseToolTipMessage();
  578. });
  579. await Taskbar.NoProgress().ConfigureAwait(false);
  580. FolderIndex = 0;
  581. DeleteFiles.DeleteTempFiles();
  582. }
  583. /// <summary>
  584. /// Load a picture from a base64
  585. /// </summary>
  586. /// <param name="pic"></param>
  587. /// <param name="imageName"></param>
  588. internal static async Task LoadBase64PicAsync(string base64string)
  589. {
  590. if (string.IsNullOrEmpty(base64string))
  591. {
  592. return;
  593. }
  594. var pic = await Base64.Base64StringToBitmap(base64string).ConfigureAwait(false);
  595. if (pic == null)
  596. {
  597. return;
  598. }
  599. if (Application.Current.Resources["Base64Image"] is not string b64)
  600. {
  601. return;
  602. }
  603. UpdatePic(b64, pic);
  604. }
  605. #endregion
  606. static void LoadingPreview(FileInfo fileInfo)
  607. {
  608. ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Render, () =>
  609. {
  610. // Set Loading
  611. SetLoadingString();
  612. if (ConfigureWindows.GetMainWindow.MainImage.Source == null)
  613. {
  614. BitmapSource? bitmapSource = GetBitmapSourceThumb(fileInfo);
  615. if (bitmapSource != null)
  616. {
  617. ConfigureWindows.GetMainWindow.MainImage.Source = bitmapSource;
  618. }
  619. }
  620. // Don't allow image size to stretch the whole screen, fixes when opening new image from unloaded status
  621. if (XWidth < 1)
  622. {
  623. ConfigureWindows.GetMainWindow.MainImage.Width = ConfigureWindows.GetMainWindow.ParentContainer.ActualWidth;
  624. ConfigureWindows.GetMainWindow.MainImage.Height = ConfigureWindows.GetMainWindow.ParentContainer.ActualHeight;
  625. }
  626. });
  627. }
  628. }
  629. }