首页 > 代码库 > C#缩放和裁剪图片
C#缩放和裁剪图片
在GDI+中,缩放和剪裁可以看作同一个操作,无非就是原始区域的选择不同罢了。空口无凭,先看具体算法可能更好理解。
C#代码
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Imaging;
- namespace Project
- {
- class ImageOperation
- {
- /// <summary>
- /// Resize图片
- /// </summary>
- /// <param name="bmp">原始Bitmap </param>
- /// <param name="newW">新的宽度</param>
- /// <param name="newH">新的高度</param>
- /// <param name="Mode">保留着,暂时未用</param>
- /// <returns>处理以后的图片</returns>
- public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH, int Mode)
- {
- try
- {
- Bitmap b = new Bitmap(newW, newH);
- Graphics g = Graphics.FromImage(b);
- // 插值算法的质量
- g.InterpolationMode = InterpolationMode.HighQualityBicubic;
- g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
- g.Dispose();
- return b;
- }
- catch
- {
- return null;
- }
- }
- /// <summary>
- /// 剪裁 -- 用GDI+
- /// </summary>
- /// <param name="b">原始Bitmap</param>
- /// <param name="StartX">开始坐标X</param>
- /// <param name="StartY">开始坐标Y</param>
- /// <param name="iWidth">宽度</param>
- /// <param name="iHeight">高度</param>
- /// <returns>剪裁后的Bitmap</returns>
- public static Bitmap Cut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
- {
- if (b == null)
- {
- return null;
- }
- int w = b.Width;
- int h = b.Height;
- if (StartX >= w || StartY >= h)
- {
- return null;
- }
- if (StartX + iWidth > w)
- {
- iWidth = w - StartX;
- }
- if (StartY + iHeight > h)
- {
- iHeight = h - StartY;
- }
- try
- {
- Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
- Graphics g = Graphics.FromImage(bmpOut);
- g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
- g.Dispose();
- return bmpOut;
- }
- catch
- {
- return null;
- }
- }
- }
- }
目标其实都是new Rectangle(0, 0, iWidth, iHeight),缩放算法把整个原始图都往目标区域里塞new Rectangle(0, 0, bmp.Width, bmp.Height),而剪裁只是把原始区域上等宽等高的那个区域new Rectangle(StartX, StartY, iWidth, iHeight)1:1的塞到目标区域里。
<iframe id="google_ads_frame5" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1402505322&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fgc2013%2Fp%2F3764791.html&dt=1402505323980&shv=r20140603&cbv=r20140417&saldr=sb&correlator=1402505323455&frm=20&ga_vid=469728998.1401679695&ga_sid=1402494257&ga_hid=1073694382&ga_fc=1&u_tz=480&u_his=420&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=12&adx=0&ady=46306&biw=314&bih=74&eid=33995330%2C317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=5&xpc=0NuUlKLqr7&p=http%3A//www.cnblogs.com&dtd=49" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame5" marginWidth="0" scrolling="no" hspace="0"></iframe><iframe id="google_ads_frame6" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1402505322&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fgc2013%2Fp%2F3764791.html&dt=1402505324037&shv=r20140603&cbv=r20140417&saldr=sb&prev_slotnames=8660799060&correlator=1402505323455&frm=20&ga_vid=469728998.1401679695&ga_sid=1402494257&ga_hid=1073694382&ga_fc=1&u_tz=480&u_his=420&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=12&adx=304&ady=46556&biw=314&bih=74&eid=33995330%2C317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=6&xpc=moMx45HYms&p=http%3A//www.cnblogs.com&dtd=42" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame6" marginWidth="0" scrolling="no" hspace="0"></iframe>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。