首页 > 编程 > .NET > 正文

在ASP.NET里轻松实现缩略图

2024-07-10 12:57:12
字体:
来源:转载
供稿:网友
,欢迎访问网页设计爱好者web开发。以前,在页面上实现缩略图必须借助第三方组件。现在,有了.net,就可以很轻松地实现缩略图。下面就是实现缩略图的例子。

查看例子

代码如下:thumbnail.aspx

<%@ page language="vb" autoeventwireup="false" codebehind="thumbnail.aspx.vb"
inherits="aspxweb.thumbnail" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>在asp.net里轻松实现缩略图</title>
<meta content="microsoft visual studio.net 7.0" name="generator">
<meta content="visual basic 7.0" name="code_language">
<meta content="javascript" name="vs_defaultclientscript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetschema">
</head>
<body ms_positioning="gridlayout">
<asp:label id="label1" runat="server"></asp:label>
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
<input type="file" name="file" width="600"><br><br>
<asp:button id="button1" runat="server"></asp:button>
</form>
</body>
</html>

后代码:thumbnail.aspx.vb

imports system
imports system.web
imports system.drawing
imports system.io
imports system.drawing.imaging

public class thumbnail
inherits system.web.ui.page
protected withevents label1 as system.web.ui.webcontrols.label
protected withevents button1 as system.web.ui.webcontrols.button

#region " web form designer generated code "

'this call is required by the web form designer.
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()

end sub

private sub page_init(byval sender as system.object, byval e as system.eventargs) handles mybase.init
'codegen: this method call is required by the web form designer
'do not modify it using the code editor.
initializecomponent()
end sub

#end region

private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
label1.text = "<h3>在asp.net里轻松实现缩略图</h3>"
button1.text = "上载并显示缩略图"
end sub

private sub button1_click(byval sender as object, byval e as system.eventargs) handles button1.click
dim myfilecoll as httpfilecollection = httpcontext.current.request.files
dim mypostedfile as httppostedfile = myfilecoll.item(0)
if lcase(mypostedfile.contenttype.tostring()).indexof("image") < 0 then
response.write("无效的图形格式。")
exit sub
end if
getthumbnail(mypostedfile.filename, 100, 100, mypostedfile.contenttype.tostring(),_
false, mypostedfile.inputstream)
end sub

private function getimagetype(byval strcontenttype) as system.drawing.imaging.imageformat
select case (strcontenttype.tostring().tolower())
case "image/pjpeg"
getimagetype = system.drawing.imaging.imageformat.jpeg
case "image/gif"
getimagetype = system.drawing.imaging.imageformat.gif
case "image/bmp"
getimagetype = system.drawing.imaging.imageformat.bmp
case "image/tiff"
getimagetype = system.drawing.imaging.imageformat.tiff
case "image/x-icon"
getimagetype = system.drawing.imaging.imageformat.icon
case "image/x-png"
getimagetype = system.drawing.imaging.imageformat.png
case "image/x-emf"
getimagetype = system.drawing.imaging.imageformat.emf
case "image/x-exif"
getimagetype = system.drawing.imaging.imageformat.exif
case "image/x-wmf"
getimagetype = system.drawing.imaging.imageformat.wmf
case else
getimagetype = system.drawing.imaging.imageformat.memorybmp
end select
end function

private sub getthumbnail(byval strfilename, byval iwidth, byval iheight, byval strcontenttype, _
byval blngetfromfile, byval imgstream)
dim oimg as image
if blngetfromfile then
oimg = oimg.fromfile(strfilename)
else
oimg = oimg.fromstream(imgstream)
end if
oimg = oimg.getthumbnailimage(iwidth, iheight, nothing, (new intptr()).zero)
dim strguid as string = (new guid()).newguid().tostring().toupper()
dim strfileext as string = strfilename.substring(strfilename.lastindexof("."))
'保存到本地
'oimg.save(server.mappath("images") + "/" + strguid + strfileext, getimagetype(strcontenttype))
'直接输出url文件
'response.redirect("images/" + strguid + strfileext)
'以下显示在屏幕上
response.contenttype = strcontenttype
dim memstream as new memorystream()
' 注意:这里如果直接用 oimg.save(response.outputstream, getimagetype(strcontenttype))
' 对不同的格式可能会出错,比如png格式。
oimg.save(memstream, getimagetype(strcontenttype))
memstream.writeto(response.outputstream)
end sub

end class

c#

using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.io;
using system.drawing.imaging;

namespace emeng.exam
{
/// <summary>
/// thumbnail 的摘要说明。
/// </summary>
public class thumbnail : system.web.ui.page
{
protected system.web.ui.webcontrols.label label1;
protected system.web.ui.webcontrols.button button1;

private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
label1.text = "<h3>在asp.net里轻松实现缩略图</h3>";
button1.text = "上载并显示缩略图";

}

#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.button1.click += new system.eventhandler(this.button1_click);
this.load += new system.eventhandler(this.page_load);

}
#endregion

private void button1_click(object sender, system.eventargs e)
{
httpfilecollection myfilecoll = httpcontext.current.request.files;
httppostedfile mypostedfile = myfilecoll[0];

if (mypostedfile.contenttype.tostring().tolower().indexof("image") < 0)
{
response.write("无效的图形格式。");
return;
}
getthumbnail(mypostedfile.filename, 100, 100,
mypostedfile.contenttype.tostring(), false, mypostedfile.inputstream);
}
private system.drawing.imaging.imageformat getimagetype(object strcontenttype)
{
if ((strcontenttype.tostring().tolower()) == "image/pjpeg")
{
return system.drawing.imaging.imageformat.jpeg;
}
else if ((strcontenttype.tostring().tolower()) == "image/gif")
{
return system.drawing.imaging.imageformat.gif;
}
else if ((strcontenttype.tostring().tolower()) == "image/bmp")
{
return system.drawing.imaging.imageformat.bmp;
}
else if ((strcontenttype.tostring().tolower()) == "image/tiff")
{
return system.drawing.imaging.imageformat.tiff;
}
else if ((strcontenttype.tostring().tolower()) == "image/x-icon")
{
return system.drawing.imaging.imageformat.icon;
}
else if ((strcontenttype.tostring().tolower()) == "image/x-png")
{
return system.drawing.imaging.imageformat.png;
}
else if ((strcontenttype.tostring().tolower()) == "image/x-emf")
{
return system.drawing.imaging.imageformat.emf;
}
else if ((strcontenttype.tostring().tolower()) == "image/x-exif")
{
return system.drawing.imaging.imageformat.exif;
}
else if ((strcontenttype.tostring().tolower()) == "image/x-wmf")
{
return system.drawing.imaging.imageformat.wmf;
}
else
{
return system.drawing.imaging.imageformat.memorybmp;
}
}

private void getthumbnail(string strfilename, int iwidth, int iheight,
string strcontenttype, bool blngetfromfile, system.io.stream imgstream)
{
system.drawing.image oimg;

if (blngetfromfile)
{
oimg = system.drawing.image.fromfile(strfilename);
}
else
{
oimg = system.drawing.image.fromstream(imgstream);
}
oimg = oimg.getthumbnailimage(iwidth, iheight, null, intptr.zero);
string strguid = system.guid.newguid().tostring().toupper();
string strfileext = strfilename.substring(strfilename.lastindexof("."));
response.contenttype = strcontenttype;
memorystream memstream = new memorystream();
oimg.save(memstream, getimagetype(strcontenttype));
memstream.writeto(response.outputstream);
}

}
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表