• C#缩放和裁剪图片


    在GDI+中,缩放和剪裁可以看作同一个操作,无非就是原始区域的选择不同罢了。空口无凭,先看具体算法可能更好理解。

    C#代码  收藏代码
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Text;  
    4. using System.Drawing;  
    5. using System.Drawing.Drawing2D;  
    6. using System.Drawing.Imaging;  
    7.   
    8. namespace Project  
    9. {  
    10.     class ImageOperation  
    11.     {  
    12.         /// <summary>  
    13.         ///  Resize图片   
    14.         /// </summary>  
    15.         /// <param name="bmp">原始Bitmap </param>  
    16.         /// <param name="newW">新的宽度</param>  
    17.         /// <param name="newH">新的高度</param>  
    18.         /// <param name="Mode">保留着,暂时未用</param>  
    19.         /// <returns>处理以后的图片</returns>  
    20.   
    21.         public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH, int Mode)  
    22.         {  
    23.             try  
    24.             {  
    25.                 Bitmap b = new Bitmap(newW, newH);  
    26.                 Graphics g = Graphics.FromImage(b);  
    27.                 // 插值算法的质量   
    28.                 g.InterpolationMode = InterpolationMode.HighQualityBicubic;  
    29.                 g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);  
    30.                 g.Dispose();  
    31.                 return b;  
    32.             }  
    33.             catch  
    34.             {  
    35.                 return null;  
    36.             }  
    37.         }  
    38.         /// <summary>  
    39.         /// 剪裁 -- 用GDI+   
    40.         /// </summary>  
    41.         /// <param name="b">原始Bitmap</param>  
    42.         /// <param name="StartX">开始坐标X</param>  
    43.         /// <param name="StartY">开始坐标Y</param>  
    44.         /// <param name="iWidth">宽度</param>  
    45.         /// <param name="iHeight">高度</param>  
    46.         /// <returns>剪裁后的Bitmap</returns>  
    47.         public static Bitmap Cut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)  
    48.         {  
    49.             if (b == null)  
    50.             {  
    51.                 return null;  
    52.             }  
    53.             int w = b.Width;  
    54.             int h = b.Height;  
    55.             if (StartX >= w || StartY >= h)  
    56.             {  
    57.                 return null;  
    58.             }  
    59.             if (StartX + iWidth > w)  
    60.             {  
    61.                 iWidth = w - StartX;  
    62.             }  
    63.             if (StartY + iHeight > h)  
    64.             {  
    65.                 iHeight = h - StartY;  
    66.             }  
    67.             try  
    68.             {  
    69.                 Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);  
    70.                 Graphics g = Graphics.FromImage(bmpOut);  
    71.                 g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);  
    72.                 g.Dispose();  
    73.                 return bmpOut;  
    74.             }  
    75.             catch  
    76.             {  
    77.                 return null;  
    78.             }  
    79.         }  
    80.     }  
    81. }  

     目标其实都是new Rectangle(0, 0, iWidth, iHeight),缩放算法把整个原始图都往目标区域里塞new Rectangle(0, 0, bmp.Width, bmp.Height),而剪裁只是把原始区域上等宽等高的那个区域new Rectangle(StartX, StartY, iWidth, iHeight)1:1的塞到目标区域里。

  • 相关阅读:
    visio 改变画布大小
    Delphi 让自己的软件实现双击打开文件 转
    气相色谱里面的斜率灵敏度什么意思
    色谱峰的类型BB,BV,VB等都是什么意思
    Delphi的windows剪切板操作函数
    上传图片,多图上传,预览功能,js原生无依赖
    前端性能监控:window.performance
    ES6新属性笔记
    :jQuery实例【DEMO】
    自定义checkbox/radio
  • 原文地址:https://www.cnblogs.com/gc2013/p/3764791.html
Copyright © 2020-2023  润新知