|
@@ -0,0 +1,645 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Diagnostics;
|
|
|
+using System.Drawing.Imaging;
|
|
|
+using System.Drawing;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+using System.Runtime.InteropServices;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using System.Windows.Forms;
|
|
|
+using System.Windows.Media.Imaging;
|
|
|
+using System.Windows;
|
|
|
+
|
|
|
+namespace PDF_Office.Helper
|
|
|
+{
|
|
|
+ public enum FileExtension
|
|
|
+ {
|
|
|
+ JPG = 255216,
|
|
|
+ GIF = 7173,
|
|
|
+ PNG = 13780,
|
|
|
+ SWF = 6787,
|
|
|
+ RAR = 8297,
|
|
|
+ ZIP = 8075,
|
|
|
+ _7Z = 55122,
|
|
|
+ VALIDFILE = 9999999
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 页面编辑的工具类
|
|
|
+ /// </summary>
|
|
|
+ public static class PageEditTool
|
|
|
+ {
|
|
|
+ [DllImport("user32.dll")]
|
|
|
+ private static extern IntPtr SetCapture(long hWnd);
|
|
|
+
|
|
|
+ [DllImport("user32.dll")]
|
|
|
+ private static extern long ReleaseCapture();
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 打开路径并定位文件...对于@"h:\Bleacher Report - Hardaway with the safe call ??.mp4"这样的,explorer.exe /select,d:xxx不认,用API整它
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="filePath">文件绝对路径</param>
|
|
|
+ [DllImport("shell32.dll", ExactSpelling = true)]
|
|
|
+ private static extern void ILFree(IntPtr pidlList);
|
|
|
+
|
|
|
+ [DllImport("shell32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
|
|
|
+ private static extern IntPtr ILCreateFromPathW(string pszPath);
|
|
|
+
|
|
|
+ [DllImport("shell32.dll", ExactSpelling = true)]
|
|
|
+ private static extern int SHOpenFolderAndSelectItems(IntPtr pidlList, uint cild, IntPtr children, uint dwFlags);
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 打开文件夹浏览,并选中一个文件(对于文件名称比较不规范的,可以用这个)
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="filePath"></param>
|
|
|
+ public static void ExplorerFile(string filePath)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (!File.Exists(filePath) && !Directory.Exists(filePath))
|
|
|
+ return;
|
|
|
+
|
|
|
+ if (Directory.Exists(filePath))
|
|
|
+ Process.Start(@"explorer.exe", "/select,\"" + filePath + "\"");
|
|
|
+ else
|
|
|
+ {
|
|
|
+ IntPtr pidlList = ILCreateFromPathW(filePath);
|
|
|
+ if (pidlList != IntPtr.Zero)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ Marshal.ThrowExceptionForHR(SHOpenFolderAndSelectItems(pidlList, 0, IntPtr.Zero, 0));
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ ILFree(pidlList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch { }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 强制捕获鼠标
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="hwd"></param>
|
|
|
+ public static void DOSetCapture(long hwd)
|
|
|
+ {
|
|
|
+ SetCapture(hwd);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void DoReleaseCapture()
|
|
|
+ {
|
|
|
+ ReleaseCapture();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 返回文件占有的大小
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="filePath"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static string GetFileSize(string filePath)
|
|
|
+ {
|
|
|
+ //判断当前路径所指向的是否为文件
|
|
|
+ if (File.Exists(filePath))
|
|
|
+ {
|
|
|
+ //定义一个FileInfo对象,使之与filePath所指向的文件向关联,
|
|
|
+ //以获取其大小
|
|
|
+ FileInfo fileInfo = new FileInfo(filePath);
|
|
|
+ var size = fileInfo.Length;
|
|
|
+ string strSize = "";
|
|
|
+ if (size < 1024)
|
|
|
+ {
|
|
|
+ strSize = Math.Round(size / 1024.0, 3) + " KB";
|
|
|
+ }
|
|
|
+ else if (size / 1024 > 1024)
|
|
|
+ {
|
|
|
+ strSize = Math.Round(size / 1024 / 1024.0, 1) + " MB";
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ strSize = Math.Round(size / 1024.0, 1) + " KB";
|
|
|
+ }
|
|
|
+ return strSize;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return string.Empty;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 用照片查看器查看指定路径的图片
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="Path">带文件名的绝对路径</param>
|
|
|
+ public static void BrowsePicture(string Path)
|
|
|
+ {
|
|
|
+ //建立新的系统进程
|
|
|
+ System.Diagnostics.Process process = new System.Diagnostics.Process();
|
|
|
+ //设置文件名,此处为图片的真实路径+文件名
|
|
|
+ process.StartInfo.FileName = Path;
|
|
|
+ //此为关键部分。设置进程运行参数,此时为最大化窗口显示图片。
|
|
|
+ process.StartInfo.Arguments = "rundll32.exe C://WINDOWS//system32//shimgvw.dll,ImageView_Fullscreen";
|
|
|
+ //此项为是否使用Shell执行程序,因系统默认为true,此项也可不设,但若设置必须为true
|
|
|
+ process.StartInfo.UseShellExecute = true;
|
|
|
+ //此处可以更改进程所打开窗体的显示样式,可以不设
|
|
|
+ process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
|
|
|
+ process.Start();
|
|
|
+ process.Dispose();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 校验PageRange 输入是否合法,且可返回List<int> Pages 存放的索引值
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="pageList">返回的页面集合</param>
|
|
|
+ /// <param name="pageRange">需要判断的文本</param>
|
|
|
+ /// <param name="count">页面总数</param>
|
|
|
+ /// <param name="enumerationSeparator">例 new char[] { ',' }</param>
|
|
|
+ /// <param name="rangeSeparator">例 new char[] { '-' }</param>
|
|
|
+ /// <param name="inittag"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static bool GetPagesInRange(ref List<int> pageList, string pageRange, int count, char[] enumerationSeparator, char[] rangeSeparator, bool inittag = false)
|
|
|
+ {
|
|
|
+ string[] rangeSplit = pageRange.Split(enumerationSeparator);//根据分隔符 拆分字符串
|
|
|
+
|
|
|
+ pageList.Clear();
|
|
|
+
|
|
|
+ foreach (string range in rangeSplit)
|
|
|
+ {
|
|
|
+ int starttag = 1;
|
|
|
+ if (inittag)
|
|
|
+ {
|
|
|
+ starttag = 0;
|
|
|
+ }
|
|
|
+ if (range.Contains("-"))//连续页
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ string[] limits = range.Split(rangeSeparator);//对子字符串再根据”-“ 拆分
|
|
|
+ if (limits.Length >= 2 && !string.IsNullOrWhiteSpace(limits[0]) && !string.IsNullOrWhiteSpace(limits[1]))
|
|
|
+ {
|
|
|
+ int start = int.Parse(limits[0]);
|
|
|
+ int end = int.Parse(limits[1]);
|
|
|
+
|
|
|
+ if ((start < starttag) || (end > count) || (start > end))
|
|
|
+ {
|
|
|
+ //throw new Exception(string.Format("Invalid page(s) in range {0} - {1}", start, end));
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = start; i <= end; ++i)
|
|
|
+ {
|
|
|
+ if (pageList.Contains(i))
|
|
|
+ {
|
|
|
+ // throw new Exception(string.Format("Invalid page(s) in range {0} - {1}", start, end));
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ pageList.Add(i - 1);
|
|
|
+ }
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ // MessageBox.Show("请检查符号或页码范围是否正确。","提示",MessageBoxButton.OKCancel,MessageBoxImage.Warning);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ int pageNr;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ // Single page
|
|
|
+ pageNr = int.Parse(range);//单页
|
|
|
+ }
|
|
|
+ catch (Exception)//格式不正确时
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (pageNr < starttag || pageNr > count)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ //throw new Exception(string.Format("Invalid page {0}", pageNr));
|
|
|
+ }
|
|
|
+ if (pageList.Contains(pageNr))
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ // throw new Exception(string.Format("Invalid page {0}", pageNr));
|
|
|
+ }
|
|
|
+ pageList.Add(pageNr - 1);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 返回指定路径的图标
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="path"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static BitmapSource ToBitmapSource(string path)
|
|
|
+ {
|
|
|
+ System.Drawing.Icon ico = System.Drawing.Icon.ExtractAssociatedIcon(path);
|
|
|
+ System.Drawing.Bitmap bitmap = ico.ToBitmap();
|
|
|
+ BitmapSource returnSource;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ returnSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ returnSource = null;
|
|
|
+ }
|
|
|
+ return returnSource;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static string GetPageParmFromList(List<int> pagesList)
|
|
|
+ {
|
|
|
+ string pageParam = "";
|
|
|
+ if (pagesList.Count != 0)
|
|
|
+ {
|
|
|
+ pagesList.Sort();//先对页码排序
|
|
|
+
|
|
|
+ for (int i = 0; i < pagesList.Count; i++)
|
|
|
+ {
|
|
|
+ if (i == 0)
|
|
|
+ {
|
|
|
+ pageParam += pagesList[0].ToString();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (pagesList[i] == pagesList[i - 1] + 1)//页码连续
|
|
|
+ {
|
|
|
+ if (i >= 2)
|
|
|
+ {
|
|
|
+ if (pagesList[i - 1] != pagesList[i - 2] + 1)
|
|
|
+ pageParam += "-";
|
|
|
+ }
|
|
|
+ else
|
|
|
+ pageParam += "-";
|
|
|
+
|
|
|
+ if (i == pagesList.Count - 1)
|
|
|
+ {
|
|
|
+ pageParam += pagesList[i].ToString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else//页码不连续时
|
|
|
+ {
|
|
|
+ if (i >= 2)
|
|
|
+ {
|
|
|
+ if (pagesList[i - 1] == pagesList[i - 2] + 1)
|
|
|
+ pageParam += pagesList[i - 1].ToString();
|
|
|
+ }
|
|
|
+ pageParam += "," + pagesList[i].ToString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return pageParam;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 检测文件是否重复 追加尾号
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="path"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static string CreateFilePath(string path)
|
|
|
+ {
|
|
|
+ int i = 1;
|
|
|
+ string oldDestName = path;
|
|
|
+ do
|
|
|
+ {
|
|
|
+ if (File.Exists(path))
|
|
|
+ {
|
|
|
+ int lastDot = oldDestName.LastIndexOf('.');
|
|
|
+
|
|
|
+ string fileExtension = string.Empty;
|
|
|
+
|
|
|
+ string fileName = oldDestName;
|
|
|
+
|
|
|
+ if (lastDot > 0)
|
|
|
+ {
|
|
|
+ fileExtension = fileName.Substring(lastDot);
|
|
|
+
|
|
|
+ fileName = fileName.Substring(0, lastDot);
|
|
|
+ }
|
|
|
+
|
|
|
+ path = fileName + string.Format(@"({0})", i) + fileExtension;
|
|
|
+ }
|
|
|
+ ++i;
|
|
|
+ } while (File.Exists(path));
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 检查文件夹是否重复 追加尾号
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="path"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static string CreateFolder(string folder)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ int i = 1;
|
|
|
+ string oldDestName = folder;
|
|
|
+ DirectoryInfo info = new DirectoryInfo(folder);
|
|
|
+ do
|
|
|
+ {
|
|
|
+ info = new DirectoryInfo(folder);
|
|
|
+ if (info.Exists)
|
|
|
+ {
|
|
|
+ string fileName = oldDestName;
|
|
|
+
|
|
|
+ folder = fileName + string.Format(@"({0})", i);
|
|
|
+ }
|
|
|
+ ++i;
|
|
|
+ } while (info.Exists);
|
|
|
+ info.Create();
|
|
|
+ }
|
|
|
+ catch { return null; }
|
|
|
+ return folder;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 文件名称是否合法
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="fileName">文件名</param>
|
|
|
+ /// <returns>返回true是合法</returns>
|
|
|
+ public static bool IsValidFileName(string fileName)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ foreach (char invalidChar in Path.GetInvalidFileNameChars())
|
|
|
+ {
|
|
|
+ if (fileName.Contains(invalidChar.ToString()))
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 悬浮提示框(气泡形式)
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="control">显示提示的控件</param>
|
|
|
+ /// <param name="tipMsg">内容</param>
|
|
|
+ /// <param name="tipTitle">标题</param>
|
|
|
+ /// <param name="delay">调用该函数而显示时间</param>
|
|
|
+ /// <param name="autoPopDelay">悬浮控件上的显示时间</param>
|
|
|
+ public static async void ShowToolTip(Control control, string tipMsg, string tipTitle = "", int delay = 2000, int autoPopDelay = 0)
|
|
|
+ {
|
|
|
+ if (control == null || string.IsNullOrEmpty(tipMsg))
|
|
|
+ return;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ToolTip toolTip = new ToolTip();
|
|
|
+
|
|
|
+ //if (string.IsNullOrEmpty(tipTitle))
|
|
|
+ // tipTitle = App.MainPageLoader.GetString("Main_HintWarningTilte");
|
|
|
+
|
|
|
+ toolTip.ToolTipTitle = tipTitle;
|
|
|
+ toolTip.SetToolTip(control, tipMsg);
|
|
|
+ toolTip.IsBalloon = true;
|
|
|
+ toolTip.ToolTipIcon = ToolTipIcon.Warning;
|
|
|
+ toolTip.ShowAlways = false;
|
|
|
+ toolTip.AutoPopDelay = autoPopDelay;
|
|
|
+ toolTip.Show(tipMsg, control);
|
|
|
+ await Task.Delay(delay);
|
|
|
+ toolTip.Hide(control);
|
|
|
+ }
|
|
|
+ catch { }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static BitmapImage ToBitmapImage(System.Drawing.Bitmap ImageOriginal)
|
|
|
+ {
|
|
|
+ System.Drawing.Bitmap ImageOriginalBase = new System.Drawing.Bitmap(ImageOriginal);
|
|
|
+ BitmapImage bitmapImage = new BitmapImage();
|
|
|
+ using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
|
|
|
+ {
|
|
|
+ //ImageOriginalBase.Save(ms, ImageOriginalBase.RawFormat);
|
|
|
+ ImageOriginalBase.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
|
|
|
+ bitmapImage.BeginInit();
|
|
|
+ bitmapImage.StreamSource = ms;
|
|
|
+ bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
|
|
|
+ bitmapImage.EndInit();
|
|
|
+ bitmapImage.Freeze();
|
|
|
+ }
|
|
|
+ return bitmapImage;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取图片的旋转角度
|
|
|
+ /// 获取后可以 通过bitmap的RotateFlip 回位
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="path">文件路径</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static int GetPictureRotation(string path)
|
|
|
+ {
|
|
|
+ int rotate = 0;
|
|
|
+ using (var image = System.Drawing.Image.FromFile(path))
|
|
|
+ {
|
|
|
+ foreach (var prop in image.PropertyItems)
|
|
|
+ {
|
|
|
+ if (prop.Id == 0x112)
|
|
|
+ {
|
|
|
+ if (prop.Value[0] == 6)
|
|
|
+ rotate = 90;
|
|
|
+ if (prop.Value[0] == 8)
|
|
|
+ rotate = -90;
|
|
|
+ if (prop.Value[0] == 3)
|
|
|
+ rotate = 180;
|
|
|
+ prop.Value[0] = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return rotate;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] GetImageByteFromPath(string ImagePath, string fileExt, out double width, out double height)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ #region 跟图章一样的转换方法。
|
|
|
+
|
|
|
+ if (ImagePath != null && ImagePath.Length > 0 && File.Exists(ImagePath))
|
|
|
+ {
|
|
|
+ FileStream fileData = File.OpenRead(ImagePath);
|
|
|
+ BitmapFrame frame = null;
|
|
|
+ width = 0;
|
|
|
+ height = 0;
|
|
|
+
|
|
|
+ if (fileExt.Contains(".jpg") || fileExt.Contains(".jpeg"))
|
|
|
+ {
|
|
|
+ JpegBitmapDecoder decoder = (JpegBitmapDecoder)JpegBitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
|
|
|
+ frame = decoder.Frames[0];
|
|
|
+ width = frame.Width;
|
|
|
+ height = frame.Height;
|
|
|
+ }
|
|
|
+ else if (fileExt.Contains(".png"))
|
|
|
+ {
|
|
|
+ PngBitmapDecoder decoder = (PngBitmapDecoder)PngBitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
|
|
|
+ frame = decoder.Frames[0];
|
|
|
+ width = frame.Width;
|
|
|
+ height = frame.Height;
|
|
|
+ }
|
|
|
+ else if (fileExt.Contains(".bmp"))
|
|
|
+ {
|
|
|
+ BmpBitmapDecoder decoder = (BmpBitmapDecoder)BmpBitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
|
|
|
+ frame = decoder.Frames[0];
|
|
|
+ width = frame.Width;
|
|
|
+ height = frame.Height;
|
|
|
+ }
|
|
|
+ else//默认采用png的解码方式
|
|
|
+ {
|
|
|
+ BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
|
|
|
+ frame = decoder.Frames[0];
|
|
|
+ width = frame.Width;
|
|
|
+ height = frame.Height;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (frame != null)
|
|
|
+ {
|
|
|
+ var ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
|
|
|
+ frame.CopyPixels(ImageArray, frame.PixelWidth * 4, 0);
|
|
|
+ return ImageArray;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ width = 0;
|
|
|
+ height = 0;
|
|
|
+ return null;
|
|
|
+
|
|
|
+ #endregion 跟图章一样的转换方法。
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ width = 0;
|
|
|
+ height = 0;
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// BitmapImage 转Bitmap
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="bitmapImage"></param>
|
|
|
+ /// <param name="isPng"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static Bitmap GetBitmapByBitmapImage(BitmapImage bitmapImage, bool isPng = false)
|
|
|
+ {
|
|
|
+ Bitmap bitmap;
|
|
|
+ MemoryStream outStream = new MemoryStream();
|
|
|
+ BitmapEncoder enc = new JpegBitmapEncoder();
|
|
|
+ if (isPng)
|
|
|
+ {
|
|
|
+ enc = new PngBitmapEncoder();
|
|
|
+ }
|
|
|
+ enc.Frames.Add(BitmapFrame.Create(bitmapImage));
|
|
|
+ enc.Save(outStream);
|
|
|
+ bitmap = new Bitmap(outStream);
|
|
|
+ return bitmap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 根据图片数据判断图片类型
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="fileName"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static FileExtension CheckTextFile(string fileName)
|
|
|
+ {
|
|
|
+ FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
|
|
|
+ System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
|
|
|
+ string fileType = string.Empty; ;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ byte data = br.ReadByte();
|
|
|
+ fileType += data.ToString();
|
|
|
+ data = br.ReadByte();
|
|
|
+ fileType += data.ToString();
|
|
|
+ FileExtension extension;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ extension = (FileExtension)Enum.Parse(typeof(FileExtension), fileType);
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ extension = FileExtension.VALIDFILE;
|
|
|
+ }
|
|
|
+ return extension;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ if (fs != null)
|
|
|
+ {
|
|
|
+ fs.Close();
|
|
|
+ br.Close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 压缩图片到600*600以内,压缩成功后返回压缩完成的缓存路径
|
|
|
+ /// 如果压缩 失败 返回原图路径
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="filePath"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static string CompressImage(string filePath, int maxwidth = 600)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ string sourcepath = filePath;
|
|
|
+ var guid = Guid.NewGuid().ToString();
|
|
|
+ string folder = Path.Combine(App.CurrentPath, "Temp");
|
|
|
+ if (!Directory.Exists(folder))
|
|
|
+ {
|
|
|
+ Directory.CreateDirectory(folder);
|
|
|
+ }
|
|
|
+ var path = System.IO.Path.Combine(App.CurrentPath, "Temp", guid);
|
|
|
+
|
|
|
+ Bitmap bitmap = new Bitmap(sourcepath);
|
|
|
+
|
|
|
+ var b = bitmap;
|
|
|
+ //bitmap.Dispose();
|
|
|
+
|
|
|
+ double scale = Math.Min((double)maxwidth / b.Width, (double)maxwidth / b.Height);
|
|
|
+ scale = Math.Min(scale, 1);
|
|
|
+ System.Drawing.Size newsize = new System.Drawing.Size(maxwidth, maxwidth);
|
|
|
+ newsize.Width = (int)(scale * b.Width);
|
|
|
+ newsize.Height = (int)(scale * b.Height);
|
|
|
+
|
|
|
+ if (!File.Exists(path))
|
|
|
+ {
|
|
|
+ if (CheckTextFile(sourcepath) == FileExtension.PNG)
|
|
|
+ {
|
|
|
+ using (var bp = new Bitmap(b, (int)newsize.Width, (int)newsize.Height))
|
|
|
+ {
|
|
|
+ bp.Save(path, ImageFormat.Png);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ using (var bp = new Bitmap(b, (int)newsize.Width, (int)newsize.Height))
|
|
|
+ {
|
|
|
+ bp.Save(path, ImageFormat.Jpeg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ return filePath;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|