在网页上显示真正的缩略图,非js处理的缩放

本文介绍了一种生成图片缩略图的方法,通过调整图片尺寸并保持原有比例,使其适合网页展示,同时提高了加载速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网站上可能会有很多图片,比如产品图片等,而且他们可能大小不一,宽度和高度也不一定一样,有的很大有的很小。如果放在一张网页上,可能会破坏版面,但是如果强制让他们按照指定的宽度和高度显示,因为比例不同还会出现变形,显示效果很糟糕,还有最大的缺点是,文件尺寸丝毫没有变化,当图片很大的时候,用户想要看到图片,必须经过漫长等待下载图片,怎么办呢?

好,这里设计到了缩略图,就像Windows中的缩略图查看一样,你所看到的是从原图按照1:1比例缩小的图片,而且满足规定在指定宽度和高度的范围内显示(如果图片填不满,就用空白)。缩略图不是原图,而是利用原图实时按照指定大小生成的,他的好处就是你可以充分控制缩略图的质量,宽度高度,文件大小也在合理的范围内,省去漫长等待。

本文将讨论如何生成缩略图,举一反三,又可以派生许多用处,比如,自己写一个验证码控件等。

1、理解对图片的请求和流
一般来说,我们用http://xxx/a.aspx对a.aspx网页请求。ASP.NET处理了网页以后,就把该网页的内容发送回浏览器。a.aspx的内容一般是含有超文本标记的文本文件流,这是谁都不会怀疑的。但是a.aspx不但能够返回这种非常平常的网页文本外,把它广义开来,它其实可以放回任何类型的流数据。而,我们只需要对Response对象进行操作即可改变输出流的内容。
把图像文件看作是一个二进制流,我们试图从一个图像文件创建了他的流对象,并且将流写入到Response.OutputStream中,这样图像文件就被发给了请求的浏览器。但是浏览器还必须要识别出这是一个图像文件,因此,在发送这个流之前,将Response.ContentType更改成这种图像文件的MIME类型。浏览器在收到这个流之后,调用相关的应用程序,图像就被显示在了浏览器上。虽然实际地址还是aspx结尾。
这样我们就能很好理解怎么去向用户发送标记。例如,在一张普通的网页中写标记img标记,使他的src指向a.aspx。浏览器在得到这张网页后,会处理img标记的内容,并向a.aspx发出请求,这是a.aspx作为图像流返回,浏览器就将图片显示出来。

2、生成缩略图
有了上述技术基础,我们可以建立这样一个空的aspx网页,他通过接受传入的参数,生成缩略图的流,发送回浏览器。
我们创建一个名叫GetThumbnail.aspx的文件,内容如下:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="GetThumbnail.aspx.vb" Inherits="_51use.GetThumbnail"%>

这一句Page指令仅仅是告诉服务器,这个文件的隐藏类是_51use.GetThumbnail,而如果我们都不作任何操作的话,这个文件最终输出时空的。
接下来看一下他的隐藏类是如何写的,在这里我们使用的是B语言:

程序代码:

