zoukankan      html  css  js  c++  java
  • C#高质量缩略图

    private static Size NewSize( int maxWidth,
    int maxHeight, int width,
    int height )  
    {
        double w = 0.0;
        double h = 0.0;
        double sw = Convert.ToDouble( width );
        double sh = Convert.ToDouble( height ); double mw = Convert.ToDouble( maxWidth );
        double mh = Convert.ToDouble( maxHeight ); if (sw < mw && sh < mh)
        {
            w = sw;
            h = sh;
        }
        else if (( sw/sh ) > ( mw/mh ))
        {
            w = maxWidth;
            h = ( w * sh )/sw;
        }
        else
        {
            h = maxHeight;
            w = ( h * sw )/sh;
        }
        return new Size( Convert.ToInt32( w ), Convert.ToInt32( h ) );
    }

    public static void SendSmallImage( string fileName,
    string newFile,
    int maxHeight,
    int maxWidth )  
    {
        System.Drawing.Image img = System.Drawing.Image.FromFile( fileName );
        System.Drawing.Imaging.ImageFormat
        thisFormat = img.RawFormat;
        Size newSize = NewSize( maxWidth, maxHeight, img.Width, img.Height );
        Bitmap outBmp = new Bitmap( newSize.Width, newSize.Height );
        Graphics g = Graphics.FromImage( outBmp );
        // 设置画布的描绘质量
        g.CompositingQuality = CompositingQuality.HighQuality;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.DrawImage( img, new 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( );
    }


       本人博客的文章大部分来自网络转载,因为时间的关系,没有写明转载出处和作者。所以在些郑重的说明:文章只限交流,版权归作者。谢谢

  • 相关阅读:
    Redis是可以安装成windows服务-开机自启 win7 64位
    lnmp 一键安装包 nginx配置tp5 phpinfo模式 隐藏index.php
    Redis PHP通用类
    Swoole2.0协程客户端连接池的实现
    Tomcat 部署项目无法加载静态资源
    IntelliJ IDEA 的下载和安装
    Redis 配置内容总结
    Eclipse 中 Spring 项目的 XML 配置文件报错 Referenced file contains errors
    Eclipse 中 Maven 项目 pom.xml 提示错误 org.codehaus.plexus.archiver.jar.Manifest.write(java.io.PrintWriter)
    文本编辑器 EditPlus 的激活与设置
  • 原文地址:https://www.cnblogs.com/wzg0319/p/1543050.html
Copyright © 2011-2022 走看看