国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

在ASP.NET里輕松實現縮略圖

2024-07-10 12:57:12
字體:
來源:轉載
供稿:網友
,歡迎訪問網頁設計愛好者web開發。以前,在頁面上實現縮略圖必須借助第三方組件?,F在,有了.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);
}

}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁晋县| 临邑县| 北川| 崇阳县| 视频| 陇南市| 德州市| 桃园市| 洮南市| 平潭县| 金堂县| 息烽县| 亚东县| 岳阳市| 高唐县| 白城市| 万盛区| 上栗县| 眉山市| 三河市| 江永县| 肥乡县| 潍坊市| 栾川县| 墨脱县| 观塘区| 五寨县| 密云县| 和顺县| 锦州市| 文水县| 双柏县| 莱阳市| 双流县| 南涧| 富锦市| 上饶市| 刚察县| 航空| 吉隆县| 萍乡市|