上傳圖片並產生縮圖

來源:互聯網
上載者:User
上傳|上傳圖片|縮圖 Sub upimages()
Dim iname As String
If Not (fileup.PostedFile Is Nothing) Then
Dim namestr1 As String = (fileup.PostedFile.FileName)
If LCase(fileup.PostedFile.ContentType.ToString()) = "image/pjpeg" Or LCase(fileup.PostedFile.ContentType.ToString()) = "image/jpg" Then
Dim j As Integer = namestr1.LastIndexOf(".")
Dim newname As String = namestr1.Substring(j) '圖片得到後輟名
iname = CStr(Now.ToFileTimeUtc) '隨機的檔案名稱(不會重復)
Dim newnames As String = iname + newname '重新組合檔案名稱
Dim i As Integer = namestr1.LastIndexOf("\") + 1
Dim namestr As String = namestr1.Substring(i)
fileup.PostedFile.SaveAs(Server.MapPath(imagelocal) + "\" + newnames) '儲存檔案到imagelocal檔案夾
'產生縮略圖()
Dim image, sImage As System.Drawing.Image
image = System.Drawing.Image.FromStream(fileup.PostedFile.InputStream)'得到原圖
Dim width As Decimal = image.Width過且過'得到原圖的寬
Dim height As Decimal = image.Height'得到原圖的高
Dim newwidth, newheight As Integer
'設定縮圖的高和寬
If (width > height) Then
newwidth = 150
newheight = CInt(height / width * 150)
Else
newheight = 150
newwidth = CInt(width / height * 150)
End If
sImage = image.GetThumbnailImage(newwidth, newheight, Nothing, IntPtr.Zero)
Dim x As Integer = sImage.Width / 2 - 30
Dim y As Integer = sImage.Height - 20
Dim output As Bitmap = New Bitmap(sImage)
Dim g As Graphics = Graphics.FromImage(output)
' 給縮略圖加上版權資訊()
Dim fonts As New Font("Courier New", 9)
g.DrawString("版權資訊", fonts, New SolidBrush(Color.Red), x, y)
output.Save(Server.MapPath("Simagelocal") + "\s_" + newnames, System.Drawing.Imaging.ImageFormat.Jpeg)
'儲存縮略圖到Simagelocal檔案夾
Image1.Visible=true;
Image1.ImageUrl = "Simagelocal" + "\s_" + newnames

Else
Label1.Text = "請選擇jpg類型的圖片"

End If
End If
End Sub





相關文章

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。