GlobalFunction.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Imaging;
  11. using System.Xml.Linq;
  12. namespace BellwetherBackend.Utility
  13. {
  14. public class GlobalFunction
  15. {
  16. //圖片規格
  17. public static string[] imgDimList = { ".jpg", ".png", ".bmp", ".jpeg" };
  18. //影片規格
  19. public static string[] mediaDimList = { ".avi", ".mp4", "mpeg", ".mpg", ".wmv" };
  20. //簡報
  21. public static string[] pptDimList = { ".ppt", ".pptx" };
  22. public static ImageSource GetBitMap(string imgpath)
  23. {
  24. BitmapImage bitmapImg = new BitmapImage();
  25. if (File.Exists(imgpath))
  26. {
  27. bitmapImg.BeginInit();
  28. bitmapImg.UriSource = new Uri(imgpath, UriKind.RelativeOrAbsolute);
  29. //忽略原始圖檔的色盤設定,使用預設值,可縮短載入圖檔的時間
  30. //如有遇到色彩不正確的問題,或者原圖有特殊的自定義色盤,請不要使用此設定
  31. bitmapImg.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
  32. //使用以下的預先載入設定,在BitmapImage創建完成之後,可立刻釋放其所占用或鎖住的資源,以免咬住檔案
  33. bitmapImg.CacheOption = BitmapCacheOption.OnLoad;
  34. //根據欲顯示的區域長或寬,來設定 DecodePixelHeight 或 DecodePixelWidth 的值
  35. //bitmapImg.DecodePixelHeight = THUMB_HEIGHT;
  36. //bitmapImg.DecodePixelWidth = THUMB_WIDTH;
  37. bitmapImg.EndInit();
  38. bitmapImg.Freeze();
  39. }
  40. return bitmapImg;
  41. }
  42. public static ImageSource GetBitMap(string imgpath, double resolutionWidth, double resolutionHeight)
  43. {
  44. BitmapImage bitmapImg = new BitmapImage();
  45. if (File.Exists(imgpath))
  46. {
  47. bitmapImg.BeginInit();
  48. bitmapImg.UriSource = new Uri(imgpath, UriKind.RelativeOrAbsolute);
  49. //忽略原始圖檔的色盤設定,使用預設值,可縮短載入圖檔的時間
  50. //如有遇到色彩不正確的問題,或者原圖有特殊的自定義色盤,請不要使用此設定
  51. bitmapImg.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
  52. //使用以下的預先載入設定,在BitmapImage創建完成之後,可立刻釋放其所占用或鎖住的資源,以免咬住檔案
  53. bitmapImg.CacheOption = BitmapCacheOption.OnLoad;
  54. //根據欲顯示的區域長或寬,來設定 DecodePixelHeight 或 DecodePixelWidth 的值
  55. BitmapDecoder decoder = BitmapDecoder.Create(new Uri(imgpath), BitmapCreateOptions.None, BitmapCacheOption.None);
  56. BitmapFrame frame = decoder.Frames[0];
  57. //ShellFile shellFile = ShellFile.FromFilePath(imgpath);
  58. var imageSourceHeight = frame.PixelHeight; //shellFile.Properties.System.Image.VerticalSize.Value;
  59. var imageSourceWidth = frame.PixelWidth;//shellFile.Properties.System.Image.HorizontalSize.Value;
  60. var imageSourceRatio = imageSourceWidth / (imageSourceHeight == 0 ? 1 : imageSourceHeight);
  61. if (imageSourceHeight > resolutionHeight && imageSourceWidth > resolutionWidth)
  62. {
  63. if (imageSourceRatio > resolutionWidth / resolutionHeight)
  64. {
  65. bitmapImg.DecodePixelWidth = Convert.ToInt32(resolutionWidth);
  66. }
  67. else
  68. {
  69. bitmapImg.DecodePixelHeight = Convert.ToInt32(resolutionHeight);
  70. }
  71. }
  72. else if (imageSourceHeight > resolutionHeight)
  73. {
  74. bitmapImg.DecodePixelHeight = Convert.ToInt32(resolutionHeight);
  75. }
  76. else if (imageSourceWidth > resolutionWidth)
  77. {
  78. bitmapImg.DecodePixelWidth = Convert.ToInt32(resolutionWidth);
  79. }
  80. bitmapImg.EndInit();
  81. bitmapImg.Freeze();
  82. }
  83. return bitmapImg;
  84. }
  85. public static bool CheckFileIsImage(string file)
  86. {
  87. if (GlobalFunction.imgDimList.Contains(System.IO.Path.GetExtension(file.ToLower())))
  88. {
  89. return true;
  90. }
  91. return false;
  92. }
  93. public static bool CheckFileIsVideo(string file)
  94. {
  95. if (GlobalFunction.mediaDimList.Contains(System.IO.Path.GetExtension(file.ToLower())))
  96. {
  97. return true;
  98. }
  99. return false;
  100. }
  101. public static string GetFolderFileName(string rootPath, string filename)
  102. {
  103. string result = "";
  104. Random Counter = new Random(Guid.NewGuid().GetHashCode());
  105. do
  106. {
  107. result = (Counter.Next(0, 10000)).ToString() + System.IO.Path.GetExtension(filename).ToLower();
  108. }
  109. while (File.Exists(Path.Combine(rootPath, result)));
  110. return result;
  111. }
  112. #region icon
  113. public static Geometry AddBtnData;
  114. public static Geometry DeleteBtnData;
  115. public static Geometry MoveUpBtnData;
  116. public static Geometry MoveDownBtnData;
  117. public static Geometry MapBtnData;
  118. public static Geometry ImgDeleteData;
  119. public static Geometry ConnectBtnData;
  120. public static Geometry DisconnectBtnData;
  121. public static Geometry UpdateBtnData;
  122. public GlobalFunction()
  123. {
  124. LoadAllIconData();
  125. //FillBaseFileSystem();
  126. }
  127. private Geometry LoadIconDataFromResource(string xamlName)
  128. {
  129. Geometry iconData = null;
  130. var assembly = GetType().Assembly;
  131. using (var stream = assembly.GetManifestResourceStream(xamlName))
  132. {
  133. var doc = XDocument.Load(stream);
  134. var path = doc.Root.Element("{http://schemas.microsoft.com/winfx/2006/xaml/presentation}Path");
  135. if (path != null)
  136. {
  137. var data = (string)path.Attribute("Data");
  138. iconData = PathGeometry.Parse(data);
  139. }
  140. return iconData;
  141. }
  142. }
  143. private void LoadAllIconData()
  144. {
  145. AddBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.add.xaml");
  146. DeleteBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.delete.xaml");
  147. MoveUpBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.upward.xaml");
  148. MoveDownBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.downward.xaml");
  149. MapBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.map.xaml");
  150. ImgDeleteData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.imgdelete.xaml");
  151. ConnectBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.connect.xaml");
  152. DisconnectBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.disconnect.xaml");
  153. UpdateBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.update.xaml");
  154. }
  155. #endregion
  156. }
  157. public class ScrollViewerUtilities
  158. {
  159. #region HorizontalOffset
  160. /// <summary>
  161. /// HorizontalOffset Attached Dependency Property
  162. /// </summary>
  163. public static readonly DependencyProperty HorizontalOffsetProperty =
  164. DependencyProperty.RegisterAttached("HorizontalOffset", typeof(double), typeof(ScrollViewerUtilities),
  165. new FrameworkPropertyMetadata((double)0.0,
  166. new PropertyChangedCallback(OnHorizontalOffsetChanged)));
  167. /// <summary>
  168. /// Gets the HorizontalOffset property. This dependency property
  169. /// indicates ....
  170. /// </summary>
  171. public static double GetHorizontalOffset(DependencyObject d)
  172. {
  173. return (double)d.GetValue(HorizontalOffsetProperty);
  174. }
  175. /// <summary>
  176. /// Sets the HorizontalOffset property. This dependency property
  177. /// indicates ....
  178. /// </summary>
  179. public static void SetHorizontalOffset(DependencyObject d, double value)
  180. {
  181. d.SetValue(HorizontalOffsetProperty, value);
  182. }
  183. /// <summary>
  184. /// Handles changes to the HorizontalOffset property.
  185. /// </summary>
  186. private static void OnHorizontalOffsetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  187. {
  188. var viewer = (ScrollViewer)d;
  189. viewer.ScrollToHorizontalOffset((double)e.NewValue);
  190. }
  191. #endregion
  192. #region VerticalOffset
  193. /// <summary>
  194. /// VerticalOffset Attached Dependency Property
  195. /// </summary>
  196. public static readonly DependencyProperty VerticalOffsetProperty =
  197. DependencyProperty.RegisterAttached("VerticalOffset", typeof(double), typeof(ScrollViewerUtilities),
  198. new FrameworkPropertyMetadata((double)0.0,
  199. new PropertyChangedCallback(OnVerticalOffsetChanged)));
  200. /// <summary>
  201. /// Gets the VerticalOffset property. This dependency property
  202. /// indicates ....
  203. /// </summary>
  204. public static double GetVerticalOffset(DependencyObject d)
  205. {
  206. return (double)d.GetValue(VerticalOffsetProperty);
  207. }
  208. /// <summary>
  209. /// Sets the VerticalOffset property. This dependency property
  210. /// indicates ....
  211. /// </summary>
  212. public static void SetVerticalOffset(DependencyObject d, double value)
  213. {
  214. d.SetValue(VerticalOffsetProperty, value);
  215. }
  216. /// <summary>
  217. /// Handles changes to the VerticalOffset property.
  218. /// </summary>
  219. private static void OnVerticalOffsetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  220. {
  221. var viewer = (ScrollViewer)d;
  222. viewer.ScrollToVerticalOffset((double)e.NewValue);
  223. }
  224. #endregion
  225. }
  226. }