使用带有C#的asp.net上传照片时出现GDI +问题

时间:2013-12-18 01:52:56

标签: c# asp.net generics gdi+ image-uploading

我正在使用以下代码调整大小并将图像上传到我的服务器:

   Bitmap originalBMP = new Bitmap(uploader.FileContent);

   int origWidth = originalBMP.Width;
   int origHeight = originalBMP.Height;
   double sngRatio = (double)origWidth / origHeight;
   int newHeight = (int)Math.Ceiling(newWidth / sngRatio);

   Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
   Graphics oGraphics = Graphics.FromImage(newBMP);

   oGraphics.SmoothingMode = SmoothingMode.AntiAlias;
   oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
   oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);

   string uploadingMapPath = Path.Combine(serverMapPath, imageNewLocation);
   newBMP.Save(uploadingMapPath);

   originalBMP.Dispose();
   newBMP.Dispose();
   oGraphics.Dispose();

它在我的大多数项目中运行得很好,但是,最近我尝试再次使用它,但这次它不起作用并且它一直给我“gdi +中发生了一般错误”,我意识到当我提供时具有完整路径的函数如:"C:/Users/Saajid-PC/Desktop/NewProject/MAXimages/upload"该函数将按照我的预期工作,但如果我发送此路径:"~/MAXimages"将生成错误... 此代码的保存部分是发出此错误的地方

  

newBMP.Save(uploadingMapPath);

任何建议都将不胜感激!

1 个答案:

答案 0 :(得分:1)

您需要在相对于网站的位置使用Server.MapPath(例如〜/ MAXimages),因为GDI +方法需要完整的UNC或驱动器映射路径,如c:\ folder \ subfolder ..