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();
        }
  • 相关阅读:
    别再乱升级 MySQL 驱动了。。
    Spring Boot + MyBatis + MySQL 实现读写分离
    多线程环境下,HashMap 为什么会出现死循环?
    亿级流量架构怎么做资源隔离?写得太好了!
    refdeveloptools for developers
    how to setup ppc2003 or smartphone 2003 to connect to internet
    转载:一篇java与C#的对比文章(英文)
    在sqlexpress中添加DB和在sql analyzer中操作DB.
    windows 2003下配置IIS6为iis5方式的隔离模式运行
    开源的pop3和smtp组件(支持中文及SSL)
  • 原文地址:https://www.cnblogs.com/goldnet/p/1515180.html
Copyright © 2011-2022 走看看