11
[ExternalException (0x80004005): A generic error occurred in GDI+.]
   IpitchitImageHandler.Data.ImageRepository.AddNewTempImage(Stream image, String extension, Guid PageId, Guid ImageId, ImageTransformCollection toDoTransforms) +1967
   IpitchitImageHandler.Data.ImageRepository.AddNewTempImage(Stream image, String extension, Guid PageId, Guid ImageId) +85
   IpitchitWeb.Sell.Controls.UploadImagesSubstep.UploadImages(Object sender, EventArgs e) in F:\Documents and Settings\Vjeran\My Documents\Visual Studio 2008\Projects\Ipitchit\IpitchitWeb\Sell\Controls\UploadImagesSubstep.ascx.cs:88
   System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +79
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

my code is:

public void AddNewTempImage(Stream image, string extension, Guid PageId, Guid ImageId, 
    ImageTransformCollection toDoTransforms)
{
    //mapping steam to memory stream so it does support the seek

    MemoryStream targetStream = new MemoryStream(ReadStream(image, 1024));
    Image ImageToTransform=null;
    Image transformedImage = null;
    string storagePath = ImageTransformManager.Config.StorageServerPhysicalPath;
    Uri storageUrl = new Uri(ImageTransformManager.Config.StorageServerUrl);

    //string TempPath = Path.Combine(storagePath, GenerateFileName(extension));
    //SaveStream(TempPath, image);
    //File.WriteAllBytes(TempPath, ReadStream(image, 1024));

    if (!HttpContext.Current.User.Identity.IsAuthenticated)
        throw new Exception("Nonauthenticated users image submition is not supported");

        try
        {            
            foreach (ImageTransform transform in toDoTransforms)
            {
            ImageRepositoryTempImage newimage = new ImageRepositoryTempImage();
            newimage.ImageGuid = ImageId;
            newimage.PageGuid = PageId;
            newimage.CreatedBy = HttpContext.Current.User.Identity.Name;
            newimage.CreatedDate = DateTime.UtcNow;
            newimage.Format = transform.OutputType;
            newimage.Width = transform.Width;
            newimage.Height = transform.Height;
            newimage.Watermark = transform.UseWaterMark;
            string filename = GenerateFileName(transform.OutputType);
            string fullStoragePath = Path.Combine(storagePath, Path.Combine(transform.StorageFolder, filename));
            string fullStorageUrl = CombineUri(storageUrl, Path.Combine(transform.StorageFolder, filename));
            newimage.PhysicalStoragePath = fullStoragePath;
            newimage.StoragePath = fullStorageUrl;
            CheckOrAddImageTransform(transform);
            var ImgRepTransform = GetTransformation(transform);
            newimage.ImageRepositoryTransformation = ImgRepTransform;
            newimage.TransformId = ImgRepTransform.Id;

            Bitmap uploaded = new Bitmap(image);

            ImageToTransform = (Image)uploaded.Clone();
            uploaded.Dispose();
            transformedImage = transform.Transform(ImageToTransform);
            AddNewTempImage(newimage);
            //adding named watermark and transformation
            string wname = ImageTransformManager.Config.WaterMarkName;
            string wpath = ImageTransformManager.Config.WaterMarkPath;
            ChechOrAddWaterMark(wname, wpath);


            if (!(string.IsNullOrEmpty(wname) && string.IsNullOrEmpty(wpath)))
                newimage.ImageRepositoryWaterMark = GetWatermark(wname, wpath);

            transformedImage.Save(fullStoragePath, GetFormat(newimage.Format));
            }
        }
        catch (System.Exception ex)
        {
            ErrorHandling.LogErrorEvent("Add new temp image method", ex);
            throw ex;
        }
        finally
        {
            //File.Delete(TempPath);
            if (ImageToTransform!=null)
                ImageToTransform.Dispose();
            image.Dispose();
            targetStream.Dispose();
            if (transformedImage != null)
            transformedImage.Dispose();
        }

}

On my local machine everything works - but still happens.. on server (2003) - i have folder permissions .. and everything...

Cœur
  • 37,241
  • 25
  • 195
  • 267
Vjeran
  • 141
  • 1
  • 1
  • 6

11 Answers11

27

I hate that error with a passion. Generic Error is possibly the most useless error description ever written.

When I've encountered it the problem as always been related to file IO.

