John
John

Reputation: 135

A generic error occurred in GDI+. (Only if i try it on the server)

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

Upvotes: 1

Views: 6193

Answers (4)

tek
tek

Reputation: 41

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

Upvotes: 1

Alireza Maddah
Alireza Maddah

Reputation: 5885

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

Upvotes: 7

Saurabh
Saurabh

Reputation: 5727

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

Upvotes: 0

esteuart
esteuart

Reputation: 1423

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.

Upvotes: 0

Related Questions