ContractedBlock.gif ExpandedBlockStart.gif 源代码
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->1None.gifusingSystem;
2None.gifusingSystem.Collections;
3None.gifusingSystem.ComponentModel;
4None.gifusingSystem.Data;
5None.gifusingSystem.Drawing;
6None.gifusingSystem.Drawing.Drawing2D;
7None.gifusingSystem.Drawing.Imaging;
8None.gifusingSystem.Web;
9None.gifusingSystem.Web.SessionState;
10None.gifusingSystem.Web.UI;
11None.gifusingSystem.Web.UI.WebControls;
12None.gifusingSystem.Web.UI.HtmlControls;
13None.gif
14None.gifnamespaceMeiYuan
15ExpandedBlockStart.gifContractedBlock.gifdot.gif{
16ExpandedSubBlockStart.gifContractedSubBlock.gif/**////<summary>
17InBlock.gif///GetThumbnail的摘要说明。
18ExpandedSubBlockEnd.gif///</summary>

19InBlock.gifpublicclassGetThumbnail:System.Web.UI.Page
20ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
21InBlock.gif
22InBlock.gifprivatevoidPage_Load(objectsender,System.EventArgse)
23ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
24InBlock.gifResponse.Clear();
25InBlock.gif
26InBlock.gifstringoriginalFileName=Server.MapPath(Request.QueryString["fn"]);
27InBlock.gifintthumbnailWidth=Convert.ToInt16(Request.QueryString["tw"]);
28InBlock.gifintthumbnailHeight=Convert.ToInt16(Request.QueryString["th"]);
29InBlock.gifstringbgColorString=Convert.ToString(Request.QueryString["bg"]);
30InBlock.gif
31InBlock.gifColorConvertercv=newColorConverter();
32InBlock.gifColorbgColor=(Color)cv.ConvertFromString("#"+bgColorString);
33InBlock.gif
34InBlock.gifSystem.Drawing.Imageimg=System.Drawing.Image.FromFile(originalFileName);
35InBlock.gif
36InBlock.gifSystem.Drawing.Imaging.ImageFormatthisFormat=img.RawFormat;
37InBlock.gif
38InBlock.gifSystem.Drawing.SizenewSize=this.GetNewSize(thumbnailWidth,thumbnailHeight,img.Width,img.Height);
39InBlock.gif
40InBlock.gifSystem.Drawing.BitmapoutBmp=newBitmap(thumbnailWidth,thumbnailHeight);
41InBlock.gif
42InBlock.gifSystem.Drawing.Graphicsg=System.Drawing.Graphics.FromImage(outBmp);
43InBlock.gif
44InBlock.gif//设置画布的描绘质量
45InBlock.gifg.CompositingQuality=CompositingQuality.HighQuality;
46InBlock.gifg.SmoothingMode=SmoothingMode.HighQuality;
47InBlock.gifg.InterpolationMode=InterpolationMode.HighQualityBicubic;
48InBlock.gif//g.Clear(System.Drawing.Color.FromArgb(255,249,255,240));
49InBlock.gifg.Clear(bgColor);
50InBlock.gifg.DrawImage(img,newRectangle((thumbnailWidth-newSize.Width)/2,
51InBlock.gif(thumbnailHeight-newSize.Height)/2,
52InBlock.gifnewSize.Width,newSize.Height),
53InBlock.gif0,0,img.Width,img.Height,GraphicsUnit.Pixel);
54InBlock.gifg.Dispose();
55InBlock.gif
56InBlock.gifif(thisFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
57ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
58InBlock.gifResponse.ContentType="image/gif";
59ExpandedSubBlockEnd.gif}

60InBlock.gifelse
61ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
62InBlock.gifResponse.ContentType="image/jpeg";
63ExpandedSubBlockEnd.gif}

64InBlock.gif
65InBlock.gif
66InBlock.gif//设置压缩质量
67InBlock.gifSystem.Drawing.Imaging.EncoderParametersencoderParams=newEncoderParameters();
68InBlock.gif
69InBlock.gifSystem.Drawing.Imaging.EncoderParameterencoderParam=
70ExpandedSubBlockStart.gifContractedSubBlock.gifnewEncoderParameter(System.Drawing.Imaging.Encoder.Quality,newlong[]dot.gif{100});
71InBlock.gifencoderParams.Param[0]=encoderParam;
72InBlock.gif
73InBlock.gif
74InBlock.gif
75InBlock.gif//获得包含有关内置图像编码解码器的信息的ImageCodecInfo对象。
76InBlock.gifSystem.Drawing.Imaging.ImageCodecInfo[]arrayICI=System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
77InBlock.gifSystem.Drawing.Imaging.ImageCodecInfojpegICI=null;
78InBlock.gif
79InBlock.giffor(intfwd=0;fwd<arrayICI.Length;fwd++)
80ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
81InBlock.gifif(arrayICI[fwd].FormatDescription.Equals("JPEG"))
82ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
83InBlock.gifjpegICI=arrayICI[fwd];
84InBlock.gifbreak;
85ExpandedSubBlockEnd.gif}