Here is the list of fixes I keep in my notes- Not sure if these apply but they usually do the trick for me.

  • Check File path
    • Make sure that the parent directory exists
    • Ensure that path includes both the filename and extension
    • Use server.MapPath() to create the path
  • Make sure that the file isn't being written back to it's source. Close and reopen the stream if necessary.

My apologies if i stole this list from somewhere else. It has been in my notebook for awhile and I can't remember where it came from.

Kelly Robins
  • 7,168
  • 6
  • 43
  • 66
  • It was indeed a missing path. – sabbour Aug 11 '10 at 11:01
  • 1
    +1 for hating with a passion! I also had this error for a missing path. – Curtis Jan 24 '12 at 09:26
  • +1 Indeed, it was my file path as well. The path to the directory was correct but I had failed to put the filename into the path. – Travis J Mar 14 '13 at 00:14
  • 1
    This worked for me: http://www.hanselman.com/blog/TheWeeklySourceCode50ALittleOnAGenericErrorOccurredInGDIAndTroubleGeneratingImagesOnWithASPNET.aspx (It creates an intermediary stream from the image, before writing it to the Response stream) – Cosmin Apr 16 '13 at 12:41
  • "Generic Error is possibly the most useless error description ever written", you obviously never came across "Method '~' of object '~' failed" issued by COM. – Martin Brown Jan 21 '15 at 10:27
  • @MartinBrown Yeah... I made that comment at the blissful point in my career before I had dealt with COM or Oracle. Those were such happy times. – Kelly Robins Jan 22 '15 at 01:13
1

Everyone here (and on this site) has discussed this as due to a permissions error. I've stumbled over another flavor: memory. I ran out of memory and started receiving this error as well. So keep that in mind as another potential source of error.

In my particular case I was running in a .NET application, and running against a number of threads (few enough that I wasn't being held back too much by the GDI+ process-wide lock). Adding a "GC.Collect()" after major tasks finished didn't seem to affect the speed much, but did completely get rid of the out of memory errors.

Gordon
  • 3,012
  • 2
  • 26
  • 35
0

Do not use direct path

    System.Drawing.Image img = Base64ToImage(_ImagePath);
    img.Save(_attachmentPath, ImageFormat.Jpeg);

Use Server.MapPath and it will create Directory If needed

    System.Drawing.Image img = Base64ToImage(_ImagePath);
    img.Save(Server.MapPath(_attachmentPath), ImageFormat.Jpeg);
Brian Webster
  • 30,033
  • 48
  • 152
  • 225
sam
  • 45
  • 1
  • 5
0

I just had an similar problem (same exception) on my hosted website: It turned out to be a permissions issue, the ASP.NET account was trying to read image files from a directory outside its available scope.

I suggest you double-check the permissions as well as the <trust> element in web.config, it should be set to "Medium" or higher depending on where the files are located.

Jakob Gade
  • 12,319
  • 15
  • 70
  • 118
0

This one followed me for a long time. Yes you can check permissions but what you also should do is to dispose of your bitmap correctly.

bitmap.Dispose();
CodeSpeaker
  • 810
  • 8
  • 22
0

i agree permissions is the problem here.

network service maybe ?

0

I've got the same error just now and Google help me to find answer: Loading/Saving bitmap causes the locked file.

Workaround is create othe bitmap, save and let it release:

Bitmap tmp = new Bitmap(bitmapToBeSaved); tmp.SaveToFile(fileName);

0

Please be sure about the path you are using for images,I also face the same error.Check all paths you are using.

gofor.net
  • 4,218
  • 10
  • 43
  • 65
0

Is your local machine Vista or Windows 7, and your server Windows Server 2003? I believe the GDI+ implementation differs, and you won't see the error on the newer OSs. I'm just running into this issue now, and that's one of the factoids I've come across.

Mark Richman
  • 28,948
  • 25
  • 99
  • 159
0

Make sure IIS_WPG has the correct permissions on your upload folder and also ASPNET.

I just had the same problem and this fixed it.

Don't forget to propagate the permissions through your sub folders if required too ( I may have forgotten that.. :) )

Andrew
  • 9,967
  • 10
  • 64
  • 103
0

Try changing you application pool identity setting from ApplicationPoolIdentity to LocalSystem to verify its a permission problem.

However, don't use this setting long-term as it's a security risk; use it only as a diagnosis.

Clinton Ward
  • 2,441
  • 1
  • 22
  • 26