#region CreateThumbnail
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="uploadObject">上传的HttpPostedFile或图片物理路径</param>
/// <param name="uploaddir">上传文件夹相对路径</param>
/// <param name="ext">后缀(如:.jpg)</param>
/// <param name="t_width">缩略图宽</param>
/// <param name="t_height">缩略图高</param>
/// <param name="filename">文件夹,不含路径和后缀</param>
/// <param name="tm">枚举类-缩略图的样式</param>
/// <returns>返回生成图片的路径</returns>
public static string CreateThumbnail(object uploadObject, string uploaddir, string ext, int t_width, int t_height, string filename, ThumbModel tm)
{
System.Drawing.Image thumbnail_image = null;
System.Drawing.Image original_image = null;
System.Drawing.Bitmap final_image = null;
System.Drawing.Graphics graphic = null;
MemoryStream ms = null;
string ThumbnailFilename = "";
try
{
if (uploadObject is HttpPostedFileBase)
{
HttpPostedFileBase jpeg_image_upload = uploadObject as HttpPostedFileBase;
original_image = System.Drawing.Image.FromStream(jpeg_image_upload.InputStream);
}
else if (uploadObject is HttpPostedFile)
{
HttpPostedFile jpeg_image_upload = uploadObject as HttpPostedFile;
original_image = System.Drawing.Image.FromStream(jpeg_image_upload.InputStream);
}
else
{
string jpeg_image_upload = uploadObject as string;
original_image = System.Drawing.Image.FromFile(jpeg_image_upload);
}
// Calculate the new width and height
int original_paste_x = 0;
int original_paste_y = 0;
int original_width = original_image.Width;//截取原图宽度
int original_height = original_image.Height;//截取原图高度
int target_paste_x = 0;
int target_paste_y = 0;
int target_width1 = t_width;
int target_height1 = t_height;
if (tm == ThumbModel.NoDeformationAllThumb)
{
float target_ratio = (float)t_width / (float)t_height;//缩略图 宽、高的比例
float original_ratio = (float)original_width / (float)original_height;//原图 宽、高的比例
if (target_ratio > original_ratio)//宽拉长
{
target_height1 = t_height;
target_width1 = (int)Math.Floor(original_ratio * (float)t_height);
}
else
{
target_height1 = (int)Math.Floor((float)t_width / original_ratio);
target_width1 = t_width;
}
target_width1 = target_width1 > t_width ? t_width : target_width1;
target_height1 = target_height1 > t_height ? t_height : target_height1;
target_paste_x = (t_width - target_width1) / 2;
target_paste_y = (t_height - target_height1) / 2;
}
else if (tm == ThumbModel.NoDeformationCenterThumb)
{
float target_ratio = (float)t_width / (float)t_height;//缩略图 宽、高的比例
float original_ratio = (float)original_width / (float)original_height;//原图 宽、高的比例
if (target_ratio > original_ratio)//宽拉长
{
original_height = (int)Math.Floor((float)original_width / target_ratio);
}
else
{
original_width=(int)Math.Floor((float)original_height * target_ratio);
}
original_paste_x = (original_image.Width - original_width) / 2;
original_paste_y = (original_image.Height - original_height) / 2;
}
else if(tm == ThumbModel.NoDeformationCenterBig)
{
original_paste_x = (original_width - target_width1) / 2;
original_paste_y = (original_height - target_height1) / 2;
if(original_height>target_height1) original_height=target_height1;
if (original_width > target_width1) original_width = target_width1;
}
final_image = new System.Drawing.Bitmap(t_width, t_height);
graphic = System.Drawing.Graphics.FromImage(final_image);
// graphic.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.White), new System.Drawing.Rectangle(0, 0, t_width, t_height));//背景颜色
graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; /* new way */
graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
graphic.Clear(Color.White);//背景
Rectangle SrcRec = new Rectangle(original_paste_x, original_paste_y, original_width, original_height);
Rectangle targetRec = new Rectangle(target_paste_x, target_paste_y, target_width1, target_height1);
graphic.DrawImage(original_image, targetRec, SrcRec, GraphicsUnit.Pixel);
string saveFileName = uploaddir + filename + "_small" + ext;
using (FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("/" + saveFileName), FileMode.Create))
{
final_image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
ThumbnailFilename = saveFileName;
}
}
catch
{
// If any kind of error occurs return a 500 Internal Server error
HttpContext.Current.Response.StatusCode = 500;
HttpContext.Current.Response.Write("An error occured");
HttpContext.Current.Response.End();
}
finally
{
// Clean up
if (final_image != null) final_image.Dispose();
if (graphic != null) graphic.Dispose();
if (original_image != null) original_image.Dispose();
if (thumbnail_image != null) thumbnail_image.Dispose();
if (ms != null) ms.Close();
}
return ThumbnailFilename;
}
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="uploadObject">上传的HttpPostedFile或图片物理路径</param>
/// <param name="uploaddir">上传文件夹相对路径</param>
/// <param name="ext">后缀(如:.jpg)</param>
/// <param name="t_width">缩略图宽</param>
/// <param name="t_height">缩略图高</param>
/// <param name="filename">文件夹,不含路径和后缀</param>
/// <param name="tm">枚举类-缩略图的样式</param>
/// <returns>返回生成图片的路径</returns>
public static string CreateThumbnail(object uploadObject, string uploaddir, string ext, int t_width, int t_height, string filename, ThumbModel tm)
{
System.Drawing.Image thumbnail_image = null;
System.Drawing.Image original_image = null;
System.Drawing.Bitmap final_image = null;
System.Drawing.Graphics graphic = null;
MemoryStream ms = null;
string ThumbnailFilename = "";
try
{
if (uploadObject is HttpPostedFileBase)
{
HttpPostedFileBase jpeg_image_upload = uploadObject as HttpPostedFileBase;
original_image = System.Drawing.Image.FromStream(jpeg_image_upload.InputStream);
}
else if (uploadObject is HttpPostedFile)
{
HttpPostedFile jpeg_image_upload = uploadObject as HttpPostedFile;
original_image = System.Drawing.Image.FromStream(jpeg_image_upload.InputStream);
}
else
{
string jpeg_image_upload = uploadObject as string;
original_image = System.Drawing.Image.FromFile(jpeg_image_upload);
}
// Calculate the new width and height
int original_paste_x = 0;
int original_paste_y = 0;
int original_width = original_image.Width;//截取原图宽度
int original_height = original_image.Height;//截取原图高度
int target_paste_x = 0;
int target_paste_y = 0;
int target_width1 = t_width;
int target_height1 = t_height;
if (tm == ThumbModel.NoDeformationAllThumb)
{
float target_ratio = (float)t_width / (float)t_height;//缩略图 宽、高的比例
float original_ratio = (float)original_width / (float)original_height;//原图 宽、高的比例
if (target_ratio > original_ratio)//宽拉长
{
target_height1 = t_height;
target_width1 = (int)Math.Floor(original_ratio * (float)t_height);
}
else
{
target_height1 = (int)Math.Floor((float)t_width / original_ratio);
target_width1 = t_width;
}
target_width1 = target_width1 > t_width ? t_width : target_width1;
target_height1 = target_height1 > t_height ? t_height : target_height1;
target_paste_x = (t_width - target_width1) / 2;
target_paste_y = (t_height - target_height1) / 2;
}
else if (tm == ThumbModel.NoDeformationCenterThumb)
{
float target_ratio = (float)t_width / (float)t_height;//缩略图 宽、高的比例
float original_ratio = (float)original_width / (float)original_height;//原图 宽、高的比例
if (target_ratio > original_ratio)//宽拉长
{
original_height = (int)Math.Floor((float)original_width / target_ratio);
}
else
{
original_width=(int)Math.Floor((float)original_height * target_ratio);
}
original_paste_x = (original_image.Width - original_width) / 2;
original_paste_y = (original_image.Height - original_height) / 2;
}
else if(tm == ThumbModel.NoDeformationCenterBig)
{
original_paste_x = (original_width - target_width1) / 2;
original_paste_y = (original_height - target_height1) / 2;
if(original_height>target_height1) original_height=target_height1;
if (original_width > target_width1) original_width = target_width1;
}
final_image = new System.Drawing.Bitmap(t_width, t_height);
graphic = System.Drawing.Graphics.FromImage(final_image);
// graphic.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.White), new System.Drawing.Rectangle(0, 0, t_width, t_height));//背景颜色
graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; /* new way */
graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
graphic.Clear(Color.White);//背景
Rectangle SrcRec = new Rectangle(original_paste_x, original_paste_y, original_width, original_height);
Rectangle targetRec = new Rectangle(target_paste_x, target_paste_y, target_width1, target_height1);
graphic.DrawImage(original_image, targetRec, SrcRec, GraphicsUnit.Pixel);
string saveFileName = uploaddir + filename + "_small" + ext;
using (FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("/" + saveFileName), FileMode.Create))
{
final_image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
ThumbnailFilename = saveFileName;
}
}
catch
{
// If any kind of error occurs return a 500 Internal Server error
HttpContext.Current.Response.StatusCode = 500;
HttpContext.Current.Response.Write("An error occured");
HttpContext.Current.Response.End();
}
finally
{
// Clean up
if (final_image != null) final_image.Dispose();
if (graphic != null) graphic.Dispose();
if (original_image != null) original_image.Dispose();
if (thumbnail_image != null) thumbnail_image.Dispose();
if (ms != null) ms.Close();
}
return ThumbnailFilename;
}
#endregion
public enum ThumbModel
{
/// <summary>
/// 不变形,全部(缩略图)
/// </summary>
NoDeformationAllThumb,
/// <summary>
/// 变形,全部填充(缩略图)
/// </summary>
DeformationAllThumb,
/// <summary>
/// 不变形,截中间(缩略图)
/// </summary>
NoDeformationCenterThumb,
/// <summary>
/// 不变形,截中间(非缩略图)
/// </summary>
NoDeformationCenterBig
}
理论效果:
原测试图:
效果: