asp.net 上传图片并同时生成缩略图的代码
程序员文章站
2024-03-08 10:37:40
复制代码 代码如下:<%@ page language="c#" responseencoding="gb2312" %> <%@ import name...
复制代码 代码如下:
<%@ page language="c#" responseencoding="gb2312" %>
<%@ import namespace="system" %>
<%@ import namespace="system.io" %>
<%@ import namespace="system.drawing" %>
<%@ import namespace="system.drawing.imaging" %>
<script runat="server">
void page_load(object sender, eventargs e)
{
if (!page.ispostback)
{
imgpreview.visible = false;
}
}
void getthumbnailimage(int width, int height, string strinfo, int left, int right)
{
string file = "uploads/" + uploadfile.postedfile.filename.substring(uploadfile.postedfile.filename.lastindexof('\\') + 1);
string newfile = "uploads/" + uploadfile.postedfile.filename.substring(uploadfile.postedfile.filename.lastindexof('\\') + 1) + ".jpg";
string stradd = strinfo;
system.drawing.image oldimage = system.drawing.image.fromfile(server.mappath(file));
system.drawing.image thumbnailimage =
oldimage.getthumbnailimage(width, height, new system.drawing.image.getthumbnailimageabort(thumbnailcallback), intptr.zero);
response.clear();
bitmap output = new bitmap(thumbnailimage);
graphics g = graphics.fromimage(output);
g.drawstring(stradd, new font("courier new", 14), new solidbrush(color.red), left, right);
output.save(server.mappath(newfile), system.drawing.imaging.imageformat.jpeg);
response.contenttype = "image/gif";
imgpreview.visible = true;
imgpreview.imageurl = newfile;
}
bool thumbnailcallback()
{
return true;
}
void button_click(object sender, eventargs e)
{
int width, height, left, right;
string straddinfo = txtaddinfo.text;
width = int32.parse(txtwidth.text);
height = int32.parse(txtheight.text);
left = int32.parse(txtleft.text);
right = int32.parse(txtright.text);
if (!(uploadfile.postedfile.contentlength > 0))
{
lblerrinfo.text = "没有选择文件";
}
else
{
string path = server.mappath("./uploads/" + uploadfile.postedfile.filename.substring(uploadfile.postedfile.filename.lastindexof('\\') + 1));
if (file.exists(path))
{
lblerrinfo.text = "已经有同名文件";
}
else
{
uploadfile.postedfile.saveas(path);
getthumbnailimage(width, height, straddinfo, left, right);
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="head1" runat="server">
<title>上传图片并生成缩略图</title>
</head>
<body>
<form id="form1" method="post" enctype="multipart/form-data" runat="server">
<p>
<input id="uploadfile" type="file" runat="server" />
<asp:label id="lblerrinfo" runat="server" forecolor="red"></asp:label>
</p>
<p>
width:<asp:textbox id="txtwidth" runat="server" width="40px">100</asp:textbox>
height:<asp:textbox id="txtheight" runat="server" width="40px">150</asp:textbox>
</p>
<p>
添加信息:<asp:textbox id="txtaddinfo" runat="server"> aspxboy.com</asp:textbox>
</p>
<p>
信息位置:left:<asp:textbox id="txtleft" runat="server" width="40px">10</asp:textbox>
right:<asp:textbox id="txtright" runat="server" width="40px">135</asp:textbox>
</p>
<p>
<input id="button" type="button" value="上传生成所略图" onserverclick="button_click" runat="server" />
</p>
<p>
<asp:image id="imgpreview" runat="server"></asp:image>
</p>
<!-- insert content here -->
</form>
</body>
</html>
asp.net(vbscript)上传图片并同时生成缩略图:
复制代码 代码如下:
<script language="vb" runat="server">
sub uploadfile(sender as object, e as eventargs)
if fileup.postedfile.contentlength = 0 then
fileinfo.visible = false
exit sub
else
fileinfo.visible = true
fdisplay1.visible = true
end if
fsize.text ="上传文件大小"+ cstr(fileup.postedfile.contentlength/1024)+"kb"
fname.text = "已上传文件名:"+fileup.postedfile.filename+"<br>"+fname.text
"写入数据库
on error resume next
dim myconn as sqlconnection
dim mycomm as sqlcommand
dim sql as string
dim id as integer
dim image,anewimage as system.drawing.image
dim width,height,newwidth,newheight as integer
dim callb as system.drawing.image.getthumbnailimageabort
myconn=new sqlconnection(configurationsettings.appsettings("数据库"))
myconn.open()
sql="insert into picture (姓名,班级,介绍,属性) values (""&request.cookies("dgxyl").values("dgxylname")&"",""&request.cookies("dgxyl").values("dgxylbj")&"",""&trim(request("textbox1"))&"",""&request("r1")&"")"
mycomm=new sqlcommand(sql,myconn)
mycomm.executenonquery()
myconn.close()
dim mycommand as new sqlcommand("select top 1 id from picture order by id desc", myconn)
mycommand.connection.open()
dim myreader as sqldatareader=mycommand.executereader()
if myreader.read() then
id=myreader("id")
end if
myconn.close()
"保存图片
fileup.postedfile.saveas( server.mappath("\classpic\")&cstr(id)&".jpg" )
"生成缩略图
image=system.drawing.image.fromfile(server.mappath("/classpic/"+cstr(id)+".jpg"))
width=image.width
height=image.height
if width>height then
newwidth=250
newheight=image.height/image.width*newwidth
else
newheight=250
newwidth=image.width/image.height*newheight
end if
response.write("id="+cstr(id)+"width="+cstr(width)+";height="+cstr(height)+" ")
response.write("newwidth="+cstr(newwidth)+";newheight="+cstr(newheight)+"<br>")
anewimage=image.getthumbnailimage(newwidth,newheight,callb,new system.intptr())
anewimage.save(server.mappath("/smallpic/"+cstr(id)+".jpg"))
image.dispose()
dim filesplit() as string = split( fileup.postedfile.filename, "\" )
dim filename as string = filesplit(filesplit.length-1)
dim exts() as string = split( filename, "." )
dim ext as string = lcase(exts(exts.length-1))
fdisplay.text = "<a target="_blank" href="/classpic/"&cstr(id)&".jpg"& "">查看上传文件</a>"
fdisplay1.text="<a href="/picture/default.asp?bj="&cstr(request.cookies("dgxyl").values("dgxylbj"))&"">返回</a>"
end sub
</script>
<script language=javascript>
<!--
var requestsubmitted=false;
function guestbook_validator(theform)
{
//检查是否从新提交
if (requestsubmitted==true){
alert("你已经提交了留言,请等待服务器应答!");
return(false);
}
requestsubmitted=true;
return (true);
}
//-->
</script>
<html>
<body bgcolor=white>
<h3 align="center">请正确填写下面各项</h3>
<hr></h3>
<div id="fileinfo" visible="false" runat="server">
<asp:label id="fsize" runat="server"/><br>
<asp:label id="fname" runat="server"/><br>
<asp:label id="fdisplay" runat="server"/>
<asp:label id="fdisplay1" runat="server"/>
</div>
<form enctype="multipart/form-data" onsubmit="return guestbook_validator(this)" runat="server">
上传文件
<input type="file" id="fileup" runat="server" size="20"><br>
图片属性:<input type="radio" value="<%=request.cookies("dgxyl").values("dgxylbj")%>" name="r1" checked>本班<input type="radio" value="全 校" name="r1">全校(本班则只在本班显示,全校则在全校显示)<p>
图片说明:<br>
<asp:textbox id="textbox1" runat="server" width="233px" height="141px">
</asp:textbox>
<asp:requiredfieldvalidator id="requiredfieldvalidator1" runat="server" controltovalidate="textbox1" errormessage="请输入图片说明">
</asp:requiredfieldvalidator><br>
<asp:button id="upload" onclick="uploadfile" text="上传图片" runat="server"/>
</form>
<hr>
<p>注意:</p>
<ol>
<li><b>严禁上传污染环境的照片,否则账号将被删除!</b></li>
<li><font color="#808000"><b>请详细填写照片说明,图片说明不详细将会被视为乱传图片,将会被删除!</b></font></li>
</ol>
</body>
</html>
上一篇: asp.net伪静态配置备忘