86ExpandedSubBlockEnd.gif}

87InBlock.gif
88InBlock.gifif(jpegICI==null)
89ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
90InBlock.gifoutBmp.Save(Response.OutputStream,jpegICI,encoderParams);
91ExpandedSubBlockEnd.gif}

92InBlock.gifelse
93ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
94InBlock.gifoutBmp.Save(Response.OutputStream,thisFormat);
95ExpandedSubBlockEnd.gif}

96InBlock.gif
97InBlock.gif
98InBlock.gif
99ExpandedSubBlockEnd.gif}

100InBlock.gif
101ContractedSubBlock.gifExpandedSubBlockStart.gifWeb窗体设计器生成的代码#regionWeb窗体设计器生成的代码
102InBlock.gifoverrideprotectedvoidOnInit(EventArgse)
103ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
104InBlock.gif//
105InBlock.gif//CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。
106InBlock.gif//
107InBlock.gifInitializeComponent();
108InBlock.gifbase.OnInit(e);
109ExpandedSubBlockEnd.gif}

110InBlock.gif
111ExpandedSubBlockStart.gifContractedSubBlock.gif/**////<summary>
112InBlock.gif///设计器支持所需的方法-不要使用代码编辑器修改
113InBlock.gif///此方法的内容。
114ExpandedSubBlockEnd.gif///</summary>

115InBlock.gifprivatevoidInitializeComponent()
116ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
117InBlock.gifthis.Load+=newSystem.EventHandler(this.Page_Load);
118ExpandedSubBlockEnd.gif}

119ExpandedSubBlockEnd.gif#endregion

120InBlock.gif
121InBlock.gifSystem.Drawing.SizeGetNewSize(intmaxWidth,intmaxHeight,intwidth,intheight)
122ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
123InBlock.gifDoublew=0.0;
124InBlock.gifDoubleh=0.0;
125InBlock.gifDoublesw=Convert.ToDouble(width);
126InBlock.gifDoublesh=Convert.ToDouble(height);
127InBlock.gifDoublemw=Convert.ToDouble(maxWidth);
128InBlock.gifDoublemh=Convert.ToDouble(maxHeight);
129InBlock.gif
130InBlock.gifif(sw<mw&&sh<mh)
131ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
132InBlock.gifw=sw;
133InBlock.gifh=sh;
134ExpandedSubBlockEnd.gif}

135InBlock.gifelseif((sw/sh)>(mw/mh))
136ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
137InBlock.gifw=maxWidth;
138InBlock.gifh=(w*sh)/sw;
139ExpandedSubBlockEnd.gif}

140InBlock.gifelse
141ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
142InBlock.gifh=maxHeight;
143InBlock.gifw=(h*sw)/sh;
144ExpandedSubBlockEnd.gif}

145InBlock.gifreturnnewSystem.Drawing.Size(Convert.ToInt32(w),Convert.ToInt32(h));
146ExpandedSubBlockEnd.gif}

147InBlock.gif
148InBlock.gif
149ExpandedSubBlockEnd.gif}

150ExpandedBlockEnd.gif}

151None.gif


网页中使用方法

ContractedBlock.gif ExpandedBlockStart.gif 调用示例
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->1None.gif<Ahref='ProductDetail.aspx?ProductID=<%#DataBinder.Eval(Container,"DataItem.ProductID")%>'target=_blank>
<
IMGheight=80src='GetThumbnail.aspx?fn=<%#Server.UrlEncode(DataBinder.Eval(Container,"DataItem.ImageUrl").ToString())%>&amp;tw=80&amp;th=80&amp;bg=FFF3F7'width=80border=0>
</
A>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值