123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- using GeekDesk.Constant;
- using System;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Windows;
- using System.Windows.Interop;
- using System.Windows.Media.Imaging;
- namespace GeekDesk.Util
- {
- class ImageUtil
- {
- /// <summary>
- /// 图片数组转 BitmapImage
- /// </summary>
- /// <param name="array"></param>
- /// <returns></returns>
- public static BitmapImage ByteArrToImage(byte[] array)
- {
- using (var ms = new System.IO.MemoryStream(array))
- {
- var image = new BitmapImage();
- image.BeginInit();
- image.CacheOption = BitmapCacheOption.OnLoad; // here
- image.StreamSource = ms;
- image.EndInit();
- return image;
- }
- }
- /// <summary>
- /// BitmapImage 转数组
- /// </summary>
- /// <param name="bi"></param>
- /// <returns></returns>
- public static byte[] BitmapImageToByte(BitmapImage bi)
- {
- using (MemoryStream memStream = new MemoryStream())
- {
- PngBitmapEncoder encoder = new PngBitmapEncoder();
- encoder.Frames.Add(BitmapFrame.Create(bi));
- encoder.Save(memStream);
- return memStream.GetBuffer();
- }
- }
- /// <summary>
- /// byte[]转换成Image
- /// </summary>
- /// <param name="byteArrayIn">二进制图片流</param>
- /// <returns>Image</returns>
- public static Image ByteArrayToImage(byte[] byteArrayIn)
- {
- if (byteArrayIn == null)
- return null;
- using (System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArrayIn))
- {
- System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
- ms.Flush();
- return returnImage;
- }
- }
- /// <summary>
- /// 图片base64 转 BitmapImage
- /// </summary>
- /// <param name="base64"></param>
- /// <returns></returns>
- public static BitmapImage Base64ToBitmapImage(string base64)
- {
- byte[] byteBuffer = Convert.FromBase64String(base64);
- return ByteArrToImage(byteBuffer);
- }
- /// <summary>
- /// 获取文件 icon
- /// </summary>
- /// <param name="filePath">文件路径</param>
- /// <returns></returns>
- public static BitmapImage GetBitmapIconByPath(string filePath)
- {
- if (File.Exists(filePath))
- {
- if (IsImage(filePath)) {
- //图片
- return GetThumbnailByFile(filePath, 256, 256);
- } else
- { //其它文件
- return FileIcon.GetBitmapImage(filePath);
- }
- } else if(Directory.Exists(filePath)) {
- //文件夹
- return ImageUtil.Base64ToBitmapImage(Constants.DEFAULT_DIR_IMAGE_BASE64);
- }
- return null;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="lcFilename">需要改变大小的图片位置</param>
- /// <param name="lnWidth">缩略图的宽度</param>
- /// <param name="lnHeight">缩略图的高度</param>
- /// <returns></returns>
- //public static BitmapImage GetThumbnail(string lcFilename, int lnWidth, int lnHeight)
- //{
- // Bitmap bmpOut = null;
- // try
- // {
- // Bitmap loBMP = new Bitmap(lcFilename);
- // ImageFormat loFormat = loBMP.RawFormat;
- // decimal lnRatio;
- // int lnNewWidth = 0;
- // int lnNewHeight = 0;
- // //如果图像小于缩略图直接返回原图,因为upfront
- // if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
- // return BitmapToBitmapImage(loBMP);
- // if (loBMP.Width > loBMP.Height)
- // {
- // lnRatio = (decimal)lnWidth / loBMP.Width;
- // lnNewWidth = lnWidth;
- // decimal lnTemp = loBMP.Height * lnRatio;
- // lnNewHeight = (int)lnTemp;
- // }
- // else
- // {
- // lnRatio = (decimal)lnHeight / loBMP.Height;
- // lnNewHeight = lnHeight;
- // decimal lnTemp = loBMP.Width * lnRatio;
- // lnNewWidth = (int)lnTemp;
- // }
- // bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
- // Graphics g = Graphics.FromImage(bmpOut);
- // g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
- // g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
- // g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
- // loBMP.Dispose();
- // }
- // catch (Exception e)
- // {
- // return Base64ToBitmapImage(Constants.DEFAULT_IMG_IMAGE_BASE64);
- // }
- // return BitmapToBitmapImage(bmpOut);
- //}
- public static BitmapImage GetThumbnailByFile(string filePath, int tWidth, int tHeight)
- {
- try
- {
- Image img = Image.FromFile(filePath);
- if (img.Width <= tWidth && img.Height <= tHeight)
- {
- return GetBitmapImageByFile(filePath);
- }
- else
- {
- Bitmap loBMP = new Bitmap(filePath);
- ImageFormat loFormat = loBMP.RawFormat;
- decimal lnRatio;
- int lnNewWidth;
- int lnNewHeight;
- if (loBMP.Width > loBMP.Height)
- {
- lnRatio = (decimal)tWidth / loBMP.Width;
- lnNewWidth = tWidth;
- decimal lnTemp = loBMP.Height * lnRatio;
- lnNewHeight = (int)lnTemp;
- }
- else
- {
- lnRatio = (decimal)tHeight / loBMP.Height;
- lnNewHeight = tHeight;
- decimal lnTemp = loBMP.Width * lnRatio;
- lnNewWidth = (int)lnTemp;
- }
- Bitmap bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
- Graphics g = Graphics.FromImage(bmpOut);
- g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
- g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
- g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
- loBMP.Dispose();
- string tempPath = Constants.APP_DIR + "\\temp";
- if (File.Exists(tempPath))
- {
- File.Delete(tempPath);
- }
- bmpOut.Save(tempPath, loFormat);
- BitmapImage bm = GetBitmapImageByFile(tempPath);
- File.Delete(tempPath);
- return bm;
- }
- }
- catch (Exception)
- {
- return Base64ToBitmapImage(Constants.DEFAULT_IMG_IMAGE_BASE64);
- }
-
- }
- public static BitmapImage GetBitmapImageByFile(string filePath)
- {
- BitmapImage bmImg = new BitmapImage();
- bmImg.BeginInit();
- bmImg.CacheOption = BitmapCacheOption.OnLoad;
- using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
- {
- bmImg.StreamSource = fs;
- bmImg.EndInit();
- }
- return bmImg;
- }
- public static BitmapImage MemoryStremToBitMapImage(MemoryStream ms)
- {
- BitmapImage bi = new BitmapImage();
- bi.BeginInit();
- bi.StreamSource = ms;
- bi.CacheOption = BitmapCacheOption.OnLoad;
- bi.EndInit();
- bi.Freeze();
- return bi;
- }
- /// <summary>
- /// Bitmap to BitmapImage
- /// </summary>
- /// <param name="bitmap"></param>
- /// <returns></returns>
- public static BitmapImage BitmapToBitmapImage(Bitmap bitmap)
- {
- return BitmapToBitmapImage(bitmap, null);
- }
- public static BitmapImage BitmapToBitmapImage(Image bitmap, ImageFormat format)
- {
- BitmapImage bitmapImage = new BitmapImage();
- using (MemoryStream ms = new MemoryStream())
- {
- if (format == null)
- {
- bitmap.Save(ms, bitmap.RawFormat);
- }
- else
- {
- bitmap.Save(ms, format);
- }
- bitmapImage.BeginInit();
- bitmapImage.StreamSource = ms;
- bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
- bitmapImage.EndInit();
- bitmapImage.Freeze();
- }
- return bitmapImage;
- }
- [System.Runtime.InteropServices.DllImport("gdi32.dll")]
- public static extern bool DeleteObject(IntPtr hObject);
- public static BitmapImage Bitmap2BitmapImage(Bitmap bitmap)
- {
- IntPtr hBitmap = bitmap.GetHbitmap();
- BitmapImage retval;
- try
- {
- retval = (BitmapImage)Imaging.CreateBitmapSourceFromHBitmap(
- hBitmap,
- IntPtr.Zero,
- Int32Rect.Empty,
- BitmapSizeOptions.FromEmptyOptions());
- }
- finally
- {
- DeleteObject(hBitmap);
- }
- return retval;
- }
- /// <summary>
- /// 图片文件转base64
- /// </summary>
- /// <param name="Imagefilename"></param>
- /// <returns></returns>
- public static string FileImageToBase64(string Imagefilename, ImageFormat format)
- {
- try
- {
- Bitmap bmp = new Bitmap(Imagefilename);
- MemoryStream ms = new MemoryStream();
- bmp.Save(ms, format);
- byte[] arr = new byte[ms.Length];
- ms.Position = 0;
- ms.Read(arr, 0, (int)ms.Length);
- ms.Close();
- return Convert.ToBase64String(arr);
- }
- catch (Exception)
- {
- return null;
- }
- }
- /// <summary>
- /// 判断文件是否为图片
- /// </summary>
- /// <param name="path">文件路径</param>
- /// <returns></returns>
- public static bool IsImage(string path)
- {
- try
- {
- string strExt = Path.GetExtension(path).Substring(1);
- string suffixs = "bmp,jpg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,WMF,webp,avif";
- string[] suffixArr = suffixs.Split(',');
- foreach (string suffix in suffixArr)
- {
- if (suffix.Equals(strExt, StringComparison.InvariantCultureIgnoreCase))
- {
- return true;
- }
- }
- return false;
- }
- catch (Exception)
- {
- return false;
- }
- }
- }
- }
|