zoukankan      html  css  js  c++  java
  • c# 图片按比例缩放

     public static Bitmap GetThumbnail(Bitmap b, int destHeight, int destWidth)   
             {           
                 System.Drawing.Image imgSource = b;      
                 System.Drawing.Imaging.ImageFormat thisFormat = imgSource.RawFormat;  
                 int sW = 0, sH = 0;          
                 // 按比例缩放           
                 int sWidth = imgSource.Width;
                 int sHeight = imgSource.Height;
                 if (sHeight > destHeight || sWidth > destWidth) 
                 {               
                     if ((sWidth * destHeight) > (sHeight * destWidth))    
                     {                 
                         sW = destWidth;    
                         sH = (destWidth * sHeight) / sWidth;  
                     }               
                     else            
                     {           
                         sH = destHeight;    
                         sW = (sWidth * destHeight) / sHeight;    
                     }           
                 }           
                 else         
                 {          
                     sW = sWidth;  
                     sH = sHeight;  
                 }    
                 Bitmap outBmp = new Bitmap(destWidth, destHeight);  
                 Graphics g = Graphics.FromImage(outBmp);      
                 g.Clear(Color.Transparent);         
                 // 设置画布的描绘质量         
                 g.CompositingQuality = CompositingQuality.HighQuality; 
                 g.SmoothingMode = SmoothingMode.HighQuality;       
                 g.InterpolationMode = InterpolationMode.HighQualityBicubic;    
                 g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.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;   
                 imgSource.Dispose();         
                 return outBmp;      
             }
    
  • 相关阅读:
    比较两个json数组是否有相同的选项
    使用gulp实现静态资源版本号替换
    Happy Halloween
    前端学习plan
    Python之函数式编程
    秋意浓
    2018给自己个plan,给自己一个小目标
    see goodbye with 2017
    杂记(一)
    The fruit in mid-summer
  • 原文地址:https://www.cnblogs.com/liuxinls/p/2960310.html
Copyright © 2011-2022 走看看