博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#缩放和裁剪图片
阅读量:7079 次
发布时间:2019-06-28

本文共 1925 字,大约阅读时间需要 6 分钟。

在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的塞到目标区域里。

转载地址:http://scpml.baihongyu.com/

你可能感兴趣的文章
OPC接口相关资料地址
查看>>
SpringMVC HelloWorld实例开发及部署
查看>>
BUPT2017 wintertraining(15) #2 题解
查看>>
Java开发中的23种设计模式详解(转)
查看>>
Android最佳性能实践(一)——合理管理内存
查看>>
assert的作用是什么
查看>>
HTML5+CSS3 效果网站集合
查看>>
AutoPlay Menu Builder入门教程
查看>>
Request.Cookies和Response.Cookies
查看>>
在Razor中输出Html的两种方式
查看>>
iOS-打包成ipa的4种方法
查看>>
转负二进制(个人模版)
查看>>
MySQL数据库如何导入导出
查看>>
yii2: oralce中文,有的汉字是2个字节,有的汉字是3个字节
查看>>
mysql utf8mb4 所引起的问题
查看>>
JGraphT
查看>>
【java】实体类中 按照特定的字段 进行升序/降序 排序
查看>>
Unity Shader 基础(1): RenderType & ReplacementShader
查看>>
51Nod 1016 水仙花数 V2(组合数学,枚举打表法)
查看>>
记一次校招面试
查看>>