// 更換圖片路徑 public static void change_img(string path) { try { var image = new BitmapImage(); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; image.DecodePixelWidth = 400; image.UriSource = new Uri(path); image.EndInit(); GloableObject.img_path = path; GloableObject.img_filename = System.IO.Path.GetFileName(path); GloableObject.file_ex = Path.GetExtension(img_filename).ToLower(); if (normal_img.Contains(file_ex)) { mainWin.gif_previewer.Visibility = System.Windows.Visibility.Collapsed; mainWin.normal_img_previewer.Visibility = System.Windows.Visibility.Visible; mainWin.normal_img_previewer.Source = image; } else if (gif_img.Contains(file_ex)) { WpfAnimatedGif.ImageBehavior.SetAutoStart(mainWin.gif_previewer, true); mainWin.normal_img_previewer.Visibility = System.Windows.Visibility.Collapsed; mainWin.gif_previewer.Visibility = System.Windows.Visibility.Visible; ImageBehavior.SetAnimatedSource(mainWin.gif_previewer, image); } else if (video.Contains(file_ex)) { mainWin.normal_img_previewer.Source = new BitmapImage(new Uri(@"\src\file_not_Support.png", UriKind.Relative)); logger("⚠ [Warning] - Not Support Video Files Yet."); return; } else { mainWin.normal_img_previewer.Source = new BitmapImage(new Uri(@"\src\file_not_Support.png", UriKind.Relative)); logger($"⚠ [Warning] - Not Support {file_ex} Files."); } mainWin.imgFileName.Text = GloableObject.img_filename; mainWin.imgFileName.Text = Path.GetFileName(path); GC.Collect(); } catch (System.NotSupportedException) { } catch (System.IO.DirectoryNotFoundException) { GloableObject.logger($"❌ [Error] - NotFound Directory. Pls Choose The Main Directoy"); } catch (System.IndexOutOfRangeException) { GloableObject.logger($"❌ [Error] - No File in this Directory."); } catch (Exception error) { GloableObject.logger($"❌ [Error] - {error}"); } }
// 隨機圖片 public static void random_image(string path) { if (path == string.Empty) { return; } try { // 從資料夾中隨機選取一張圖片 string[] filePaths = Directory.GetFiles(path); var ran = new Random(); int ran_index = ran.Next(filePaths.Length); string fpath = filePaths[ran_index]; GloableObject.img_path = fpath; GloableObject.img_filename = System.IO.Path.GetFileName(fpath); GloableObject.file_ex = Path.GetExtension(img_filename).ToLower(); // 判斷檔案類型 使用哪種圖片顯示器 if (normal_img.Contains(file_ex)) { // 使用 原生Image 更改img_preview的圖片路徑 mainWin.gif_previewer.Visibility = System.Windows.Visibility.Collapsed; mainWin.normal_img_previewer.Visibility = System.Windows.Visibility.Visible; BitmapImage image = change_src(fpath); img = image; mainWin.normal_img_previewer.Source = image; GC.Collect(); } else if (gif_img.Contains(file_ex)) { WpfAnimatedGif.ImageBehavior.SetAutoStart(mainWin.gif_previewer, true); mainWin.normal_img_previewer.Visibility = System.Windows.Visibility.Collapsed; mainWin.gif_previewer.Visibility = System.Windows.Visibility.Visible; // 使用 wpfGIF 更改img_preview的圖片路徑 BitmapImage image = change_src(fpath); img = image; ImageBehavior.SetAnimatedSource(mainWin.gif_previewer, image); GC.Collect(); } else if (video.Contains(file_ex)) { mainWin.normal_img_previewer.Source = new BitmapImage(new Uri(@"\src\file_not_Support.png", UriKind.Relative)); logger("⚠ [Warning] - Not Support Video Files Yet."); return; } else { mainWin.normal_img_previewer.Source = new BitmapImage(new Uri(@"\src\file_not_Support.png", UriKind.Relative)); logger($"⚠ [Warning] - Not Support {file_ex} Files."); } mainWin.imgFileName.Text = GloableObject.img_filename; } catch (System.IO.DirectoryNotFoundException) { GloableObject.logger($"❌ [Error] - NotFound Directory. Pls Choose The Main Directoy"); } catch (System.IndexOutOfRangeException) { GloableObject.logger($"❌ [Error] - No File in this Directory."); } catch (Exception error) { GloableObject.logger($"❌ [Error] - {error}"); } }