C#图片截取压缩(百分比压缩/大小压缩)实现代码(2)


View Code
/// <summary>
/// 剪裁 -- 用GDI+
/// </summary>
/// <param>原始Bitmap</param>
/// <param>开始坐标X</param>
/// <param>开始坐标Y</param>
/// <param>宽度</param>
/// <param>高度</param>
/// <returns>剪裁后的Bitmap</returns>
public static Bitmap CutImage(Image 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);
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;
}
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjddyd.html