博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#图像缩放与裁剪
阅读量:4145 次
发布时间:2019-05-25

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

文笔不好,不废话,直接进入正题,不解释,相信大家都看得懂。

 

按百分比缩放图像:

public static Image ScaleByPercent(Image imgPhoto, int Percent){    float nPercent = ((float)Percent/100);    int sourceWidth = imgPhoto.Width;    int sourceHeight = imgPhoto.Height;    int sourceX = 0;    int sourceY = 0;    int destX = 0;    int destY = 0;     int destWidth  = (int)(sourceWidth * nPercent);    int destHeight = (int)(sourceHeight * nPercent);    Bitmap bmPhoto = new Bitmap(destWidth, destHeight,                              PixelFormat.Format24bppRgb);    bmPhoto.SetResolution(imgPhoto.HorizontalResolution,                             imgPhoto.VerticalResolution);    Graphics grPhoto = Graphics.FromImage(bmPhoto);    grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;    grPhoto.DrawImage(imgPhoto,         new Rectangle(destX,destY,destWidth,destHeight),        new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),        GraphicsUnit.Pixel);    grPhoto.Dispose();    return bmPhoto;}

按指定大小缩放图像:

public static Image FixedSize(Image imgPhoto, int Width, int Height){    int sourceWidth = imgPhoto.Width;    int sourceHeight = imgPhoto.Height;    int sourceX = 0;    int sourceY = 0;    int destX = 0;    int destY = 0;     float nPercent = 0;    float nPercentW = 0;    float nPercentH = 0;    nPercentW = ((float)Width/(float)sourceWidth);    nPercentH = ((float)Height/(float)sourceHeight);    if(nPercentH < nPercentW)    {        nPercent = nPercentH;        destX = System.Convert.ToInt16((Width -                       (sourceWidth * nPercent))/2);    }    else    {        nPercent = nPercentW;        destY = System.Convert.ToInt16((Height -                       (sourceHeight * nPercent))/2);    }    int destWidth  = (int)(sourceWidth * nPercent);    int destHeight = (int)(sourceHeight * nPercent);    Bitmap bmPhoto = new Bitmap(Width, Height,                       PixelFormat.Format24bppRgb);    bmPhoto.SetResolution(imgPhoto.HorizontalResolution,                      imgPhoto.VerticalResolution);    Graphics grPhoto = Graphics.FromImage(bmPhoto);    grPhoto.Clear(Color.Red);    grPhoto.InterpolationMode =             InterpolationMode.HighQualityBicubic;    grPhoto.DrawImage(imgPhoto,         new Rectangle(destX,destY,destWidth,destHeight),        new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),        GraphicsUnit.Pixel);    grPhoto.Dispose();    return bmPhoto;}

按指定位置裁剪指定大小图像:

public static Image Crop(Image imgPhoto, int Width,                     int Height, AnchorPosition Anchor){    int sourceWidth = imgPhoto.Width;    int sourceHeight = imgPhoto.Height;    int sourceX = 0;    int sourceY = 0;    int destX = 0;    int destY = 0;    float nPercent = 0;    float nPercentW = 0;    float nPercentH = 0;    nPercentW = ((float)Width/(float)sourceWidth);    nPercentH = ((float)Height/(float)sourceHeight);    if(nPercentH < nPercentW)    {        nPercent = nPercentW;        switch(Anchor)        {            case AnchorPosition.Top:                destY = 0;                break;            case AnchorPosition.Bottom:                destY = (int)                    (Height - (sourceHeight * nPercent));                    break;            default:                destY = (int)                    ((Height - (sourceHeight * nPercent))/2);                break;        }    }    else    {        nPercent = nPercentH;        switch(Anchor)        {            case AnchorPosition.Left:                destX = 0;                break;            case AnchorPosition.Right:                destX = (int)                  (Width - (sourceWidth * nPercent));                break;            default:                destX = (int)                  ((Width - (sourceWidth * nPercent))/2);                break;        }     }    int destWidth  = (int)(sourceWidth * nPercent);    int destHeight = (int)(sourceHeight * nPercent);    Bitmap bmPhoto = new Bitmap(Width,             Height, PixelFormat.Format24bppRgb);    bmPhoto.SetResolution(imgPhoto.HorizontalResolution,             imgPhoto.VerticalResolution);    Graphics grPhoto = Graphics.FromImage(bmPhoto);    grPhoto.InterpolationMode =             InterpolationMode.HighQualityBicubic;    grPhoto.DrawImage(imgPhoto,         new Rectangle(destX,destY,destWidth,destHeight),        new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),        GraphicsUnit.Pixel);    grPhoto.Dispose();    return bmPhoto;}

 

 

转载请注明出处!!!

 

 

你可能感兴趣的文章
spring事务探索
查看>>
浅谈Spring声明式事务管理ThreadLocal和JDKProxy
查看>>
初识xsd
查看>>
java 设计模式-职责型模式
查看>>
构造型模式
查看>>
svn out of date 无法更新到最新版本
查看>>
java杂记
查看>>
RunTime.getRuntime().exec()
查看>>
Oracle 分组排序函数
查看>>
删除weblogic 域
查看>>
VMware Workstation 14中文破解版下载(附密钥)(笔记)
查看>>
日志框架学习
查看>>
日志框架学习2
查看>>
SVN-无法查看log,提示Want to go offline,时间显示1970问题,error主要是 url中 有一层的中文进行了2次encode
查看>>
NGINX
查看>>
Qt文件夹选择对话框
查看>>
1062 Talent and Virtue (25 分)
查看>>
1061 Dating (20 分)
查看>>
1060 Are They Equal (25 分)
查看>>
83. Remove Duplicates from Sorted List(easy)
查看>>