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

    1. 等比例缩放图片(C#)

    2. private Bitmap ZoomImage(Bitmap bitmap, int destHeight, int destWidth)  
    3. {  
    4.     try  
    5.     {  
    6.         System.Drawing.Image sourImage = bitmap;  
    7.         int width = 0, height = 0;  
    8.         //按比例缩放             
    9.         int sourWidth = sourImage.Width;  
    10.         int sourHeight = sourImage.Height;  
    11.         if (sourHeight > destHeight || sourWidth > destWidth)  
    12.         {  
    13.             if ((sourWidth * destHeight) > (sourHeight * destWidth))  
    14.             {  
    15.                 width = destWidth;  
    16.                 height = (destWidth * sourHeight) / sourWidth;  
    17.             }  
    18.             else  
    19.             {  
    20.                 height = destHeight;  
    21.                 width = (sourWidth * destHeight) / sourHeight;  
    22.             }  
    23.         }  
    24.         else  
    25.         {  
    26.             width = sourWidth;  
    27.             height = sourHeight;  
    28.         }  
    29.         Bitmap destBitmap = new Bitmap(destWidth, destHeight);  
    30.         Graphics g = Graphics.FromImage(destBitmap);  
    31.         g.Clear(Color.Transparent);  
    32.         //设置画布的描绘质量           
    33.         g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;  
    34.         g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;  
    35.         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;  
    36.         g.DrawImage(sourImage, new Rectangle((destWidth - width) / 2, (destHeight - height) / 2, width, height), 0, 0, sourImage.Width, sourImage.Height, GraphicsUnit.Pixel);  
    37.         g.Dispose();  
    38.         //设置压缩质量       
    39.         System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();  
    40.         long[] quality = new long[1];  
    41.         quality[0] = 100;  
    42.         System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);  
    43.         encoderParams.Param[0] = encoderParam;  
    44.         sourImage.Dispose();  
    45.         return destBitmap;  
    46.     }  
    47.     catch  
    48.     {  
    49.         return bitmap;  
    50.     }  
    51. }  
  • 相关阅读:
    Delphi中常用字符串处理函数
    ListView的DrawSubItem时间添加边框,字体变粗问题
    解决d7在更高版本上运行乱码问题,或者是调用更高版本的dll
    使用Indy解决Could not load SSL Library错误
    局域网映射硬盘
    delphi http请求用到的编码方式
    delphi base64编码
    Java基础之抽象类
    ORA-12737: Instant Client Light: unsupported server character set CHS16GBK
    Android TagFlowLayout完全解析 一款针对Tag的布局
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/11171616.html
Copyright © 2011-2022 走看看