简单的图片剪裁
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO; namespace ClassLibrary
{
/// <summary>
/// 图片处理
/// </summary>
public static class Img
{
#region 图片裁剪
#region 通过文件路径图片 public static Image ImgCutFromFile
/// <summary>
/// 通过文件路径裁剪图片
/// </summary>
/// <param name="imgFile">文件路径</param>
/// <param name="startX">图片左上角X坐标</param>
/// <param name="startY">图片左上角Y坐标</param>
/// <param name="width">截取宽度</param>
/// <param name="height">截取高度</param>
/// <param name="widthS">图片显示宽度</param>
/// <param name="heightS">图片显示高度</param>
/// <returns></returns>
public static Image ImgCutFromFile(string imgFile, int startX, int startY, int width, int height, int widthS = , int heightS=)
{ Image img = Image.FromFile(imgFile);
if (widthS > && heightS > )
{
GetXYWH(ref startX, ref startY, ref width, ref height, widthS, heightS, img);
}
return ImgCut(img, startX, startY, width, height);
}
#endregion #region 通过流图片 public static Image ImgCutFromStream
/// <summary>
/// 通过流图片
/// </summary>
/// <param name="stream">流</param>
/// <param name="startX">图片左上角X坐标</param>
/// <param name="startY">图片左上角Y坐标</param>
/// <param name="width">截取宽度</param>
/// <param name="height">截取高度</param>
/// <param name="widthS">图片显示宽度</param>
/// <param name="heightS">图片显示高度</param>
/// <returns></returns>
public static Image ImgCutFromStream(Stream stream, int startX, int startY, int width, int height, int widthS = , int heightS = )
{
Image img = Image.FromStream(stream);
if (widthS > && heightS > )
{
GetXYWH(ref startX, ref startY, ref width, ref height, widthS, heightS, img);
}
return ImgCut(img, startX, startY, width, height);
}
#endregion #region 获取实际XYWH private static void GetXYWH
/// <summary>
/// 获取实际XYWH
/// </summary>
/// <param name="startX">图片左上角X坐标</param>
/// <param name="startY">图片左上角Y坐标</param>
/// <param name="width">截取宽度</param>
/// <param name="height">截取高度</param>
/// <param name="widthS">图片显示宽度</param>
/// <param name="heightS">图片显示高度</param>
/// <param name="img"></param>
private static void GetXYWH(ref int startX, ref int startY, ref int width, ref int height, int widthS, int heightS, Image img)
{
/*img.Width / widthS 是实际图片大小与页面显示图片大小比值*/
startX = Convert.ToInt32(startX * img.Width / widthS);
startY = Convert.ToInt32(startY * img.Height / heightS);
width = Convert.ToInt32(width * img.Width / widthS);
height = Convert.ToInt32(height * img.Height / heightS);
}
#endregion #region 执行剪裁 public static Image ImgCut
/// <summary>
/// 图片裁剪
/// </summary>
/// <param name="image">图片</param>
/// <param name="startX">图片左上角X坐标</param>
/// <param name="startY">图片左上角Y坐标</param>
/// <param name="width">截取宽度</param>
/// <param name="height">截取高度</param>
/// <returns></returns>
public static Image ImgCut(Image image, int startX, int startY, int width, int height)
{
Image img = image;//获取原始图片
Image newImg = new Bitmap(width, height);//裁剪后的图片
Graphics g = Graphics.FromImage(newImg);
/*设置质量*/
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
/*图片源位置,图片源大小*/
Rectangle fromR = new Rectangle(startX, startY, width, height);
/*绘图位置绘图大小*/
Rectangle toR = new Rectangle(, , width, height);
/*画图*/
g.DrawImage(img, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
/*释放截图资源*/
g.Dispose();
return newImg;
//newImg.Save(Server.MapPath(""));
//newImg.Dispose();
}
#endregion
#endregion #region 改变图片大小 public static Image ImgSize
/// <summary>
/// 改变图片大小
/// </summary>
/// <param name="image">图片</param>
/// <param name="width">新宽度</param>
/// <param name="height">新高度</param>
/// <returns></returns>
public static Image ImgSize(Image image, int width = , int height = )
{
Image img = image;//获取原始图片
Image newImg = new Bitmap(width, height);//裁剪后的图片
Graphics g = Graphics.FromImage(newImg);
/*设置质量*/
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
/*图片源位置,图片源大小*/
Rectangle fromR = new Rectangle(, , img.Width, img.Height);
/*绘图位置绘图大小*/
if (width == && height == ) {
width = img.Width;
height = img.Height;
}
Rectangle toR = new Rectangle(, , width, height);
/*画图*/
g.DrawImage(img, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
/*释放截图资源*/
g.Dispose();
return newImg;
}
#endregion }
} Img