我在图片上传方面遇到了问题 - GDI +错误。在ASP.net中完成我的最终课程项目。请参阅下面我的图片加载完整代码:
问题行是:newimage.Save(path +“/”+ newImageName + Path.GetExtension(File1));
你能帮我解决这个问题吗?请为初学者程序员清楚解释。提前谢谢。
完整代码:
private void ResizeAndUpload(FileUpload fileUpload1, MainCategory add, string path)
{
path = HttpContext.Current.Request.PhysicalApplicationPath + "Media";
var chars = "abcdefghijklmnopqrstuvwxyz0123456789";
var random = new Random();
var results = new string(Enumerable.Repeat(chars,8).Select(s => s[random.Next(s.Length)]).ToArray());
string File1 = FileUpload1.FileName;
newImageName = results + "_Categories_" + DateTime.Now.ToString("ddMMyyyyhhmmss");
Bitmap original = new Bitmap(fileUpload1.FileContent);
decimal originWidth = original.Width;
decimal originHeight = original.Height;
decimal ratio = originHeight / originWidth;
int newHeight = 300;
decimal newWidth_temp = newHeight / ratio;
int newWidth = Convert.ToInt16(newWidth_temp);
Bitmap newimage = new Bitmap(original,newWidth,newHeight);
Graphics ographics = Graphics.FromImage(newimage);
ographics.SmoothingMode = SmoothingMode.AntiAlias;
ographics.InterpolationMode = InterpolationMode.Default;
ographics.DrawImage(original, 0, 0, newWidth, newHeight);
newimage.Save(path + "/" + newImageName + Path.GetExtension(File1));
original.Dispose();
newimage.Dispose();
ographics.Dispose();
add.MainCatIcon = newImageName + Path.GetExtension(File1)