我想讓用戶將圖像上傳到我的Web應用程序中的文件。但是,在保存之前,我想將圖像調整爲指定的大小。我在互聯網上找到了代碼來做我想做的事情,但我無法適應我的需求。這裏是我遇到的麻煩的部分:在ASPNET中調整圖像大小VB
' Resize Image Before Uploading to DataBase
Dim imageToBeResized As System.Drawing.Image = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream)
Dim imageHeight As Integer = imageToBeResized.Height
Dim imageWidth As Integer = imageToBeResized.Width
Dim maxHeight As Integer = 240
Dim maxWidth As Integer = 320
imageHeight = (imageHeight * maxWidth)/imageWidth
imageWidth = maxWidth
If imageHeight > maxHeight Then
imageWidth = (imageWidth * maxHeight)/imageHeight
imageHeight = maxHeight
End If
Dim bitmap As New Bitmap(imageToBeResized, imageWidth, imageHeight)
Dim stream As System.IO.MemoryStream = New MemoryStream()
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
stream.Position = 0
Dim image As Byte() = New Byte(stream.Length) {}
stream.Read(image, 0, image.Length)
而不是上傳到數據庫我想圖像到一個文件夾中保存我的Web應用程序。我遇到的問題是,VB不會讓我保存一個字節()到一個文件夾。不知道我可以安全地改變以適應我的目的。爲什麼需要首先將其更改爲byte()?
爲什麼不使用Bitmap.Save(文件名)或使用寫入文件的流? – mhttk