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

     1         /// <summary>
     2         /// 等比例缩放图片
     3         /// </summary>
     4         /// <param name="bitmap">图片</param>
     5         /// <param name="destHeight">高度</param>
     6         /// <param name="destWidth">宽度</param>
     7         /// <returns></returns>
     8         private Bitmap ZoomImage(Bitmap bitmap, int destHeight, int destWidth)
     9         {
    10             try
    11             {
    12                 System.Drawing.Image sourImage = bitmap;
    13                 int width = 0, height = 0;
    14                 //按比例缩放           
    15                 int sourWidth = sourImage.Width;
    16                 int sourHeight = sourImage.Height;
    17                 if (sourHeight > destHeight || sourWidth > destWidth)
    18                 {
    19                     if ((sourWidth * destHeight) > (sourHeight * destWidth))
    20                     {
    21                         width = destWidth;
    22                         height = (destWidth * sourHeight) / sourWidth;
    23                     }
    24                     else
    25                     {
    26                         height = destHeight;
    27                         width = (sourWidth * destHeight) / sourHeight;
    28                     }
    29                 }
    30                 else
    31                 {
    32                     width = sourWidth;
    33                     height = sourHeight;
    34                 }
    35                 Bitmap destBitmap = new Bitmap(destWidth, destHeight);
    36                 Graphics g = Graphics.FromImage(destBitmap);
    37                 g.Clear(Color.Transparent);
    38                 //设置画布的描绘质量         
    39                 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
    40                 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    41                 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    42                 g.DrawImage(sourImage, new Rectangle((destWidth - width) / 2, (destHeight - height) / 2, width, height), 0, 0, sourImage.Width, sourImage.Height, GraphicsUnit.Pixel);
    43                 g.Dispose();
    44                 //设置压缩质量     
    45                 System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
    46                 long[] quality = new long[1];
    47                 quality[0] = 100;
    48                 System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
    49                 encoderParams.Param[0] = encoderParam;
    50                 sourImage.Dispose();
    51                 return destBitmap;
    52             }
    53             catch
    54             {
    55                 return bitmap;
    56             }
    57         }
  • 相关阅读:
    苹果IPhone真机开发调试
    Unity3d 错误提示 GUI Error: You are pushing more GUIClips than you are popping. Make sure they are balanced
    Unity编辑器环境在Inspector面板中显示变量
    微信小程序开发
    Android 9.0 Http不能访问网络
    c#传不确定的参数个数,比如int型
    玩转@Git三剑客
    白话法律42讲-为程序员打造的专属法律武器
    uSurvival 1.41多人在线生存逃杀吃鸡类游戏源码
    NGUI: Next-Gen UI 2018.3.0f
  • 原文地址:https://www.cnblogs.com/s666/p/13594252.html
Copyright © 2011-2022 走看看