ASP.NET 你肯定会用到的图片裁剪功能,可按长度或宽度裁剪,也能绘制一个更大图
以下包括3个功能,
1.按照长度裁剪
2.按照宽度裁剪
3.不用裁剪,直接生成一张大图,把原图包含进去
以上功能不会影响图片质量,这个也是一个亮点
public class UploadPicCut
{
/// <summary>
/// 计算图片大小
/// </summary>
/// <param name="maxWidth"></param>
/// <param name="maxHeight"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public Size NewSize(int maxWidth, int maxHeight, int width, int height)
{
double w = 0.0;
double h = 0.0;
double sw = Convert.ToDouble(width);
double sh = Convert.ToDouble(height);
double mw = Convert.ToDouble(maxWidth);
double mh = Convert.ToDouble(maxHeight);
if (sw < mw && sh < mh)
{
w = sw;
h = sh;
}
else if ((sw / sh) > (mw / mh))
{
w = maxWidth;
h = (w * sh) / sw;
}
else
{
h = maxHeight;
w = (h * sw) / sh;
}
return new Size(Convert.ToInt32(w), Convert.ToInt32(h));
}
/// <summary>
/// 按最大长宽裁剪
/// </summary>
/// <param name="fileName"></param>
/// <param name="SavaFile"></param>
/// <param name="maxWidth"></param>
/// <param name="maxHeight"></param>
public void SendSmallImage(string fileName,string SavaFile ,int maxWidth, int maxHeight)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(fileName));
System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
Size newSize = NewSize(maxWidth, maxHeight, img.Width, img.Height);
Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);
Graphics g = Graphics.FromImage(outBmp);
Color BgColor = Color.FromArgb(235, 235, 235);
g.Clear(BgColor);
// 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height),0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
g.Dispose();
//if (thisFormat.Equals(ImageFormat.Gif))
//{
// System.Web.HttpContext.Current.Response.ContentType = "image/gif";
//}
//else
//{
// System.Web.HttpContext.Current.Response.ContentType = "image/jpeg";
//}
// 以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];//设置JPEG编码
break;
}
}
if (jpegICI != null)
{
outBmp.Save(System.Web.HttpContext.Current.Server.MapPath(SavaFile), jpegICI, encoderParams);
}
else
{
outBmp.Save(System.Web.HttpContext.Current.Server.MapPath(SavaFile), thisFormat);
}
img.Dispose();
outBmp.Dispose();
}
/// <summary>
/// 按最大长宽裁剪
/// </summary>
/// <param name="fileName"></param>
/// <param name="SavaFile"></param>
/// <param name="maxWidth"></param>
/// <param name="maxHeight"></param>
public void SaveToBig(string fileName, string SavaFile, int ToWidth, int ToHeight)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(fileName));
System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
Bitmap outBmp = new Bitmap(ToWidth, ToHeight);
Graphics g = Graphics.FromImage(outBmp);
Color BgColor = Color.FromArgb(235, 235, 235);
g.Clear(BgColor);
// 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, 0, 0, ToWidth, ToHeight);
g.Dispose();
// 以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];//设置JPEG编码
break;
}
}
if (jpegICI != null)
{
outBmp.Save(System.Web.HttpContext.Current.Server.MapPath(SavaFile), jpegICI, encoderParams);
}
else
{
outBmp.Save(System.Web.HttpContext.Current.Server.MapPath(SavaFile), thisFormat);
}
img.Dispose();
outBmp.Dispose();
}
/// <summary>
/// 绘成更大的图片
/// </summary>
/// <param name="fileName"></param>
/// <param name="SavaFile"></param>
/// <param name="maxWidth"></param>
/// <param name="maxHeight"></param>
public void SendSmallImageB(string fileName, string SavaFile, int maxWidth, int maxHeight, int FromX, int FromY)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(fileName));
System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
//Size newSize = NewSize(maxWidth, maxHeight, img.Width, img.Height);
//Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);
Bitmap outBmp = new Bitmap(maxWidth, maxHeight);
Graphics g = Graphics.FromImage(outBmp);
Color BgColor = Color.FromArgb(235, 235, 235);
g.Clear(BgColor);
// 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, new Rectangle(FromX, FromY, img.Width+2*FromX, img.Height+2*FromY), 0, 0, maxWidth, maxHeight, GraphicsUnit.Pixel);
//g.DrawImage(img, new Rectangle(FromX, FromY, newSize.Width, newSize.Height),0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
g.Dispose();
//if (thisFormat.Equals(ImageFormat.Gif))
//{
// System.Web.HttpContext.Current.Response.ContentType = "image/gif";
//}
//else
//{
// System.Web.HttpContext.Current.Response.ContentType = "image/jpeg";
//}
// 以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];//设置JPEG编码
break;
}
}
if (jpegICI != null)
{
outBmp.Save(System.Web.HttpContext.Current.Server.MapPath(SavaFile), jpegICI, encoderParams);
}
else
{
outBmp.Save(System.Web.HttpContext.Current.Server.MapPath(SavaFile), thisFormat);
}
img.Dispose();
outBmp.Dispose();
}
}