.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();
    }
原文地址:https://www.cnblogs.com/goldnet/p/1515180.html