IO相關的操作類封裝【檔案的讀取遍歷建立等等】(工具五)

2b勿擾發表於2019-12-03

一:圖片相關操作類

public class ImageHelper
    {
        private static string ImagePath = ConfigurationManager.AppSettings["ImagePath"];
        private static string VerifyPath = ConfigurationManager.AppSettings["ImagePath"];
        //繪圖的原理很簡單:Bitmap就像一張畫布,Graphics如同畫圖的手,把Pen或Brush等繪圖物件畫在Bitmap這張畫布上

        /// <summary>
        /// 畫驗證碼
        /// </summary>
        public static void Drawing()
        {
            Bitmap bitmapobj = new Bitmap(100, 100);
            //在Bitmap上建立一個新的Graphics物件
            Graphics g = Graphics.FromImage(bitmapobj);
            //建立繪畫物件,如Pen,Brush等
            Pen redPen = new Pen(Color.Red, 8);
            g.Clear(Color.White);
            //繪製圖形
            g.DrawLine(redPen, 50, 20, 500, 20);
            g.DrawEllipse(Pens.Black, new Rectangle(0, 0, 200, 100));//畫橢圓
            g.DrawArc(Pens.Black, new Rectangle(0, 0, 100, 100), 60, 180);//畫弧線
            g.DrawLine(Pens.Black, 10, 10, 100, 100);//畫直線
            g.DrawRectangle(Pens.Black, new Rectangle(0, 0, 100, 200));//畫矩形
            g.DrawString("我愛北京天安門", new Font("微軟雅黑", 12), new SolidBrush(Color.Red), new PointF(10, 10));//畫字串
            //g.DrawImage(
            if (!Directory.Exists(ImagePath))
                Directory.CreateDirectory(ImagePath);
            bitmapobj.Save(ImagePath + "pic1.jpg", ImageFormat.Jpeg);
            //釋放所有物件
            bitmapobj.Dispose();
            g.Dispose();
        }

        public static void VerificationCode()
        {
            Bitmap bitmapobj = new Bitmap(300, 300);
            //在Bitmap上建立一個新的Graphics物件
            Graphics g = Graphics.FromImage(bitmapobj);
            g.DrawRectangle(Pens.Black, new Rectangle(0, 0, 150, 50));//畫矩形
            g.FillRectangle(Brushes.White, new Rectangle(1, 1, 149, 49));
            g.DrawArc(Pens.Blue, new Rectangle(10, 10, 140, 10), 150, 90);//干擾線
            string[] arrStr = new string[] { "我", "們", "孝", "行", "白", "到", "國", "中", "來", "真" };
            Random r = new Random();
            int i;
            for (int j = 0; j < 4; j++)
            {
                i = r.Next(10);
                g.DrawString(arrStr[i], new Font("微軟雅黑", 15), Brushes.Red, new PointF(j * 30, 10));
            }
            bitmapobj.Save(Path.Combine(VerifyPath, "Verif.jpg"), ImageFormat.Jpeg);
            bitmapobj.Dispose();
            g.Dispose();
        }

        /// <summary>
        /// 按比例縮放,圖片不會變形,會優先滿足原圖和最大長寬比例最高的一項
        /// </summary>
        /// <param name="oldPath"></param>
        /// <param name="newPath"></param>
        /// <param name="maxWidth"></param>
        /// <param name="maxHeight"></param>
        public static void CompressPercent(string oldPath, string newPath, int maxWidth, int maxHeight)
        {
            Image _sourceImg = Image.FromFile(oldPath);
            double _newW = (double)maxWidth;
            double _newH = (double)maxHeight;
            double percentWidth = (double)_sourceImg.Width > maxWidth ? (double)maxWidth : (double)_sourceImg.Width;

            if ((double)_sourceImg.Height * (double)percentWidth / (double)_sourceImg.Width > (double)maxHeight)
            {
                _newH = (double)maxHeight;
                _newW = (double)maxHeight / (double)_sourceImg.Height * (double)_sourceImg.Width;
            }
            else
            {
                _newW = percentWidth;
                _newH = (percentWidth / (double)_sourceImg.Width) * (double)_sourceImg.Height;
            }
            Image bitmap = new Bitmap((int)_newW, (int)_newH);
            Graphics g = Graphics.FromImage(bitmap);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.Clear(Color.Transparent);
            g.DrawImage(_sourceImg, new Rectangle(0, 0, (int)_newW, (int)_newH), new Rectangle(0, 0, _sourceImg.Width, _sourceImg.Height), GraphicsUnit.Pixel);
            _sourceImg.Dispose();
            g.Dispose();
            bitmap.Save(newPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            bitmap.Dispose();
        }

        /// <summary>
        /// 按照指定大小對圖片進行縮放,可能會圖片變形
        /// </summary>
        /// <param name="oldPath"></param>
        /// <param name="newPath"></param>
        /// <param name="newWidth"></param>
        /// <param name="newHeight"></param>
        public static void ImageChangeBySize(string oldPath, string newPath, int newWidth, int newHeight)
        {
            Image sourceImg = Image.FromFile(oldPath);
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(newWidth, newHeight);
            Graphics g = Graphics.FromImage(bitmap);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.Clear(Color.Transparent);
            g.DrawImage(sourceImg, new Rectangle(0, 0, newWidth, newHeight), new Rectangle(0, 0, sourceImg.Width, sourceImg.Height), GraphicsUnit.Pixel);
            sourceImg.Dispose();
            g.Dispose();
            bitmap.Save(newPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            bitmap.Dispose();
        }
        //新增水印
        //二維碼 QRCode  二維碼其實對應的是一個Url

相關文章