123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Xml.Linq;
- namespace BellwetherBackend.Utility
- {
- public class GlobalFunction
- {
- //圖片規格
- public static string[] imgDimList = { ".jpg", ".png", ".bmp", ".jpeg" };
- //影片規格
- public static string[] mediaDimList = { ".avi", ".mp4", "mpeg", ".mpg", ".wmv" };
- //簡報
- public static string[] pptDimList = { ".ppt", ".pptx" };
- public static ImageSource GetBitMap(string imgpath)
- {
- BitmapImage bitmapImg = new BitmapImage();
- if (File.Exists(imgpath))
- {
- bitmapImg.BeginInit();
- bitmapImg.UriSource = new Uri(imgpath, UriKind.RelativeOrAbsolute);
- //忽略原始圖檔的色盤設定,使用預設值,可縮短載入圖檔的時間
- //如有遇到色彩不正確的問題,或者原圖有特殊的自定義色盤,請不要使用此設定
- bitmapImg.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
- //使用以下的預先載入設定,在BitmapImage創建完成之後,可立刻釋放其所占用或鎖住的資源,以免咬住檔案
- bitmapImg.CacheOption = BitmapCacheOption.OnLoad;
- //根據欲顯示的區域長或寬,來設定 DecodePixelHeight 或 DecodePixelWidth 的值
- //bitmapImg.DecodePixelHeight = THUMB_HEIGHT;
- //bitmapImg.DecodePixelWidth = THUMB_WIDTH;
- bitmapImg.EndInit();
- bitmapImg.Freeze();
- }
- return bitmapImg;
- }
- public static ImageSource GetBitMap(string imgpath, double resolutionWidth, double resolutionHeight)
- {
- BitmapImage bitmapImg = new BitmapImage();
- if (File.Exists(imgpath))
- {
- bitmapImg.BeginInit();
- bitmapImg.UriSource = new Uri(imgpath, UriKind.RelativeOrAbsolute);
- //忽略原始圖檔的色盤設定,使用預設值,可縮短載入圖檔的時間
- //如有遇到色彩不正確的問題,或者原圖有特殊的自定義色盤,請不要使用此設定
- bitmapImg.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
- //使用以下的預先載入設定,在BitmapImage創建完成之後,可立刻釋放其所占用或鎖住的資源,以免咬住檔案
- bitmapImg.CacheOption = BitmapCacheOption.OnLoad;
- //根據欲顯示的區域長或寬,來設定 DecodePixelHeight 或 DecodePixelWidth 的值
- BitmapDecoder decoder = BitmapDecoder.Create(new Uri(imgpath), BitmapCreateOptions.None, BitmapCacheOption.None);
- BitmapFrame frame = decoder.Frames[0];
- //ShellFile shellFile = ShellFile.FromFilePath(imgpath);
- var imageSourceHeight = frame.PixelHeight; //shellFile.Properties.System.Image.VerticalSize.Value;
- var imageSourceWidth = frame.PixelWidth;//shellFile.Properties.System.Image.HorizontalSize.Value;
- var imageSourceRatio = imageSourceWidth / (imageSourceHeight == 0 ? 1 : imageSourceHeight);
- if (imageSourceHeight > resolutionHeight && imageSourceWidth > resolutionWidth)
- {
- if (imageSourceRatio > resolutionWidth / resolutionHeight)
- {
- bitmapImg.DecodePixelWidth = Convert.ToInt32(resolutionWidth);
- }
- else
- {
- bitmapImg.DecodePixelHeight = Convert.ToInt32(resolutionHeight);
- }
- }
- else if (imageSourceHeight > resolutionHeight)
- {
- bitmapImg.DecodePixelHeight = Convert.ToInt32(resolutionHeight);
- }
- else if (imageSourceWidth > resolutionWidth)
- {
- bitmapImg.DecodePixelWidth = Convert.ToInt32(resolutionWidth);
- }
- bitmapImg.EndInit();
- bitmapImg.Freeze();
- }
- return bitmapImg;
- }
- public static bool CheckFileIsImage(string file)
- {
- if (GlobalFunction.imgDimList.Contains(System.IO.Path.GetExtension(file.ToLower())))
- {
- return true;
- }
- return false;
- }
- public static bool CheckFileIsVideo(string file)
- {
- if (GlobalFunction.mediaDimList.Contains(System.IO.Path.GetExtension(file.ToLower())))
- {
- return true;
- }
- return false;
- }
- public static string GetFolderFileName(string rootPath, string filename)
- {
- string result = "";
- Random Counter = new Random(Guid.NewGuid().GetHashCode());
- do
- {
- result = (Counter.Next(0, 10000)).ToString() + System.IO.Path.GetExtension(filename).ToLower();
- }
- while (File.Exists(Path.Combine(rootPath, result)));
- return result;
- }
- #region icon
- public static Geometry AddBtnData;
- public static Geometry DeleteBtnData;
- public static Geometry MoveUpBtnData;
- public static Geometry MoveDownBtnData;
- public static Geometry MapBtnData;
- public static Geometry ImgDeleteData;
- public static Geometry ConnectBtnData;
- public static Geometry DisconnectBtnData;
- public static Geometry UpdateBtnData;
- public GlobalFunction()
- {
- LoadAllIconData();
- //FillBaseFileSystem();
- }
- private Geometry LoadIconDataFromResource(string xamlName)
- {
- Geometry iconData = null;
- var assembly = GetType().Assembly;
- using (var stream = assembly.GetManifestResourceStream(xamlName))
- {
- var doc = XDocument.Load(stream);
- var path = doc.Root.Element("{http://schemas.microsoft.com/winfx/2006/xaml/presentation}Path");
- if (path != null)
- {
- var data = (string)path.Attribute("Data");
- iconData = PathGeometry.Parse(data);
- }
- return iconData;
- }
- }
- private void LoadAllIconData()
- {
- AddBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.add.xaml");
- DeleteBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.delete.xaml");
- MoveUpBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.upward.xaml");
- MoveDownBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.downward.xaml");
- MapBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.map.xaml");
- ImgDeleteData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.imgdelete.xaml");
- ConnectBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.connect.xaml");
- DisconnectBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.disconnect.xaml");
- UpdateBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.update.xaml");
- }
- #endregion
- }
- public class ScrollViewerUtilities
- {
- #region HorizontalOffset
- /// <summary>
- /// HorizontalOffset Attached Dependency Property
- /// </summary>
- public static readonly DependencyProperty HorizontalOffsetProperty =
- DependencyProperty.RegisterAttached("HorizontalOffset", typeof(double), typeof(ScrollViewerUtilities),
- new FrameworkPropertyMetadata((double)0.0,
- new PropertyChangedCallback(OnHorizontalOffsetChanged)));
- /// <summary>
- /// Gets the HorizontalOffset property. This dependency property
- /// indicates ....
- /// </summary>
- public static double GetHorizontalOffset(DependencyObject d)
- {
- return (double)d.GetValue(HorizontalOffsetProperty);
- }
- /// <summary>
- /// Sets the HorizontalOffset property. This dependency property
- /// indicates ....
- /// </summary>
- public static void SetHorizontalOffset(DependencyObject d, double value)
- {
- d.SetValue(HorizontalOffsetProperty, value);
- }
- /// <summary>
- /// Handles changes to the HorizontalOffset property.
- /// </summary>
- private static void OnHorizontalOffsetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- var viewer = (ScrollViewer)d;
- viewer.ScrollToHorizontalOffset((double)e.NewValue);
- }
- #endregion
- #region VerticalOffset
- /// <summary>
- /// VerticalOffset Attached Dependency Property
- /// </summary>
- public static readonly DependencyProperty VerticalOffsetProperty =
- DependencyProperty.RegisterAttached("VerticalOffset", typeof(double), typeof(ScrollViewerUtilities),
- new FrameworkPropertyMetadata((double)0.0,
- new PropertyChangedCallback(OnVerticalOffsetChanged)));
- /// <summary>
- /// Gets the VerticalOffset property. This dependency property
- /// indicates ....
- /// </summary>
- public static double GetVerticalOffset(DependencyObject d)
- {
- return (double)d.GetValue(VerticalOffsetProperty);
- }
- /// <summary>
- /// Sets the VerticalOffset property. This dependency property
- /// indicates ....
- /// </summary>
- public static void SetVerticalOffset(DependencyObject d, double value)
- {
- d.SetValue(VerticalOffsetProperty, value);
- }
- /// <summary>
- /// Handles changes to the VerticalOffset property.
- /// </summary>
- private static void OnVerticalOffsetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- var viewer = (ScrollViewer)d;
- viewer.ScrollToVerticalOffset((double)e.NewValue);
- }
- #endregion
- }
- }
|