1

Hello I have problem with my resizing and uploading img to server. Everything was ok, but today tell me friend when he want add img to server, he gets "A generic error occurred in GDI+.".. But in my PC all works fine. So can be problem with IIS? (Two days ago he had some problem so admin change something on server).

Bitmap image = KTEditImage.ResizeImage(new Bitmap(file.PostedFile.InputStream), 360, 360);
image.Save(Server.MapPath("~") + "/Static/Img/Zbozi/" + urlName, ImageFormat.Jpeg);
image.Dispose();
Bitmap smallImage = KTEditImage.ResizeImage(new Bitmap(file.PostedFile.InputStream), 230, 230);                           
smallImage.Save(Server.MapPath("~") + "/Static/Img/Zbozi/Small/" + urlName, ImageFormat.Jpeg);
smallImage.Dispose();

and resize method is

public static Bitmap ResizeImage(Bitmap image, int maxWidth, int maxHeight)
{
    return new Bitmap(image, maxWidth, maxHeight);
}
3
  • I guess it's a privilege related issue. Can you check that the owner of the target folder is the same as the user IIS is running under? Commented May 17, 2011 at 18:33
  • Can I ask how can I check it? Commented May 17, 2011 at 18:36
  • i don't have a Windows machine available right now, but if i'm not mistaken you can right click on the folder, properties, security, advanced and then select the owner tab. You can then see the owner. Commented May 17, 2011 at 18:37

4 Answers 4

7

Grant write permission on the target directory to ASPNET account(Windows XP) or NETWORK SERVICE account(Windows Server 2003/2008/Vista/7),

Sign up to request clarification or add additional context in comments.

2 Comments

If the site is running on IIS 7, the account to grant permissions for depends on the account assigned to the application pool in which the web site is running. It is not always NETWORK SERVICE, and in most cases, NETWORK SERVICE is not the default.
In my case turn out to be a permission issue on the folder where I was creating a dynamic QR code
1

I had something similar to this problem where I was resizing an image and replacing the original with the resized version. I turned out GDI+ Exception was due to the fact that the image that was causing problems was Read Only and was unable to be overwritten.

My goal in the following code is to resize images that exceed a maximum filesize.

for (int i = 0; i < LoadedImgs.Length; i++)
{
    info = new FileInfo(LoadedImgs[i].origImgFullPath);
    double sizeMB = Math.Round(((double)info.Length / 1048576.0), MidpointRounding.AwayFromZero);
    if (sizeMB > (double)numImgMaxSize.Value)
    {
        Bitmap bmpOrigImg = new Bitmap(LoadedImgs[i].origImgFullPath);
        Bitmap bmpResizeImg = null;

        bmpResizeImg = ImageUtilities.Resize(bmpOrigImg, sizeMB);

        #region Save the resized image over the original
        bmpOrigImg.Dispose();                        
        bmpResizeImg.Save(LoadedImgs[i].origImgFullPath);
        bmpResizeImg.Dispose();
        #endregion
     }
}

Also, my resizing algorithm to a specific file size needs tweaking (doesn't include bit compression, etc) but in the name of sharing:

Bitmap origBmp = new Bitmap(image.Width, image.Height);
double ratio = (double)image.Width / (double)image.Height;

double bitDepth = 32.0;//Output BMP default
double newHeight = Math.Sqrt((1024.0 * 1024.0 * 8.0) / (ratio * bitDepth));

int height = (int)Math.Round(newHeight, MidpointRounding.AwayFromZero);
int width = (int)Math.Round(newHeight * ratio);

Comments

0

It is probably a permission issue with saving the file to that path. You need to be sure that the "/Static/Img/Zbozi" and "/Static/Img/Zbozi/Small" directories allow anonymous users to save files.

1 Comment

Allowing anonymous users to write on a folder must be avoided and it imposes security issues.
0

This problem is due to image resource handled by .Net framework, please use GC.Collect(); after code

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.