ASP.NET上傳圖檔自動修改成固定大小

 

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim filename As String = Session("name").ToString & ".jpg"
If FileUpload1.HasFile Then '檢查是否有檔案
Dim fileallow As Boolean = False '是否允許上載
Dim allowextensions() As String = {".jpg"} '設定允許上載的延伸檔名類型
Dim fileextension As String = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower() '取得上載檔案之延伸檔名,並轉換成小寫字母
For i As Integer = 0 To allowextensions.Length - 1
If (fileextension = allowextensions(i)) Then
fileallow = True
Exit For
End If
Next
If fileallow Then
Try
Dim path As String = HttpContext.Current.Request.MapPath("~/uploadfile/") '取得網站根目錄路徑(絕對路徑)

'thefile是一个File Field HTML 控件
FileUpload1.PostedFile.SaveAs(path & Session("name").ToString & "_1.jpg")
Dim MemStream As New MemoryStream()
Dim imgOutput As System.Drawing.Image = System.Drawing.Bitmap.FromFile(path & Session("name").ToString & "_1.jpg")
'修改成80×80大小
Dim imgOutput2 As System.Drawing.Image = imgOutput.GetThumbnailImage(110, 140, Nothing, IntPtr.Zero)
imgOutput2.Save((path & filename), ImageFormat.Jpeg)
imgOutput.Dispose()
imgOutput2.Dispose()

Dim strPath As String = Server.MapPath("~/uploadfile/")
Dim strFiles As _
System.Collections.ObjectModel.ReadOnlyCollection(Of String)
Try
' 刪除檔案。


My.Computer.FileSystem.DeleteFile(path & Session("name").ToString & "_1.jpg")

Catch ex As Exception
' 撰寫您所要的錯誤處理程式。
End Try

'FileUpload1.SaveAs(path & filename) '儲存檔案到磁碟
TextBox46.Text = "~/uploadfile/" & filename
Image1.ImageUrl = TextBox46.Text
Dim info As String = " " '動態建立JavaScript
If (Not Page.ClientScript.IsStartupScriptRegistered("Welcome")) Then '判斷myName是否已被註冊。
Page.ClientScript.RegisterStartupScript(Me.GetType(), "Welcome", info)
End If
Catch ex As Exception
Dim info As String = " " '動態建立JavaScript
If (Not Page.ClientScript.IsStartupScriptRegistered("Welcome")) Then '判斷myName是否已被註冊。
Page.ClientScript.RegisterStartupScript(Me.GetType(), "Welcome", info)
End If
End Try
Else
TextBox46.Text = "不允許上載" & FileUpload1.PostedFile.FileName & "
"
End If

Else
Dim info As String = " " '動態建立JavaScript
If (Not Page.ClientScript.IsStartupScriptRegistered("Welcome")) Then '判斷myName是否已被註冊。
Page.ClientScript.RegisterStartupScript(Me.GetType(), "Welcome", info)
End If
End If


End Sub

arrow
arrow
    全站熱搜

    毛魚 發表在 痞客邦 留言(0) 人氣()