123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace BellwetherBackend.Utility
- {
- public class ReduceImageResolution
- {
- private static ImageCodecInfo imageCodecInfo;
- private static System.Drawing.Imaging.Encoder myEncoder;
- private static EncoderParameter myEncoderParameter;
- private static EncoderParameters myEncoderParameters;
- //所要縮圖的目標寬高~
- private static int TargetWidth = 950;
- private static int TargetHeight = 620;
- private static int TransRateWidth = 0;
- private static int TransRateHeight = 0;
- private static float GetImageResolutionHeight(Bitmap bit)
- {
- return bit.HorizontalResolution;
- }
- private static float GetImageResolutionWidth(Bitmap bit)
- {
- return bit.VerticalResolution;
- }
- private static long GetImageDepth(Bitmap bit)
- {
- long result = 0;
- switch (bit.PixelFormat)
- {
- case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
- {
- result = 8;
- break;
- }
- case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
- {
- result = 24;
- break;
- }
- case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
- case System.Drawing.Imaging.PixelFormat.Format32bppPArgb:
- {
- result = 32;
- break;
- }
- }
- return result;
- }
- private static ImageCodecInfo GetEncoderInfo(String mimeType)
- {
- int j;
- ImageCodecInfo[] encoders;
- encoders = ImageCodecInfo.GetImageEncoders();
- for (j = 0; j < encoders.Length; ++j)
- {
- if (encoders[j].MimeType == mimeType)
- {
- return encoders[j];
- }
- }
- return null;
- }
- private static ImageCodecInfo GetImageCodec(string path)
- {
- string str = "jpeg";
- switch (System.IO.Path.GetExtension(path).ToLower())
- {
- case ".bmp":
- {
- str = "bmp";
- break;
- }
- case ".gif":
- {
- str = "gif";
- break;
- }
- case ".tiff":
- {
- str = "tiff";
- break;
- }
- case ".png":
- {
- str = "png";
- break;
- }
- }
- return GetEncoderInfo("image/" + str);
- }
- private void CheckMediaResolution(string fileFrom, string fileTo)
- {
- File.Copy(fileFrom, fileTo);
- }
- public static void CheckImageResolution(string fileFrom, string fileTo, int resolutionWidth, int resolutionHeight)
- {
- TargetWidth = resolutionWidth;
- TargetHeight = resolutionHeight;
- myEncoder = System.Drawing.Imaging.Encoder.ColorDepth;
- myEncoderParameters = new EncoderParameters(1);
- try
- {
- if (File.Exists(fileFrom))
- {
- Bitmap bit = new Bitmap(fileFrom);
- //先將原本圖片的影像深度及 Dpi 存起來
- float xDpi = GetImageResolutionWidth(bit);
- float yDpi = GetImageResolutionHeight(bit);
- long depth = GetImageDepth(bit);
- imageCodecInfo = GetImageCodec(fileFrom);
- TransRateWidth = bit.Width;
- TransRateHeight = bit.Height;
- if (bit.Width >= bit.Height)
- {
- if (TransRateWidth > TargetWidth || TransRateHeight > TargetHeight)
- {
- GetTargetSize(bit);
- }
- }
- else
- {
- if (TransRateWidth > TargetHeight || TransRateHeight > TargetWidth)
- {
- GetTargetSize(bit);
- }
- }
- //取得新的 bitmap
- bit = KiResizeImage(bit, TransRateWidth, TransRateHeight);
- //依照原本的影像深度及DPI來重新設定新圖示的影像深度 及 DPI
- bit.SetResolution(xDpi, yDpi);
- myEncoderParameter = new EncoderParameter(myEncoder, depth);
- myEncoderParameters.Param[0] = myEncoderParameter;
- //產生圖片
- bit.Save(fileTo, imageCodecInfo, myEncoderParameters);
- bit.Dispose();
- }
- }
- catch (Exception)
- {
- }
- }
- private static void GetTargetSize(Bitmap bmp)
- {
- //必須考慮兩種狀況 (橫或者直)
- if (bmp.Width >= bmp.Height)
- {
- TransRateWidth = TargetWidth;
- TransRateHeight = TargetHeight;
- //橫
- //第二考慮 : 往哪個方向縮
- if (bmp.Width >= TransRateWidth)
- {
- decimal result = Math.Round((decimal)TransRateWidth / bmp.Width, 5);
- //寬比高大~代表為橫式~縮圖為 : 設定的寬, 設定的高 / 寬的縮圖比例
- TransRateHeight = (int)(bmp.Height * result);
- }
- else if (bmp.Height >= TransRateHeight)
- {
- decimal result = Math.Round((decimal)TransRateHeight / bmp.Height, 5);
- //高比寬大~代表為直式~縮圖為 : 設定的寬 / 高的縮圖比例, 設定的高
- TransRateWidth = (int)(bmp.Width * result);
- }
- }
- else if (bmp.Height > bmp.Width)
- {
- TransRateWidth = TargetHeight;
- TransRateHeight = TargetWidth;
- //直
- //第二考慮 : 往哪個方向縮
- if (bmp.Width >= TransRateWidth)
- {
- decimal result = Math.Round((decimal)TransRateWidth / bmp.Width, 5);
- //寬比高大~代表為橫式~縮圖為 : 設定的寬, 設定的高 / 寬的縮圖比例
- TransRateHeight = (int)(bmp.Height * result);
- }
- else if (bmp.Height >= TransRateHeight)
- {
- decimal result = Math.Round((decimal)TransRateHeight / bmp.Height, 5);
- //高比寬大~代表為直式~縮圖為 : 設定的寬 / 高的縮圖比例, 設定的高
- TransRateWidth = (int)(bmp.Width * result);
- }
- }
- }
- public static Bitmap KiResizeImage(Bitmap bmp, int newW, int newH)
- {
- try
- {
- Bitmap b = new Bitmap(newW, newH);
- Graphics g = Graphics.FromImage(b);
- // 插值算法的質量
- g.InterpolationMode = InterpolationMode.HighQualityBicubic;
- g.CompositingMode = CompositingMode.SourceCopy;
- g.Clear(Color.Transparent);
- g.DrawImage(bmp, new System.Drawing.Rectangle(0, 0, newW, newH), new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
- g.Dispose();
- bmp.Dispose();
- bmp = null;
- return b;
- }
- catch
- {
- return null;
- }
- }
- }
- }
|