Saajid Akram
Saajid Akram

Reputation: 169

GDI+ issue in uploading photos using asp.net with C#

I'm using the following code to resize and upload images to my server:

   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();

It works just fine in most of my projects, however, recently I've tried to use it again but this time it's not working and it keeps giving me "a generic error occurred in gdi+", I realized that when I provide the function with full path like in:"C:/Users/Saajid-PC/Desktop/NewProject/MAXimages/upload" the function will work just as I expected, but if I send this path: "~/MAXimages" the error will be generated ... the saving part of this code is where this error being issued

newBMP.Save(uploadingMapPath);

Any advice will be appreciated !

Upvotes: 1

Views: 130

Answers (1)

sh1rts
sh1rts

Reputation: 1884

You need to use Server.MapPath on a location relative to the site (like ~/MAXimages) because the GDI+ method is expecting a full UNC or drive-mapped path like c:\folder\subfolder ..

Upvotes: 1

Related Questions