• ImageHelper


        public class ImagesHelper
        {
            /// <summary>
            /// 缩略图1:生成等比例高清缩略图,图片比较大
            /// </summary>
            /// <param name="phyPath">原图片的路径</param>
            /// <param name="width">缩略图宽</param>
            /// <param name="height">缩略图高</param>
            /// <returns></returns>
            public static System.Drawing.Image GetThumbnailImage(System.Drawing.Image image, int width, int height)
            {
                Bitmap bmp = new Bitmap(width, height);
                //从Bitmap创建一个System.Drawing.Graphics
                System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
                //设置 
                gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //设成高质量
                gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                //设成High
                gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                //把原始图像绘制成上面所设置宽高的缩小图
                System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, width, height);
    
                gr.DrawImage(image, rectDestination, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
                return bmp;
            }
    
            /// <summary>
            /// 缩略图2:得到指定像素缩略图,图片比较小
            /// </summary>
            /// <param name="Image"></param>
            /// <param name="newFile"></param>
            /// <param name="maxHeight"></param>
            /// <param name="maxWidth"></param>
            /// <param name="qualityNum">图片质量1-100</param>
            /// <param name="mode">缩放模式,CUT裁剪不失真,HW定宽高有可能变形,W定宽,H定高</param>
            public static void GetSmallImage(Image img, string newFile, int maxHeight, int maxWidth, long qualityNum, string mode)
            {
                System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
                int towidth = maxWidth;
                int toheight = maxHeight;
                int x = 0;
                int y = 0;
                int ow = img.Width;
                int oh = img.Height;
                switch (mode)
                {
                    case "HW"://指定高宽缩放(可能变形)                
                        break;
                    case "W"://指定宽,高按比例                    
                        toheight = img.Height * maxWidth / img.Width;
                        break;
                    case "H"://指定高,宽按比例
                        towidth = img.Width * maxHeight / img.Height;
                        break;
                    case "CUT"://指定高宽裁减(不变形)                
                        if ((double)img.Width / (double)img.Height > (double)towidth / (double)toheight)
                        {
                            oh = img.Height;
                            ow = img.Height * towidth / toheight;
                            y = 0;
                            x = (img.Width - ow) / 2;
                        }
                        else
                        {
                            ow = img.Width;
                            oh = img.Width * maxHeight / towidth;
                            x = 0;
                            y = (img.Height - oh) / 2;
                        }
                        break;
                    default:
                        break;
                }
                Bitmap outBmp = new Bitmap(towidth, toheight);
                Graphics g = Graphics.FromImage(outBmp);
                // 设置画布的描绘质量
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(img, new Rectangle(0, 0, towidth, toheight), x, y, ow, oh, GraphicsUnit.Pixel);
                g.Dispose();
                // 以下代码为保存图片时,设置压缩质量
                EncoderParameters encoderParams = new EncoderParameters();
                long[] quality = new long[1];
                quality[0] = qualityNum;//图片质量1-100
                EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
                encoderParams.Param[0] = encoderParam;
                //获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象.
                ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo jpegICI = null;
                for (int index = 0; index < arrayICI.Length; index++)
                {
                    if (arrayICI[index].FormatDescription.Equals("JPEG"))
                    {
                        jpegICI = arrayICI[index];
                        //设置JPEG编码
                        break;
                    }
                }
                if (jpegICI != null)
                {
                    outBmp.Save(newFile, jpegICI, encoderParams);
                }
                else
                {
                    outBmp.Save(newFile, thisFormat);
                }
                img.Dispose();
                outBmp.Dispose();
            }
    
            #region 图片旋转函数
    
            /// <summary>
            /// 以逆时针或顺时针方向对图像进行旋转
            /// </summary>
            /// <param name="imgFilePath">图片的物理路基</param>
            /// <param name="angle">1-360刻度</param>
            /// <returns></returns>
            public static Bitmap Rotate(string imgFilePath, int angle)
            {
                var b = new Bitmap(imgFilePath);
                return Rotate(b, angle, imgFilePath);
            }
    
            /// <summary>
            /// 图片向左旋转90度
            /// </summary>
            /// <param name="imgFilePath">图片物理路径</param>
            /// <param name="angle">旋转度数</param>
            /// <returns>成功返回true,失败返回false</returns>
            public static bool RotateLeft(string imgFilePath, int angle = 90)
            {
                try
                {
                    Rotate(imgFilePath, angle);
                }
                catch (Exception)
                {
                    return false;
                }
                return true;
            }
    
            /// <summary>
            /// 图片向右旋转90度
            /// </summary>
            /// <param name="imgFilePath">图片物理路径</param>
            /// <param name="angle">旋转度数</param>
            /// <returns>成功返回true,失败返回false</returns>
            public static bool RotateRight(string imgFilePath, int angle = 270)
            {
                try
                {
                    Rotate(imgFilePath, angle);
                }
                catch (Exception)
                {
                    return false;
                }
                return true;
            }
    
            /// <summary>
            /// 以逆时针或顺时针方向对图像进行旋转
            /// </summary>
            /// <param name="bitmap">Bitmap图片类型</param>
            /// <param name="angle">1-360刻度</param>
            /// <param name="imgFilePath">保存图片路径,可选项</param>
            /// <returns></returns>
            private static Bitmap Rotate(Image bitmap, int angle, string imgFilePath = "")
            {
                angle = angle > 0 ? angle : angle + 360;
                angle = angle % 360;            //弧度转换
                var radian = angle * Math.PI / 180.0;
                var cos = Math.Cos(radian);
                var sin = Math.Sin(radian);
                //原图的宽和高
                var w = bitmap.Width;
                var h = bitmap.Height;
                var W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
                var H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));
                //目标位图
                var dsImage = new Bitmap(W, H);
                var g = Graphics.FromImage(dsImage);
                g.InterpolationMode = InterpolationMode.Bilinear;
                g.SmoothingMode = SmoothingMode.HighQuality;
                //计算偏移量
                var offset = new Point((W - w) / 2, (H - h) / 2);
                //构造图像显示区域:让图像的中心与窗口的中心点一致
                var rect = new Rectangle(offset.X, offset.Y, w, h);
                var center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
                g.TranslateTransform(center.X, center.Y);
                g.RotateTransform(360 - angle);
                //恢复图像在水平和垂直方向的平移
                g.TranslateTransform(-center.X, -center.Y);
                g.DrawImage(bitmap, rect);
                //重至绘图的所有变换
                g.ResetTransform();
                g.Save();
                g.Dispose();
                bitmap.Dispose();
                if (imgFilePath != "")
                    dsImage.Save(imgFilePath);
                return dsImage;
            }
            #endregion 图片旋转函数
    
    
            /// <summary>
            /// 向图片中添加水印
            /// </summary>
            /// <param name="image">上传图片对象</param>
            /// <param name="waterFilePath">水印图片物理路径</param>
            public static Image AddWater(Image image, string waterFilePath)
            {
                if (image == null) return null;
                var waterImage = Image.FromFile(waterFilePath, false);
                if (image.Width <= waterImage.Width)
                {
                    return image;
                }
                //Imaging.Encoder.Quality 默认为60 指定的数字越小,压缩的级别就越高 最大为100L 质量最佳
                var g = Graphics.FromImage(image);
                //设置高质量插值法
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                //设置高质量,低速度呈现平滑程度
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.DrawImage(waterImage, new Rectangle((image.Width - waterImage.Width) - 10, (image.Height - waterImage.Height) - 10, waterImage.Width, waterImage.Height), 0, 0, waterImage.Width, waterImage.Height, GraphicsUnit.Pixel);
                g.Dispose();
                return image;
            }
        }
  • 相关阅读:
    python
    python
    python
    python
    python
    python-接口自动化 token 的处理
    如何顺利度过试用期?
    印象深刻-bug汇总
    go 实现1000以内的数字,输入35 输出三十五
    jenkins 汉化
  • 原文地址:https://www.cnblogs.com/lyboke/p/14837844.html
Copyright © 2020-2023  润新知