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

本文关键字:照片 问题 GDI+ net asp | 更新日期: 2024-10-21 18:12:20

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

   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。保存(上传MapPath);

任何建议都将不胜感激!

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

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