zoukankan      html  css  js  c++  java
  • .net生成略缩图

    /// <summary>
        /// 生成略缩图
        /// </summary>
        /// <param name="fileName">源文件路径</param>
        /// <param name="newFile">新文件路径</param>
        /// <param name="maxWidth">生成新文件的宽度,高度自动算</param>
        public static void SendSmallImage(string fileName, string newFile, int maxWidth)
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(fileName);
            System.Drawing.Imaging.ImageFormat
            thisFormat = img.RawFormat;
            int maxHeight = img.Height * maxWidth / img.Width;
            Size newSize = NewSize(maxWidth, maxHeight, img.Width, img.Height);
            Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);
            Graphics g = Graphics.FromImage(outBmp);        // 设置画布的描绘质量
            g.DrawImage(img, new System.Drawing.Rectangle(0, 0, newSize.Width, newSize.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
            g.Dispose();
            // 以下代码为保存图片时,设置压缩质量
            EncoderParameters encoderParams = new EncoderParameters();
            long[] quality = new long[1];
            quality[0] = 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 x = 0; x < arrayICI.Length; x++)
            {
                if (arrayICI[x].FormatDescription.Equals("JPEG"))
                {
                    jpegICI = arrayICI[x];
                    //设置JPEG编码
                    break;
                }
            }
            if (jpegICI != null)
            {
                outBmp.Save(newFile, jpegICI, encoderParams);
            }
            else
            {
                outBmp.Save(newFile,
                thisFormat);
            }
            img.Dispose();
            outBmp.Dispose();
        }
  • 相关阅读:
    简述 JPA 与 Spring Data JPA 与 Hibernate
    idea不识别yml配置文件,怎么办?
    Java 数组如何转成List集合
    git关于 LF 与 CRLF
    git报错:src refspec master does not match any
    蓝桥杯- 基础练习:十六进制转八进制
    蓝桥杯- 基础练习:数列排序
    蓝桥杯-入门训练 :A+B问题
    蓝桥杯-入门训练 :序列求和
    蓝桥杯-入门训练 :圆的面积
  • 原文地址:https://www.cnblogs.com/goldnet/p/1515180.html
Copyright © 2011-2022 走看看