# Bitmap 缩放 剪裁
来源:互联网 发布:上海智能消防学校应知 编辑:程序博客网 时间:2024/05/18 15:50
/// <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 KiResizeImage(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 KiCut(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;
}
}
注意到区别了吗?提示,g.DrawImage中第二个new Rectangle。
目标其实都是new Rectangle(0, 0, iWidth, iHeight),缩放算法把整个原始图都往目标区域里塞new Rectangle(0, 0, bmp.Width, bmp.Height),而剪裁只是把原始区域上等宽等高的那个区域new Rectangle(StartX, StartY, iWidth, iHeight)1:1的塞到目标区域里。很容易吧。
- # Bitmap 缩放 剪裁
- Bitmap缩放剪裁便宜旋转
- bitmap缩放剪裁图片(等宽裁中间部分高)
- 拍照+剪裁+bitmap
- Bitmap缩放
- c#缩放和剪裁图片
- Bitmap/byte/Drawble 相互转换 + 内存剪裁
- CGContext对缩放图片的剪裁
- 动态缩放图片,剪裁指定位置大小
- 图片处理类(剪裁、缩放、水印)
- 图片处理类(剪裁、缩放、水印)
- bitmap的缩放
- android bitmap缩放
- android Bitmap缩放
- 安卓-BitMap缩放
- Bitmap图片缩放处理
- Bitmap 图片缩放
- Android Bitmap缩放
- 供应商评判项目
- Error filterStart 错误解决
- 用Awstats统计多域名Apache流量
- VB.NET实现MD5算法加密(32位)
- linux mysql 命令 大全
- # Bitmap 缩放 剪裁
- 面试经验谈——理发师(摘自《程序员》07年1月刊,作者郭安定)
- 创建SvcHost.exe调用的服务原理与实践
- IE下控制打印的Script代码
- 感受
- PHP - Smarty模板引擎 - 翻译 - Manual手册 - Chapter 3. Basic Syntax第3章 基础语法 - Comments注释
- 【扩展学习wiki】EPS: Evolved Packet System演进分组系统
- 技术和业务,职业规划时,你选择哪一个?
- Update语句的高级用法