ImageManager.cs
上传用户:wedding
上传日期:2022-08-10
资源大小:558k
文件大小:7k
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Drawing.Drawing2D;
- using System.IO;
- using System.Text.RegularExpressions;
- namespace MyImageManger
- {
- class ImageManager
- {
- public static List<string> imgFileList = new List<string>();
- public static int curImg = 0;
- public void PicSized(string picPath, string reSizePicPath, int iSize, ImageFormat format)
- {
- Bitmap originBmp = new Bitmap(picPath);
- int w = originBmp.Width * iSize;
- int h = originBmp.Height * iSize;
- Bitmap resizedBmp = new Bitmap(w, h);
- Graphics g = Graphics.FromImage(resizedBmp);
- //设置高质量插值法
- g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
- //设置高质量,低速度呈现平滑程度
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
- //消除锯齿
- g.SmoothingMode = SmoothingMode.AntiAlias;
- g.DrawImage(originBmp, new Rectangle(0, 0, w, h), new Rectangle(0, 0, originBmp.Width, originBmp.Height), GraphicsUnit.Pixel);
- resizedBmp.Save(reSizePicPath, format);
- g.Dispose();
- resizedBmp.Dispose();
- originBmp.Dispose();
- }
- /// <summary>
- /// Resize图片
- /// </summary>
- /// <param name="bmp">原始Bitmap</param>
- /// <param name="newW">新的宽度</param>
- /// <param name="newH">新的高度</param>
- /// <param name="Mode">保留着,暂时未用</param>
- /// <returns>处理以后的图片</returns>
- public static Bitmap KiResizeImage(Bitmap bmp, int newW, int newH, int Mode)
- {
- try
- {
- Bitmap b = new Bitmap(newW, newH);
- Graphics g = Graphics.FromImage(b);
- // 插值算法的质量
- g.InterpolationMode = InterpolationMode.HighQualityBicubic;
- g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
- g.Dispose();
- return b;
- }
- catch
- {
- return null;
- }
- }
- // ===============================
- /// <summary>
- /// 剪裁 -- 用GDI+
- /// </summary>
- /// <param name="b">原始Bitmap</param>
- /// <param name="StartX">开始坐标X</param>
- /// <param name="StartY">开始坐标Y</param>
- /// <param name="iWidth">宽度</param>
- /// <param name="iHeight">高度</param>
- /// <returns>剪裁后的Bitmap</returns>
- public static Bitmap KiCut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
- {
- if (b == null)
- {
- return null;
- }
- int w = b.Width;
- int h = b.Height;
- if (StartX >= w || StartY >= h)
- {
- return null;
- }
- if (StartX + iWidth > w)
- {
- iWidth = w - StartX;
- }
- if (StartY + iHeight > h)
- {
- iHeight = h - StartY;
- }
- try
- {
- Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
- Graphics g = Graphics.FromImage(bmpOut);
- g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
- g.Dispose();
- return bmpOut;
- }
- catch
- {
- return null;
- }
- }
- /// <summary>
- /// 任意角度旋转
- /// </summary>
- /// <param name="bmp">原始图Bitmap</param>
- /// <param name="angle">旋转角度</param>
- /// <param name="bkColor">背景色</param>
- /// <returns>输出Bitmap</returns>
- public static Bitmap KiRotate(Bitmap bmp, float angle, Color bkColor)
- {
- int w = bmp.Width + 2;
- int h = bmp.Height + 2;
- PixelFormat pf;
- if (bkColor == Color.Transparent)
- {
- pf = PixelFormat.Format32bppArgb;
- }
- else
- {
- pf = bmp.PixelFormat;
- }
- Bitmap tmp = new Bitmap(w, h, pf);
- Graphics g = Graphics.FromImage(tmp);
- g.Clear(bkColor);
- g.DrawImageUnscaled(bmp, 1, 1);
- g.Dispose();
- GraphicsPath path = new GraphicsPath();
- path.AddRectangle(new RectangleF(0f, 0f, w, h));
- Matrix mtrx = new Matrix();
- mtrx.Rotate(angle);
- RectangleF rct = path.GetBounds(mtrx);
- Bitmap dst = new Bitmap((int)rct.Width, (int)rct.Height, pf);
- g = Graphics.FromImage(dst);
- g.Clear(bkColor);
- g.TranslateTransform(-rct.X, -rct.Y);
- g.RotateTransform(angle);
- g.InterpolationMode = InterpolationMode.HighQualityBilinear;
- g.DrawImageUnscaled(tmp, 0, 0);
- g.Dispose();
- tmp.Dispose();
- return dst;
- }
- /// <summary>
- /// 获得文件所在文件夹的所有图片列表,并标记当前的图片位置
- /// </summary>
- /// <param name="filepath">打开文件的地址</param>
- /// <returns></returns>
- public static List<string> GetALLFile(string filepath)
- {
- imgFileList.Clear();
- DirectoryInfo fileFile = new DirectoryInfo(filepath);
- string dicpath = fileFile.Parent.FullName;
- DirectoryInfo dicFile = new DirectoryInfo(dicpath);
- string[] fileNameArray = Directory.GetFiles(dicpath);
- Regex reg = new Regex(@"([A-Z]:\[^/:*?<>|]+.(jpg|JPG|PNG|png|BMP|bmp|ICO|ico)$)");
- for (int i = 0; i < fileNameArray.Length; i++)
- {
- if( reg.Match(fileNameArray[i]).Success)
- {
- imgFileList.Add(fileNameArray[i]);
- if(fileNameArray[i] == filepath)
- {
- curImg = imgFileList.Count - 1;
- }
- }
- }
- return imgFileList;
- }
- }
- }