【IT168 技术文档】本文旨在与各位朋友们分享我是如何在项目中用C# “ps图片” 为网站生成同比例微缩图的解决方案。如有不足之处欢迎您指出。
一、技术概述:
1.Ajax无刷新上传图片,详情请阅我的这篇文章。(jquery + c# ashx)
2.C#位图处理 System.Drawing。
3.最新demo支持IE7,IE8,FireFox。
二、微缩图处理方法:
生成微缩图的核心方法:
CreateThumbnailPicture
/// <summary>
/// 图片微缩图处理
/// </summary>
/// <param name="srcPath">源图片</param>
/// <param name="destPath">目标图片</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
public static void CreateThumbnailPicture(string srcPath, string destPath, int width, int height)
{
//根据图片的磁盘绝对路径获取 源图片 的Image对象
System.Drawing.Image img = System.Drawing.Image.FromFile(srcPath);
//bmp: 最终要建立的 微缩图 位图对象。 Bitmap bmp = new Bitmap(width, height);
//g: 绘制 bmp Graphics 对象
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Transparent);
//为Graphics g 对象 初始化必要参数,很容易理解。
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//源图片宽和高
int imgWidth = img.Width;
int imgHeight = img.Height;
//绘制微缩图
g.DrawImage(img, new System.Drawing.Rectangle(0, 0, width, height), new System.Drawing.Rectangle(0, 0, imgWidth, imgHeight) , GraphicsUnit.Pixel);
ImageFormat format = img.RawFormat;
ImageCodecInfo info = ImageCodecInfo.GetImageEncoders().SingleOrDefault(i => i.FormatID == format.Guid);
EncoderParameter param = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = param;
img.Dispose();
//保存已生成微缩图,这里将GIF格式转化成png格式。
if (format == ImageFormat.Gif)
{
destPath = destPath.ToLower().Replace(".gif", ".png");
bmp.Save(destPath, ImageFormat.Png);
}
else
{
if (info != null)
{
bmp.Save(destPath, info, parameters);
}
else
{
bmp.Save(destPath, format);
}
}
img.Dispose();
g.Dispose();
bmp.Dispose();
}
/// 图片微缩图处理
/// </summary>
/// <param name="srcPath">源图片</param>
/// <param name="destPath">目标图片</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
public static void CreateThumbnailPicture(string srcPath, string destPath, int width, int height)
{
//根据图片的磁盘绝对路径获取 源图片 的Image对象
System.Drawing.Image img = System.Drawing.Image.FromFile(srcPath);
//bmp: 最终要建立的 微缩图 位图对象。 Bitmap bmp = new Bitmap(width, height);
//g: 绘制 bmp Graphics 对象
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Transparent);
//为Graphics g 对象 初始化必要参数,很容易理解。
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//源图片宽和高
int imgWidth = img.Width;
int imgHeight = img.Height;
//绘制微缩图
g.DrawImage(img, new System.Drawing.Rectangle(0, 0, width, height), new System.Drawing.Rectangle(0, 0, imgWidth, imgHeight) , GraphicsUnit.Pixel);
ImageFormat format = img.RawFormat;
ImageCodecInfo info = ImageCodecInfo.GetImageEncoders().SingleOrDefault(i => i.FormatID == format.Guid);
EncoderParameter param = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = param;
img.Dispose();
//保存已生成微缩图,这里将GIF格式转化成png格式。
if (format == ImageFormat.Gif)
{
destPath = destPath.ToLower().Replace(".gif", ".png");
bmp.Save(destPath, ImageFormat.Png);
}
else
{
if (info != null)
{
bmp.Save(destPath, info, parameters);
}
else
{
bmp.Save(destPath, format);
}
}
img.Dispose();
g.Dispose();
bmp.Dispose();
}
部分代码已经加入注释,仔细阅读代码应该不难理解。下面介绍ashx中AJAX调用方法,我们在AJAX异步上传图片成功后对源图片进行"PS"。关键代码片段如下:
//上传成功后网站内源图片相对路径
string relativePath = System.Web.HttpContext.Current.Request.ApplicationPath
+ string.Format(@"Content/Upload/Images/{0}", fileName);
/*
比例处理
微缩图高度(DefaultHeight属性值为 400)
*/
System.Drawing.Image img = System.Drawing.Image.FromFile(toFile);
int width = img.Width;
int height = img.Height;
float ratio = (float)width / height;
//微缩图高度和宽度
int newHeight = height <= DefaultHeight ? height : DefaultHeight;
int newWidth = height <= DefaultHeight ? width : Convert.ToInt32(DefaultHeight * ratio);
FileInfo generatedfile = new FileInfo(toFile);
string newFileName = "Thumb_" + generatedfile.Name;
string newFilePath = Path.Combine(generatedfile.DirectoryName, newFileName);
PictureHandler.CreateThumbnailPicture(toFile, newFilePath, newWidth, newHeight);
string thumbRelativePath = System.Web.HttpContext.Current.Request.ApplicationPath
+ string.Format(@"/Content/Upload/Images/{0}", newFileName);
//返回原图和微缩图的网站相对路径
relativePath = string.Format("{0},{1}", relativePath, thumbRelativePath);
return relativePath;
string relativePath = System.Web.HttpContext.Current.Request.ApplicationPath
+ string.Format(@"Content/Upload/Images/{0}", fileName);
/*
比例处理
微缩图高度(DefaultHeight属性值为 400)
*/
System.Drawing.Image img = System.Drawing.Image.FromFile(toFile);
int width = img.Width;
int height = img.Height;
float ratio = (float)width / height;
//微缩图高度和宽度
int newHeight = height <= DefaultHeight ? height : DefaultHeight;
int newWidth = height <= DefaultHeight ? width : Convert.ToInt32(DefaultHeight * ratio);
FileInfo generatedfile = new FileInfo(toFile);
string newFileName = "Thumb_" + generatedfile.Name;
string newFilePath = Path.Combine(generatedfile.DirectoryName, newFileName);
PictureHandler.CreateThumbnailPicture(toFile, newFilePath, newWidth, newHeight);
string thumbRelativePath = System.Web.HttpContext.Current.Request.ApplicationPath
+ string.Format(@"/Content/Upload/Images/{0}", newFileName);
//返回原图和微缩图的网站相对路径
relativePath = string.Format("{0},{1}", relativePath, thumbRelativePath);
return relativePath;
三、程序运行截图:
上传前:
上传后:
四、 小结:
我使用该方法主要是为了解决打印报表时由于图片大小没有合理的比例规范导致报表样式变形,同样该方法也适合网站或论坛由用户上传源图片生成微缩头像等。
如果您感兴趣可以在这里下载本例DEMO。
最后希望本文能够帮助您解决开发中的类似问题。