asp.net上傳圖片並同時生成縮圖 (轉)

gugu99發表於2008-01-09
asp.net上傳圖片並同時生成縮圖 (轉)[@more@]

<BR>Sub File(sender As , e As EventArgs)</P> <P>If FileUp.PostedFile.ContentLength = 0 Then<BR>FileInfo.Visible = False<BR>Exit Sub<BR>Else<BR>FileInfo.Visible = True<BR>FDisplay1.Visible = True<BR>End If</P> <P>FSize.Text ="大小"+ CStr(FileUp.PostedFile.ContentLength/1024)+"KB"<BR>FName.Text = "已上傳檔名:"+FileUp.PostedFile.FileName+"<br>"+FName.Text</P> <P>'寫入<BR>on error resume next<BR>dim myconn as connection<BR>dim mycomm as sqlcommand<BR>dim sql as string<BR>dim id as integer<BR>Dim image,anewimage As System.Drawing.Image<BR>dim width,height,newwidth,newheight as integer<BR>Dim callb As System.Drawing.Image.GetThumbnailImageAbort<BR>myConn=New sqlconnection(ConfigurationSettings.AppSettings("資料庫"))<BR>myconn.open()<BR>sql="insert into picture (姓名,班級,介紹,屬性) values ('"&request.("dgxyl").values("dgxylname")&"','"&request.cookies("dgxyl").values("dgxylbj")&"','"&trim(request("TextBox1"))&"','"&request("r1")&"')"<BR>Mycomm=New sqlcommand(sql,myconn)<BR>mycomm.executenonquery()<BR>myconn.close()</P> <P>Dim myCommand As New SqlCommand(" top 1 id from picture order by id desc", myConn)<BR>myCommand.Connection.Open()<BR>Dim myReader As SqlDataReader=mycommand.executereader()<BR>if myReader.Read() then<BR>id=myReader("id")<BR>end if<BR>myconn.close()<BR>'儲存圖片<BR>FileUp.PostedFile.SaveAs( Server.MapPath("classpic")&cstr(id)&".jpg" )<BR>'生成縮圖<BR>image=System.Drawing.Image.FromFile(Server.MapPath("/classpic/"+cstr(id)+".jpg"))<BR>width=image.Width<BR>height=image.height<BR>if width>height then<BR>newwidth=250<BR>newheight=image.height/image.Width*newwidth<BR>else<BR>newheight=250<BR>newwidth=image.Width/image.height*newheight<BR>end if<BR>response.write("id="+cstr(id)+"width="+cstr(Width)+";height="+cstr(height)+"&nbsp; ")<BR>response.write("newwidth="+cstr(newwidth)+";newheight="+cstr(newheight)+"<br>")</P> <P>aNewImage=image.GetThumbnailImage(newwidth,newheight,callb,new System.IntPtr())<BR>aNewImage.Save(Server.MapPath("/smallpic/"+cstr(id)+".jpg"))<BR>image.Dispose()</P> <P><BR>Dim FileSplit() As String = Split( FileUp.PostedFile.FileName, "" )<BR>Dim FileName As String = FileSplit(FileSplit.Length-1)<BR>Dim Exts() As String = Split( FileName, "." )<BR>Dim Ext As String = LCase(Exts(Exts.Length-1))</P> <P>FDisplay.Text = "<A Target='_blank' HREF='/classpic/"&cstr(id)&".jpg"& "'>檢視上傳檔案</A>"<BR>FDisplay1.text="<a href='/picture/default.</A>?bj="&cstr(request.cookies("dgxyl").values("dgxylbj"))&"'>返回</a>"<BR>End Sub<BR>




請正確填寫下面各項





 

 


 


上傳檔案


圖片屬性:" name="R1" checked>本班全校(本班則只在本班顯示,全校則在全校顯示)


圖片說明:



 






注意:



     
  1. 嚴禁上傳汙染環境的照片,否則賬號將被刪除!

  2.  
  3. 請詳細填寫照片說明,圖片說明不詳細將會被視為亂傳圖片,將會被刪除!


 



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-996970/,如需轉載,請註明出處,否則將追究法律責任。

相關文章