C#高质量缩略图

None.gif private   static  Size NewSize(  int  maxWidth, 
None.gif
int  maxHeight,  int  width, 
None.gif
int  height )  
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
double w = 0.0;
InBlock.gif    
double h = 0.0;
InBlock.gif    
double sw = Convert.ToDouble( width );
InBlock.gif    
double sh = Convert.ToDouble( height ); double mw = Convert.ToDouble( maxWidth );
InBlock.gif    
double mh = Convert.ToDouble( maxHeight ); if (sw < mw && sh < mh)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        w 
= sw;
InBlock.gif        h 
= sh;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else if (( sw/sh ) > ( mw/mh ))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        w 
= maxWidth;
InBlock.gif        h 
= ( w * sh )/sw;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        h 
= maxHeight;
InBlock.gif        w 
= ( h * sw )/sh;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return new Size( Convert.ToInt32( w ), Convert.ToInt32( h ) );
ExpandedBlockEnd.gif}

None.gif
None.gif
public   static   void  SendSmallImage(  string  fileName, 
None.gif
string  newFile, 
None.gif
int  maxHeight, 
None.gif
int  maxWidth )  
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    System.Drawing.Image img 
= System.Drawing.Image.FromFile( fileName );
InBlock.gif    System.Drawing.Imaging.ImageFormat 
InBlock.gif    thisFormat 
= img.RawFormat;
InBlock.gif    Size newSize 
= NewSize( maxWidth, maxHeight, img.Width, img.Height );
InBlock.gif    Bitmap outBmp 
= new Bitmap( newSize.Width, newSize.Height );
InBlock.gif    Graphics g 
= Graphics.FromImage( outBmp );
InBlock.gif    
// 设置画布的描绘质量
InBlock.gif
    g.CompositingQuality = CompositingQuality.HighQuality;
InBlock.gif    g.SmoothingMode 
= SmoothingMode.HighQuality;
InBlock.gif    g.InterpolationMode 
= InterpolationMode.HighQualityBicubic;
InBlock.gif    g.DrawImage( img, 
new Rectangle( 00, newSize.Width, newSize.Height ),
InBlock.gif    
00, img.Width, img.Height, GraphicsUnit.Pixel );
InBlock.gif    g.Dispose( );
InBlock.gif    
// 以下代码为保存图片时,设置压缩质量
InBlock.gif
    EncoderParameters encoderParams = new EncoderParameters( );
InBlock.gif    
long[] quality = new long[1];
InBlock.gif    quality[
0= 100;
InBlock.gif    EncoderParameter encoderParam 
= new EncoderParameter( System.Drawing.Imaging.Encoder.Quality, quality );
InBlock.gif    encoderParams.Param[
0= encoderParam;
InBlock.gif    
//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象.
InBlock.gif
    ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders( );
InBlock.gif    ImageCodecInfo jpegICI 
= null;
InBlock.gif    
for ( int x = 0;
InBlock.gif    x 
< arrayICI.Length;
InBlock.gif    x
++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if ( arrayICI[x].FormatDescription.Equals( "JPEG" ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            jpegICI 
= arrayICI[x];
InBlock.gif            
//设置JPEG编码
InBlock.gif
            break;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
if ( jpegICI != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        outBmp.Save( newFile, jpegICI, encoderParams );
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        outBmp.Save( newFile, 
InBlock.gif        thisFormat );
ExpandedSubBlockEnd.gif    }

InBlock.gif    img.Dispose( );
InBlock.gif    outBmp.Dispose( );
ExpandedBlockEnd.gif}
 
None.gif

转载于:https://www.cnblogs.com/suchenge/articles/889352.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值