Asp.net 在ASPNET VB中调整图像大小

Asp.net 在ASPNET VB中调整图像大小,asp.net,vb.net,image,Asp.net,Vb.net,Image,我想让用户上传一个图像到我的web应用程序中的文件。但是,在保存之前,我想将图像大小调整为指定的大小。我在互联网上找到了可以做我想做的事情的代码,但是我在适应我的需要方面遇到了困难。以下是我遇到的问题: ' Resize Image Before Uploading to DataBase Dim imageToBeResized As System.Drawing.Image = System.Drawing.Image.FromStream(FileUpload1.P

我想让用户上传一个图像到我的web应用程序中的文件。但是,在保存之前,我想将图像大小调整为指定的大小。我在互联网上找到了可以做我想做的事情的代码,但是我在适应我的需要方面遇到了困难。以下是我遇到的问题:

' 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()保存到文件夹中。不确定我可以安全地改变什么以适应我的目的。为什么首先需要将其更改为byte()。shift to BYTE()用于将其保存为SQL BLOB

基本上,与其在最后减少内存流,不如减少文件流,并在文件流上调用BitMap.Save(stream…)


但请记住,运行ASP.NET/IIS的帐户需要对您试图保存所述文件的文件夹具有写入权限。

您需要在此处修改:

 Dim bitmap As New Bitmap(imageToBeResized, imageWidth, imageHeight)
 bitmap.Save("MyFile.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
移除

Dim stream As System.IO.MemoryStream = New MemoryStream()
stream.Position = 0
Dim image As Byte() = New Byte(stream.Length) {}
stream.Read(image, 0, image.Length)

为什么不直接使用位图。保存(文件名)或使用写入文件的流呢?谢谢!这个网站就是炸弹!