C#实战小技巧(六):生成缩略图

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/WZh0316/article/details/82019104

在C#开发中,经常会遇到为某张图片生成缩略图的需求,本文提供一个能够生成缩略图的C#函数,支持将bmp、png、jpg等常见格式的静态图片压缩,生成缩略图,可以避免png图片丢失透明度。不过该函数的图片压缩方法比较简单,只能压缩20M以内的图片,图片大于20M将出现内存错误,有待改进。

        /// <summary>
        /// 压缩图片并保存,文件名为compress_+原文件名
        /// </summary>
        /// <param name="filePath">原图路径</param>
        /// <returns>缩略图路径</returns>
        private string GetReducedImage(string filePath)
        {
            string fileDir = new StringBuilder(AppDomain.CurrentDomain.BaseDirectory).Append(ICTResources.ICTUser.Session.useraccount).Append("\\Image").ToString();
            if (!Directory.Exists(fileDir))
            {
                Directory.CreateDirectory(fileDir);
            }

            string compressName = new StringBuilder(fileDir).Append("\\compress_").Append(Path.GetFileName(filePath)).ToString();

            int minSize = 200;

            Graphics draw = null;
            System.Drawing.Image ResourceImage = System.Drawing.Image.FromFile(filePath);
            double percent = 0.4;
            int imageWidth = Convert.ToInt32(ResourceImage.Width);
            int imageHeight = Convert.ToInt32(ResourceImage.Height);
            if (imageWidth > imageHeight)
            {
                if (imageWidth > minSize)
                {
                    percent = Convert.ToDouble(minSize) / imageWidth;
                    imageWidth = minSize;
                    imageHeight = (int)(imageHeight * percent);
                }
            }
            else
            {
                if (imageHeight > minSize)
                {
                    percent = Convert.ToDouble(minSize) / imageHeight;
                    imageHeight = minSize;
                    imageWidth = (int)(imageWidth * percent);
                }
            }

            // 新建一个bmp图片
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(imageWidth, imageHeight);
            System.Drawing.Image bitmap2 = new System.Drawing.Bitmap(imageWidth, imageHeight);
            // 新建一个画板
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            try
            {
                // 设置高质量插值法
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                // 设置高质量,低速度呈现平滑程度
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                // 清空画布并以透明背景色(白色)填充
                g.Clear(System.Drawing.Color.White);
                //g.Clear(System.Drawing.Color.Transparent);
                // 在指定位置并且按指定大小绘制原图片的指定部分
                g.DrawImage(ResourceImage, new System.Drawing.Rectangle(0, 0, imageWidth, imageHeight));

                // 用新建立的image对象拷贝bitmap对象 让g对象可以释放资源
                draw = Graphics.FromImage(bitmap2);
                draw.DrawImage(bitmap, 0, 0);

                // 设置缩略图编码格式
                ImageCodecInfo ici = null;
                if (Path.GetExtension(filePath).Equals(".png") || Path.GetExtension(filePath).Equals(".PNG"))
                {
                    //ici = this.getImageCoderInfo("image/png");
                    ici = this.getImageCoderInfo("image/jpeg");
                }
                else if (Path.GetExtension(filePath).Equals(".gif") || Path.GetExtension(filePath).Equals(".GIF"))
                {
                    ici = this.getImageCoderInfo("image/gif");
                }
                else
                {
                    ici = this.getImageCoderInfo("image/jpeg");
                }

                // 设置压缩率
                long ratio = 20L; // 压缩为原图20%的质量

                System.Drawing.Imaging.Encoder ecd = System.Drawing.Imaging.Encoder.Quality;

                ResourceImage.Dispose();
                bitmap.Dispose();
                g.Dispose();
                draw.Dispose();

                // 保存调整在这里即可
                using (EncoderParameters eptS = new EncoderParameters(1))
                {
                    using (EncoderParameter ept = new EncoderParameter(ecd, ratio)) 
                    {
                        eptS.Param[0] = ept;
                        bitmap2.Save(compressName, ici, eptS);
                    }
                }
            }
            catch (System.Exception e)
            {
                compressName = string.Empty;
            }
            finally
            {
                if (ResourceImage != null)
                {
                    ResourceImage.Dispose();
                }

                if (bitmap != null)
                {
                    bitmap.Dispose();
                }

                if (g != null)
                {
                    g.Dispose();
                }

                if (bitmap2 != null)
                {
                    bitmap2.Dispose();
                }

                if (draw != null)
                {
                    draw.Dispose();
                }
            }

            return compressName;
        }

        /// <summary>
        /// 获取图片编码
        /// </summary>
        /// <param name="coderType">编码格式:image/png、image/jpeg等</param>
        /// <returns></returns>
        private ImageCodecInfo getImageCoderInfo(string coderType)
        {
            ImageCodecInfo[] iciS = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo retIci = null;
            foreach (ImageCodecInfo ici in iciS)
            {
                if (ici.MimeType.Equals(coderType))
                {
                    retIci = ici;
                }
            }

            return retIci;
        }

猜你喜欢

转载自blog.csdn.net/WZh0316/article/details/82019104
今日推荐