Grahame A
Grahame A

Reputation: 3953

A generic error occurred in GDI+, possible server.mappath issue

I've narrowed down the issue I'm having to this block of code, where I am resizing an uploaded image and saving it. This works fine on my local machine, but when I run the site on the server, I get a generic GDI+ error that's coming from the "thumbnail.Save" call.

if(fup_displayPicUpload.HasFile)
            {
                string imageDir = Server.MapPath("./images/");

                if (!Directory.Exists(imageDir + username))
                {
                    Directory.CreateDirectory(imageDir + username);
                    lbl_profileMessage.ForeColor = Color.Yellow;
                    lbl_profileMessage.Text = "Created User Folder";
                }
                String userFolder = imageDir + username + "/";

                using (System.Drawing.Image originalPhoto = new Bitmap(new MemoryStream(fup_displayPicUpload.FileBytes)))
                {
                    System.Drawing.Image thumbnail = originalPhoto.GetThumbnailImage(300, 300, Abort, IntPtr.Zero);
                    thumbnail.Save(userFolder + "displaypicture.jpg", ImageFormat.Jpeg);
                }

                displayPictureUrl = "/images/" + username + "/displaypicture.jpg";

            }

Upvotes: 0

Views: 755

Answers (1)

Mike
Mike

Reputation: 3460

The most common cause of this is an access denied error to the directory that you are trying to save the image to. Make sure that the user that the application is running as has write access to the destination directory.

Upvotes: 1

Related Questions