Generic GDI+ Error when saving bitmaps

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Kennedy [UB]

    Generic GDI+ Error when saving bitmaps

    Hi Everyone,

    I have this multithreaded C# windows forms application which does a lot of
    image processing. Occasionally, I get the following error:

    A generic error occurred in GDI+.
    System.Runtime. InteropServices .ExternalExcept ion: A generic error occurred
    in GDI+.
    at System.Drawing. Image.Save(Stri ng filename, ImageCodecInfo encoder,
    EncoderParamete rs encoderParams)
    at System.Drawing. Image.Save(Stri ng filename, ImageFormat format)

    It's very strange. Most of the time it works without a problem. But
    sometimes I get this error. It can happen with the same source image which
    just worked previously and will work next time.

    Does anyone know what's causing this? "A generic error" is quiet unhelpful
    tracking this down.

    Thanks for any help.

    Regards,
    Michael

    Michael Kennedy
    Saw.com has successfully helped thousands of buyers acquire the perfect domain name. Interested in Unittesting.com? Let’s get started.



  • Peter Huang [MSFT]

    #2
    RE: Generic GDI+ Error when saving bitmaps

    Hi Michael,

    The save method of Image class is not thread safe.

    Here is what MSDN said,
    Any public static (Shared in Visual Basic) members of this type are safe
    for multithreaded operations. Any instance members are not guaranteed to be
    thread safe.

    For more information, you may refer to the link below.

    frlrfsystemdraw ingimageclasssa vetopic.asp

    So I think you may try to invoke the Save method in one thread, which will
    avoid the problem.


    Regards,
    Peter Huang
    Microsoft Online Partner Support
    Get Secure! www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.

    --------------------[color=blue]
    >From: "Michael Kennedy [UB]" <mkennedy@REMOV ETHIS.unitedbin ary.com>
    >Subject: Generic GDI+ Error when saving bitmaps
    >Date: Wed, 10 Sep 2003 11:04:08 -0700
    >Lines: 28
    >Organization : United Binary, LLC.
    >X-Priority: 3
    >X-MSMail-Priority: Normal
    >X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
    >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
    >Message-ID: <uOOEwX8dDHA.35 84@tk2msftngp13 .phx.gbl>
    >Newsgroups: microsoft.publi c.dotnet.genera l
    >NNTP-Posting-Host: 130.191.240.187
    >Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
    >Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.genera l:108001
    >X-Tomcat-NG: microsoft.publi c.dotnet.genera l
    >
    >Hi Everyone,
    >
    >I have this multithreaded C# windows forms application which does a lot of
    >image processing. Occasionally, I get the following error:
    >
    >A generic error occurred in GDI+.
    >System.Runtime .InteropService s.ExternalExcep tion: A generic error occurred
    >in GDI+.
    > at System.Drawing. Image.Save(Stri ng filename, ImageCodecInfo encoder,
    >EncoderParamet ers encoderParams)
    > at System.Drawing. Image.Save(Stri ng filename, ImageFormat format)
    >
    >It's very strange. Most of the time it works without a problem. But
    >sometimes I get this error. It can happen with the same source image which
    >just worked previously and will work next time.
    >
    >Does anyone know what's causing this? "A generic error" is quiet unhelpful
    >tracking this down.
    >
    >Thanks for any help.
    >
    >Regards,
    >Michael
    >
    >Michael Kennedy
    >http://www.unittesting.com
    >
    >
    >[/color]

    Comment

    • Michael Kennedy [UB]

      #3
      Re: Generic GDI+ Error when saving bitmaps

      Hi Peter,

      Thanks for the info. However, I did *not* assume that the Save method was
      thread-safe. I only call Save once, from one thread for any given instance
      of the Bitmap class. Perhaps some more info would be useful here. This is
      the basic flow of events involving the Bitmap.

      I have a loop running in a worker thread which rebuilds scenes and then adds
      them to a control and list of controls which render copies of the images in
      the main GUI thread of the application. Here is a cut-down version of the
      thread which does the image generating and the other simply sets the values
      of PictureBox's Image property and lets them repaint themselves (indirectly
      through the ThumbnailContro l class):

      public void ProcessScenes()
      {
      foreach (Scene s in scenes)
      {
      renderer.Curren tScene = s;
      renderer.Rebuil dSceneImage();

      // ... (non-image related stuff)

      string file = FILENAME;
      try
      {
      Image sceneImage = new Bitmap(renderer .SceneImage);
      sceneImage.Save (file, ImageFormat.Png ); // <-- HERE IS THE GENERIC
      ERROR
      sceneImage.Disp ose();
      }
      catch (Exception ex)
      {
      Trace.WriteLine (ex);
      // ... (handle error)
      }

      Bitmap oldImage = thumbnailContro l.SourceImage;
      thumbnailContro lCurrent.Source Image = new Bitmap(renderer .SceneImage);
      if (oldImage != null)
      {
      oldImage.Dispos e();
      }
      thumbnailPageCo ntrol1.AddThumb nail(new ThumbnailPageCo ntrol(new
      Bitmap(renderer .SceneImage)));
      }
      }

      What could be going on here? Any ideas?

      Thanks,
      Michael

      "Peter Huang [MSFT]" <v-phuang@online.m icrosoft.com> wrote in message
      news:Bbo84DBeDH A.1564@cpmsftng xa06.phx.gbl...[color=blue]
      > Hi Michael,
      >
      > The save method of Image class is not thread safe.
      >
      > Here is what MSDN said,
      > Any public static (Shared in Visual Basic) members of this type are safe
      > for multithreaded operations. Any instance members are not guaranteed to[/color]
      be[color=blue]
      > thread safe.
      >
      > For more information, you may refer to the link below.
      >[/color]
      http://msdn.microsoft.com/library/de...us/cpref/html/[color=blue]
      > frlrfsystemdraw ingimageclasssa vetopic.asp
      >
      > So I think you may try to invoke the Save method in one thread, which will
      > avoid the problem.
      >
      >
      > Regards,
      > Peter Huang
      > Microsoft Online Partner Support
      > Get Secure! www.microsoft.com/security
      > This posting is provided "as is" with no warranties and confers no rights.
      >
      > --------------------[color=green]
      > >From: "Michael Kennedy [UB]" <mkennedy@REMOV ETHIS.unitedbin ary.com>
      > >Subject: Generic GDI+ Error when saving bitmaps
      > >Date: Wed, 10 Sep 2003 11:04:08 -0700
      > >Lines: 28
      > >Organization : United Binary, LLC.
      > >X-Priority: 3
      > >X-MSMail-Priority: Normal
      > >X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
      > >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
      > >Message-ID: <uOOEwX8dDHA.35 84@tk2msftngp13 .phx.gbl>
      > >Newsgroups: microsoft.publi c.dotnet.genera l
      > >NNTP-Posting-Host: 130.191.240.187
      > >Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
      > >Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.genera l:108001
      > >X-Tomcat-NG: microsoft.publi c.dotnet.genera l
      > >
      > >Hi Everyone,
      > >
      > >I have this multithreaded C# windows forms application which does a lot[/color][/color]
      of[color=blue][color=green]
      > >image processing. Occasionally, I get the following error:
      > >
      > >A generic error occurred in GDI+.
      > >System.Runtime .InteropService s.ExternalExcep tion: A generic error[/color][/color]
      occurred[color=blue][color=green]
      > >in GDI+.
      > > at System.Drawing. Image.Save(Stri ng filename, ImageCodecInfo encoder,
      > >EncoderParamet ers encoderParams)
      > > at System.Drawing. Image.Save(Stri ng filename, ImageFormat format)
      > >
      > >It's very strange. Most of the time it works without a problem. But
      > >sometimes I get this error. It can happen with the same source image[/color][/color]
      which[color=blue][color=green]
      > >just worked previously and will work next time.
      > >
      > >Does anyone know what's causing this? "A generic error" is quiet[/color][/color]
      unhelpful[color=blue][color=green]
      > >tracking this down.
      > >
      > >Thanks for any help.
      > >
      > >Regards,
      > >Michael
      > >
      > >Michael Kennedy
      > >http://www.unittesting.com
      > >
      > >
      > >[/color]
      >[/color]


      Comment

      • Peter Huang [MSFT]

        #4
        Re: Generic GDI+ Error when saving bitmaps

        Hi Michael,

        To isolated the problem,
        1.you may try to replace your code as follows.[color=blue]
        > Image sceneImage = new Bitmap(renderer .SceneImage);
        > sceneImage.Save (file, ImageFormat.Png ); // <-- HERE IS THE GENERIC
        >ERROR[/color]
        with
        Image sceneImage = new Bitmap(@"c:\tes t.bmp");
        sceneImage.Save (file, ImageFormat.Png ); // <-- HERE IS THE GENERIC

        2. You may try to Create a new project and modify your code to be single
        threaded to see if the problem persists.
        [You may only need to copy the necessary code to reproduce the problem in a
        single threaded environment.

        Please perform the test and let me know the result.


        Regards,
        Peter Huang
        Microsoft Online Partner Support
        Get Secure! www.microsoft.com/security
        This posting is provided "as is" with no warranties and confers no rights.

        --------------------[color=blue]
        >From: "Michael Kennedy [UB]" <mkennedy@REMOV ETHIS.unitedbin ary.com>
        >References: <uOOEwX8dDHA.35 84@tk2msftngp13 .phx.gbl>[/color]
        <Bbo84DBeDHA.15 64@cpmsftngxa06 .phx.gbl>[color=blue]
        >Subject: Re: Generic GDI+ Error when saving bitmaps
        >Date: Mon, 15 Sep 2003 13:21:31 -0700
        >Lines: 133
        >Organization : United Binary, LLC.
        >X-Priority: 3
        >X-MSMail-Priority: Normal
        >X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
        >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
        >Message-ID: <efGE2b8eDHA.55 6@TK2MSFTNGP11. phx.gbl>
        >Newsgroups: microsoft.publi c.dotnet.genera l
        >NNTP-Posting-Host: 130.191.240.187
        >Path:[/color]
        cpmsftngxa07.ph x.gbl!cpmsftngx a10.phx.gbl!TK2 MSFTNGXA05.phx. gbl!TK2MSFTNGP0 8
        ..phx.gbl!TK2MS FTNGP11.phx.gbl[color=blue]
        >Xref: cpmsftngxa07.ph x.gbl microsoft.publi c.dotnet.genera l:108293
        >X-Tomcat-NG: microsoft.publi c.dotnet.genera l
        >
        >Hi Peter,
        >
        >Thanks for the info. However, I did *not* assume that the Save method was
        >thread-safe. I only call Save once, from one thread for any given instance
        >of the Bitmap class. Perhaps some more info would be useful here. This is
        >the basic flow of events involving the Bitmap.
        >
        >I have a loop running in a worker thread which rebuilds scenes and then[/color]
        adds[color=blue]
        >them to a control and list of controls which render copies of the images in
        >the main GUI thread of the application. Here is a cut-down version of the
        >thread which does the image generating and the other simply sets the values
        >of PictureBox's Image property and lets them repaint themselves (indirectly
        >through the ThumbnailContro l class):
        >
        >public void ProcessScenes()
        >{
        > foreach (Scene s in scenes)
        > {
        > renderer.Curren tScene = s;
        > renderer.Rebuil dSceneImage();
        >
        > // ... (non-image related stuff)
        >
        > string file = FILENAME;
        > try
        > {
        > Image sceneImage = new Bitmap(renderer .SceneImage);
        > sceneImage.Save (file, ImageFormat.Png ); // <-- HERE IS THE GENERIC
        >ERROR
        > sceneImage.Disp ose();
        > }
        > catch (Exception ex)
        > {
        > Trace.WriteLine (ex);
        > // ... (handle error)
        > }
        >
        > Bitmap oldImage = thumbnailContro l.SourceImage;
        > thumbnailContro lCurrent.Source Image = new[/color]
        Bitmap(renderer .SceneImage);[color=blue]
        > if (oldImage != null)
        > {
        > oldImage.Dispos e();
        > }
        > thumbnailPageCo ntrol1.AddThumb nail(new ThumbnailPageCo ntrol(new
        >Bitmap(rendere r.SceneImage))) ;
        > }
        >}
        >
        >What could be going on here? Any ideas?
        >
        >Thanks,
        >Michael
        >
        >"Peter Huang [MSFT]" <v-phuang@online.m icrosoft.com> wrote in message
        >news:Bbo84DBeD HA.1564@cpmsftn gxa06.phx.gbl.. .[color=green]
        >> Hi Michael,
        >>
        >> The save method of Image class is not thread safe.
        >>
        >> Here is what MSDN said,
        >> Any public static (Shared in Visual Basic) members of this type are safe
        >> for multithreaded operations. Any instance members are not guaranteed to[/color]
        >be[color=green]
        >> thread safe.
        >>
        >> For more information, you may refer to the link below.
        >>[/color]
        >http://msdn.microsoft.com/library/de...-us/cpref/html[/color]
        /[color=blue][color=green]
        >> frlrfsystemdraw ingimageclasssa vetopic.asp
        >>
        >> So I think you may try to invoke the Save method in one thread, which[/color][/color]
        will[color=blue][color=green]
        >> avoid the problem.
        >>
        >>
        >> Regards,
        >> Peter Huang
        >> Microsoft Online Partner Support
        >> Get Secure! www.microsoft.com/security
        >> This posting is provided "as is" with no warranties and confers no[/color][/color]
        rights.[color=blue][color=green]
        >>
        >> --------------------[color=darkred]
        >> >From: "Michael Kennedy [UB]" <mkennedy@REMOV ETHIS.unitedbin ary.com>
        >> >Subject: Generic GDI+ Error when saving bitmaps
        >> >Date: Wed, 10 Sep 2003 11:04:08 -0700
        >> >Lines: 28
        >> >Organization : United Binary, LLC.
        >> >X-Priority: 3
        >> >X-MSMail-Priority: Normal
        >> >X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
        >> >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
        >> >Message-ID: <uOOEwX8dDHA.35 84@tk2msftngp13 .phx.gbl>
        >> >Newsgroups: microsoft.publi c.dotnet.genera l
        >> >NNTP-Posting-Host: 130.191.240.187
        >> >Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
        >> >Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.genera l:108001
        >> >X-Tomcat-NG: microsoft.publi c.dotnet.genera l
        >> >
        >> >Hi Everyone,
        >> >
        >> >I have this multithreaded C# windows forms application which does a lot[/color][/color]
        >of[color=green][color=darkred]
        >> >image processing. Occasionally, I get the following error:
        >> >
        >> >A generic error occurred in GDI+.
        >> >System.Runtime .InteropService s.ExternalExcep tion: A generic error[/color][/color]
        >occurred[color=green][color=darkred]
        >> >in GDI+.
        >> > at System.Drawing. Image.Save(Stri ng filename, ImageCodecInfo encoder,
        >> >EncoderParamet ers encoderParams)
        >> > at System.Drawing. Image.Save(Stri ng filename, ImageFormat format)
        >> >
        >> >It's very strange. Most of the time it works without a problem. But
        >> >sometimes I get this error. It can happen with the same source image[/color][/color]
        >which[color=green][color=darkred]
        >> >just worked previously and will work next time.
        >> >
        >> >Does anyone know what's causing this? "A generic error" is quiet[/color][/color]
        >unhelpful[color=green][color=darkred]
        >> >tracking this down.
        >> >
        >> >Thanks for any help.
        >> >
        >> >Regards,
        >> >Michael
        >> >
        >> >Michael Kennedy
        >> >http://www.unittesting.com
        >> >
        >> >
        >> >[/color]
        >>[/color]
        >
        >
        >[/color]

        Comment

        • banks362001
          New Member
          • Jul 2006
          • 1

          #5
          here's wat i found out;:
          //-----------------------------------------------------------------------------------------//

          Image1.Visible= true;
          byte[] picture=picture sInfo.picture;
          int newWidth=Conver t.ToInt32(Image 1.Width.Value), newHeight=Conve rt.ToInt32(Imag e1.Height.Value );
          System.IO.Memor yStream ms = new System.IO.Memor yStream(picture );
          System.Drawing. Image image = System.Drawing. Image.FromStrea m(ms);
          try
          {
          string tempFile = "\\banks.jp g";
          System.IO.Path. GetFileName(tem pFile);
          image.Save(temp File);
          string filePath=System .IO.Path.GetFul lPath(tempFile) ;
          Image1.ImageUrl =filePath.ToStr ing();
          image.Dispose() ;
          }

          catch(Exception ex)
          {
          ex.ToString();
          }






          //----------the string tempFile = "\\banks.jp g"; points to the C:\ of ur local machine..Make sure the C:\ has ASPNET permission...on ce u do dat ur image appears just like magic

          Comment

          Working...