asp.net上传图片并同时生成缩略图

 1<script language="VB" runat="server">   
 2Sub UploadFile(sender As Object, e As EventArgs) 
 3
 4If FileUp.PostedFile.ContentLength = 0 Then   
 5FileInfo.Visible = False   
 6Exit Sub   
 7Else   
 8FileInfo.Visible = True   
 9FDisplay1.Visible = True   
10End If 
11
12FSize.Text ="上传文件大小"+ CStr(FileUp.PostedFile.ContentLength/1024)+"KB"   
13FName.Text = "已上传文件名:"+FileUp.PostedFile.FileName+"<br>"+FName.Text 
14
15'写入数据库   
16on error resume next   
17dim myconn as sqlconnection   
18dim mycomm as sqlcommand   
19dim sql as string   
20dim id as integer   
21Dim image,anewimage As System.Drawing.Image   
22dim width,height,newwidth,newheight as integer   
23Dim callb As System.Drawing.Image.GetThumbnailImageAbort   
24myConn=New sqlconnection(ConfigurationSettings.AppSettings("数据库"))   
25myconn.open()   
26sql="insert into picture (姓名,班级,介绍,属性) values ('"&request.cookies("dgxyl").values("dgxylname")&"','"&request.cookies("dgxyl").values("dgxylbj")&"','"&trim(request("TextBox1"))&"','"&request("r1")&"')"   
27Mycomm=New sqlcommand(sql,myconn)   
28mycomm.executenonquery()   
29myconn.close() 
30
31Dim myCommand As New SqlCommand("select top 1 id from picture order by id desc", myConn)   
32myCommand.Connection.Open()   
33Dim myReader As SqlDataReader=mycommand.executereader()   
34if myReader.Read() then   
35id=myReader("id")   
36end if   
37myconn.close()   
38'保存图片   
39FileUp.PostedFile.SaveAs( Server.MapPath("\classpic\")&cstr(id)&".jpg" )   
40'生成缩略图   
41image=System.Drawing.Image.FromFile(Server.MapPath("/classpic/"+cstr(id)+".jpg"))   
42width=image.Width   
43height=image.height   
44if width>height then   
45newwidth=250   
46newheight=image.height/image.Width*newwidth   
47else   
48newheight=250   
49newwidth=image.Width/image.height*newheight   
50end if   
51response.write("id="+cstr(id)+"width="+cstr(Width)+";height="+cstr(height)+" ")   
52response.write("newwidth="+cstr(newwidth)+";newheight="+cstr(newheight)+"<br>") 
53
54aNewImage=image.GetThumbnailImage(newwidth,newheight,callb,new System.IntPtr())   
55aNewImage.Save(Server.MapPath("/smallpic/"+cstr(id)+".jpg"))   
56image.Dispose() 
57
58  
59Dim FileSplit() As String = Split( FileUp.PostedFile.FileName, "\" )   
60Dim FileName As String = FileSplit(FileSplit.Length-1)   
61Dim Exts() As String = Split( FileName, "." )   
62Dim Ext As String = LCase(Exts(Exts.Length-1)) 
63
64FDisplay.Text = "<A Target='_blank' HREF='/classpic/"&cstr(id)&".jpg"& "'>查看上传文件</A>"   
65FDisplay1.text="<a href='/picture/default.asp?bj="&cstr(request.cookies("dgxyl").values("dgxylbj"))&"'>返回</a>"   
66End Sub   
67</script>
 1<script language="JavaScript">   
 2<!--   
 3var requestsubmitted=false;   
 4function guestbook_Validator(theForm)   
 5{   
 6//检查是否从新提交   
 7if (requestsubmitted==true){   
 8alert("你已经提交了留言,请等待服务器应答!");   
 9return(false);   
10}   
11requestsubmitted=true; 
12
13return (true);   
14}   
15//-->   
16</script>
 1<html>
 2<body bgcolor="White">
 3<h3 align="center">请正确填写下面各项</h3>
 4<hr/>
 5<div id="FileInfo" runat="server" visible="False">
 6<asp:label id="FSize" runat="server"></asp:label><br/>
 7<asp:label id="FName" runat="server"></asp:label><br/>
 8<asp:label id="FDisplay" runat="server"></asp:label>
 9<asp:label id="FDisplay1" runat="server"></asp:label>
10</div>
11<form enctype="multipart/form-data" onsubmit="return guestbook_Validator(this)" runat="server">   
12上传文件   
13<input id="FileUp" runat="server" size="20" type="File"/><br/>   
14图片属性:<input ```"="" checked="" dgxyl").values("dgxylbj")="" name="R1" type="radio" value="```
15=request.cookies("/>本班<input name="R1" type="radio" value="全校"/>全校(本班则只在本班显示,全校则在全校显示)<p>   
16图片说明:<br/>
17<asp:textbox height="141px" id="TextBox1" runat="server" width="233px">
18</asp:textbox>
19<asp:requiredfieldvalidator controltovalidate="TextBox1" errormessage="请输入图片说明" id="RequiredFieldValidator1" runat="server">
20</asp:requiredfieldvalidator><br/>
21<asp:button id="Upload" onclick="UploadFile" runat="server" text="上传图片"></asp:button>
22</p></form>
23<hr/>
24<p>注意:</p>
25<ol>
26<li><b>严禁上传污染环境的照片,否则账号将被删除!</b></li>
27<li><font color="#808000"><b>请详细填写照片说明,图片说明不详细将会被视为乱传图片,将会被删除!</b></font></li>
28</ol>
29<p> </p>
30</body>
31</html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus