我有以下编码来调整图像大小并将其保存到我的虚拟文件夹“vicpic/scimages”
if (FileUpload5.PostedFile != null)
{
if (FileUpload5.PostedFile.ContentLength > (1024 * 1024))
{
Label4.Text = "Upload status: The file has to be less than 1 MB. Please resize your photo and than upload it again.";
}
else
{
System.Drawing.Image imageToBeResized = System.Drawing.Image.FromStream(FileUpload5.PostedFile.InputStream);
int imageHeight = imageToBeResized.Height;
int imageWidth = imageToBeResized.Width;
int maxHeight = 660;
int maxWidth = 560;
imageHeight = (imageHeight * maxWidth) / imageWidth;
imageWidth = maxWidth;
if (imageHeight > maxHeight)
{
imageWidth = (imageWidth * maxHeight) / imageHeight;
imageHeight = maxHeight;
}
Bitmap bitmap = new Bitmap(imageToBeResized, imageWidth, imageHeight);
System.IO.MemoryStream stream = new MemoryStream();
// bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Position = 0;
byte[] image = new byte[stream.Length + 1];
stream.Read(image, 0, image.Length);
string FileName = Path.GetFileName(FileUpload5.PostedFile.FileName);
//Save files to disk
string extension = Path.GetExtension(FileUpload5.PostedFile.FileName);
//Path.Combine(Server.MapPath("~/vicpic/scimages"), imageName);
string imagename = DropDownList2.SelectedItem.Text + "4" + extension;
bitmap.Save(Server.MapPath("~/vicpic/scimages/") + imagename);
bitmap.Dispose();
imageToBeResized.Dispose();
GC.Collect();
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
con.Dispose();
Label4.Text = "Upload status:Successfully.";
}
}
}
但它显示 GDI+ 中发生一般错误
谁能建议问题出在哪里?
我想将图像保存在我的主机服务器上的“vicpic/scimages”文件夹中。它是虚拟目录,我还授予了该目录所需的所有权限。