ImageManager.cs
上传用户:wedding
上传日期:2022-08-10
资源大小:558k
文件大小:7k
源码类别:

图形图像处理

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.Drawing.Drawing2D;
  7. using System.IO;
  8. using System.Text.RegularExpressions;
  9. namespace MyImageManger
  10. {
  11.     class ImageManager
  12.     {
  13.         public static List<string> imgFileList = new List<string>();
  14.         public static int curImg = 0;
  15.         public void PicSized(string picPath, string reSizePicPath, int iSize, ImageFormat format)
  16.         {
  17.             Bitmap originBmp = new Bitmap(picPath);
  18.             int w = originBmp.Width * iSize;
  19.             int h = originBmp.Height * iSize;
  20.             Bitmap resizedBmp = new Bitmap(w, h);
  21.             Graphics g = Graphics.FromImage(resizedBmp);
  22.             //设置高质量插值法   
  23.             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  24.             //设置高质量,低速度呈现平滑程度   
  25.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  26.             g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  27.             //消除锯齿 
  28.             g.SmoothingMode = SmoothingMode.AntiAlias;
  29.             g.DrawImage(originBmp, new Rectangle(0, 0, w, h), new Rectangle(0, 0, originBmp.Width, originBmp.Height), GraphicsUnit.Pixel);
  30.             resizedBmp.Save(reSizePicPath, format);
  31.             g.Dispose();
  32.             resizedBmp.Dispose();
  33.             originBmp.Dispose();
  34.         }
  35.         /// <summary>
  36.         /// Resize图片
  37.         /// </summary>
  38.         /// <param name="bmp">原始Bitmap</param>
  39.         /// <param name="newW">新的宽度</param>
  40.         /// <param name="newH">新的高度</param>
  41.         /// <param name="Mode">保留着,暂时未用</param>
  42.         /// <returns>处理以后的图片</returns>
  43.         public static Bitmap KiResizeImage(Bitmap bmp, int newW, int newH, int Mode)
  44.         {
  45.             try
  46.             {
  47.                 Bitmap b = new Bitmap(newW, newH);
  48.                 Graphics g = Graphics.FromImage(b);
  49.                 // 插值算法的质量
  50.                 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  51.                 g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
  52.                 g.Dispose();
  53.                 return b;
  54.             }
  55.             catch
  56.             {
  57.                 return null;
  58.             }
  59.         }
  60.         // ===============================
  61.         /// <summary>
  62.         /// 剪裁 -- 用GDI+
  63.         /// </summary>
  64.         /// <param name="b">原始Bitmap</param>
  65.         /// <param name="StartX">开始坐标X</param>
  66.         /// <param name="StartY">开始坐标Y</param>
  67.         /// <param name="iWidth">宽度</param>
  68.         /// <param name="iHeight">高度</param>
  69.         /// <returns>剪裁后的Bitmap</returns>
  70.         public static Bitmap KiCut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
  71.         {
  72.             if (b == null)
  73.             {
  74.                 return null;
  75.             }
  76.             int w = b.Width;
  77.             int h = b.Height;
  78.             if (StartX >= w || StartY >= h)
  79.             {
  80.                 return null;
  81.             }
  82.             if (StartX + iWidth > w)
  83.             {
  84.                 iWidth = w - StartX;
  85.             }
  86.             if (StartY + iHeight > h)
  87.             {
  88.                 iHeight = h - StartY;
  89.             }
  90.             try
  91.             {
  92.                 Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
  93.                 Graphics g = Graphics.FromImage(bmpOut);
  94.                 g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
  95.                 g.Dispose();
  96.                 return bmpOut;
  97.             }
  98.             catch
  99.             {
  100.                 return null;
  101.             }
  102.         }
  103.                     /// <summary>
  104.         /// 任意角度旋转
  105.         /// </summary>
  106.         /// <param name="bmp">原始图Bitmap</param>
  107.         /// <param name="angle">旋转角度</param>
  108.         /// <param name="bkColor">背景色</param>
  109.         /// <returns>输出Bitmap</returns>
  110.         public static Bitmap KiRotate(Bitmap bmp, float angle, Color bkColor)
  111.         {
  112.             int w = bmp.Width + 2;
  113.             int h = bmp.Height + 2;
  114.              PixelFormat pf;
  115.             if (bkColor == Color.Transparent)
  116.             {
  117.                  pf = PixelFormat.Format32bppArgb;
  118.              }
  119.             else
  120.             {
  121.                  pf = bmp.PixelFormat;
  122.             }
  123.              Bitmap tmp = new Bitmap(w, h, pf);
  124.              Graphics g = Graphics.FromImage(tmp);
  125.              g.Clear(bkColor);
  126.              g.DrawImageUnscaled(bmp, 1, 1);
  127.              g.Dispose();
  128.              GraphicsPath path = new GraphicsPath();
  129.              path.AddRectangle(new RectangleF(0f, 0f, w, h));
  130.              Matrix mtrx = new Matrix();
  131.              mtrx.Rotate(angle);
  132.              RectangleF rct = path.GetBounds(mtrx);
  133.              Bitmap dst = new Bitmap((int)rct.Width, (int)rct.Height, pf);
  134.              g = Graphics.FromImage(dst);
  135.              g.Clear(bkColor);
  136.              g.TranslateTransform(-rct.X, -rct.Y);
  137.              g.RotateTransform(angle);
  138.              g.InterpolationMode = InterpolationMode.HighQualityBilinear;
  139.              g.DrawImageUnscaled(tmp, 0, 0);
  140.              g.Dispose();
  141.              tmp.Dispose();
  142.             return dst;
  143.          }
  144.         /// <summary>
  145.         /// 获得文件所在文件夹的所有图片列表,并标记当前的图片位置
  146.         /// </summary>
  147.         /// <param name="filepath">打开文件的地址</param>
  148.         /// <returns></returns>
  149.         public static List<string> GetALLFile(string filepath)
  150.         {
  151.             imgFileList.Clear();
  152.             DirectoryInfo fileFile = new DirectoryInfo(filepath);
  153.             string dicpath = fileFile.Parent.FullName;
  154.             DirectoryInfo dicFile = new DirectoryInfo(dicpath);
  155.             string[] fileNameArray =  Directory.GetFiles(dicpath);
  156.             Regex reg = new Regex(@"([A-Z]:\[^/:*?<>|]+.(jpg|JPG|PNG|png|BMP|bmp|ICO|ico)$)");
  157.             for (int i = 0; i < fileNameArray.Length; i++)
  158.             {
  159.                 if( reg.Match(fileNameArray[i]).Success)
  160.                 {
  161.                     imgFileList.Add(fileNameArray[i]);
  162.                     if(fileNameArray[i] == filepath)
  163.                     {
  164.                         curImg = imgFileList.Count - 1;
  165.                     }
  166.                 }
  167.             } 
  168.             return imgFileList;
  169.         }
  170.     }
  171. }