您的位置:首页 > 运维架构 > 网站架构

网站中的缩略图是如何生成的?(C#处理图像)

2011-05-19 16:33 381 查看
网站中的缩略图是如何生成的?(C#处理图像)

controller层代码:

/// <summary>
/// 生成缩略图
/// </summary>
/// <returns></returns>
public ActionResult ThumImg()
{

Response.Clear();
//string originalFileName = Server.MapPath(Request.QueryString["fn"]); //服务器上的目录名
string originalFileName = Request.QueryString["fn"];//本地目录名
int thumbnailWidth = Convert.ToInt16(Request.QueryString["tw"]);
int thumbnailHeight = Convert.ToInt16(Request.QueryString["th"]);
string bgColorString = Convert.ToString(Request.QueryString["bg"]);
new ThumImage(originalFileName, thumbnailWidth, thumbnailHeight, bgColorString).ThumImg();
return View();
}

}

VIEW层代码:

<%int j=0;
foreach (var i in ViewData["ImgList"] as List<string>)
{
j++;
if (j > 20) break;
%>
<img src='/product/ThumImg?fn=<%=i %>&tw=50&th=50&bg=000000' border="0">
<%} %>

Coder层代码:

public class ThumImage
{
string originalFileName;
int thumbnailWidth;
int thumbnailHeight;
string bgColorString;
public ThumImage(string _originalFileName, int _thumbnailWidth, int _thumbnailHeight, string _bgColorString)
{
this.originalFileName = _originalFileName;
this.thumbnailWidth = _thumbnailWidth;
this.thumbnailHeight = _thumbnailHeight;
this.bgColorString = _bgColorString;
}
/// <summary>
/// 生成缩略图
/// </summary>
/// <returns></returns>
public void ThumImg()
{

HttpContext.Current.Response.Clear();
#region 建立图像主体对象
ColorConverter cv = new ColorConverter();
Color bgColor = (Color)cv.ConvertFromString("#" + bgColorString);//通过string,生成一个color对象,ColorConverter是颜色转换方法
System.Drawing.Image img = System.Drawing.Image.FromFile(originalFileName);//通过url得到image对象
System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;//指定文件的文件格式,它是ImageFormat类型,不可承继
System.Drawing.Size newSize = this.GetNewSize(thumbnailWidth, thumbnailHeight, img.Width, img.Height);//图像尺寸,GetNewSize实现等比例缩放
System.Drawing.Bitmap outBmp = new Bitmap(thumbnailWidth, thumbnailHeight);//建立一个封闭GDI+ 位图对象
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(outBmp);//建立一个GDI+ 位图画面,从指定的Image对象中
#endregion

#region 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.Default;
g.SmoothingMode = SmoothingMode.Default;
g.InterpolationMode = InterpolationMode.Default;
g.Clear(bgColor);
Rectangle r = new Rectangle((thumbnailWidth - newSize.Width) / 2,
(thumbnailHeight - newSize.Height) / 2,
newSize.Width, newSize.Height);
g.DrawImage(img, r, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
g.Dispose();
#endregion

#region 设置服务器响应文件头类型
if (thisFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
{
HttpContext.Current.Response.ContentType = "image/gif";//网页内容类型为图像的MIME
}
else
{
HttpContext.Current.Response.ContentType = "image/jpeg";
}
#endregion

//设置压缩质量
System.Drawing.Imaging.EncoderParameters encoderParams = new EncoderParameters();
System.Drawing.Imaging.EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, new long[] { 100 });
encoderParams.Param[0] = encoderParam;

//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
System.Drawing.Imaging.ImageCodecInfo jpegICI = null;

for (int fwd = 0; fwd < arrayICI.Length; fwd++)
{
if (arrayICI[fwd].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[fwd];
break;
}
}

if (jpegICI == null)
{
outBmp.Save(HttpContext.Current.Response.OutputStream, jpegICI, encoderParams);
}
else
{
outBmp.Save(HttpContext.Current.Response.OutputStream, thisFormat); //保存的页面的输出流中
}
}

/// <summary>
/// 图像根据相应尺寸实现等比例缩放
/// </summary>
/// <param name="maxWidth"></param>
/// <param name="maxHeight"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
System.Drawing.Size GetNewSize(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 System.Drawing.Size(Convert.ToInt32(w), Convert.ToInt32(h));
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: