Generic GDI+ Error when saving bitmaps

  • Thread starter Michael Kennedy [UB]
  • Start date

M

Michael Kennedy [UB]

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.ExternalException: A generic error occurred
in GDI+.
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder,
EncoderParameters encoderParams)
at System.Drawing.Image.Save(String 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
 
Ad

Advertisements

P

Peter Huang [MSFT]

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.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdrawingimageclasssavetopic.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.

--------------------
 
M

Michael Kennedy [UB]

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 ThumbnailControl class):

public void ProcessScenes()
{
foreach (Scene s in scenes)
{
renderer.CurrentScene = s;
renderer.RebuildSceneImage();

// ... (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.Dispose();
}
catch (Exception ex)
{
Trace.WriteLine(ex);
// ... (handle error)
}

Bitmap oldImage = thumbnailControl.SourceImage;
thumbnailControlCurrent.SourceImage = new Bitmap(renderer.SceneImage);
if (oldImage != null)
{
oldImage.Dispose();
}
thumbnailPageControl1.AddThumbnail(new ThumbnailPageControl(new
Bitmap(renderer.SceneImage)));
}
}

What could be going on here? Any ideas?

Thanks,
Michael

Peter Huang said:
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.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdrawingimageclasssavetopic.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.

--------------------
From: "Michael Kennedy [UB]" <[email protected]>
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: <[email protected]>
Newsgroups: microsoft.public.dotnet.general
NNTP-Posting-Host: 130.191.240.187
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:108001
X-Tomcat-NG: microsoft.public.dotnet.general

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.ExternalException: A generic error occurred
in GDI+.
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder,
EncoderParameters encoderParams)
at System.Drawing.Image.Save(String 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
 
Ad

Advertisements

P

Peter Huang [MSFT]

Hi Michael,

To isolated the problem,
1.you may try to replace your code as follows.
Image sceneImage = new Bitmap(renderer.SceneImage);
sceneImage.Save(file, ImageFormat.Png); // <-- HERE IS THE GENERIC
ERROR
with
Image sceneImage = new Bitmap(@"c:\test.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.

--------------------
From: "Michael Kennedy [UB]" <[email protected]>
References: <[email protected]>
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: <[email protected]>
Newsgroups: microsoft.public.dotnet.general
NNTP-Posting-Host: 130.191.240.187
Path: cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
..phx.gbl!TK2MSFTNGP11.phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.general:108293
X-Tomcat-NG: microsoft.public.dotnet.general

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 ThumbnailControl class):

public void ProcessScenes()
{
foreach (Scene s in scenes)
{
renderer.CurrentScene = s;
renderer.RebuildSceneImage();

// ... (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.Dispose();
}
catch (Exception ex)
{
Trace.WriteLine(ex);
// ... (handle error)
}

Bitmap oldImage = thumbnailControl.SourceImage;
thumbnailControlCurrent.SourceImage = new Bitmap(renderer.SceneImage);
if (oldImage != null)
{
oldImage.Dispose();
}
thumbnailPageControl1.AddThumbnail(new ThumbnailPageControl(new
Bitmap(renderer.SceneImage)));
}
}

What could be going on here? Any ideas?

Thanks,
Michael

Peter Huang said:
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.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html
/
frlrfsystemdrawingimageclasssavetopic.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.

--------------------
From: "Michael Kennedy [UB]" <[email protected]>
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: <[email protected]>
Newsgroups: microsoft.public.dotnet.general
NNTP-Posting-Host: 130.191.240.187
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:108001
X-Tomcat-NG: microsoft.public.dotnet.general

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.ExternalException: A generic error occurred
in GDI+.
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder,
EncoderParameters encoderParams)
at System.Drawing.Image.Save(String 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

WebClient Saving Exception 3
Image Save Error 2
GDI+ and SAVE JPG or BMP on the server 2
GDI+ problem 5
GDI+ Error on Bitmap Save 1
Tiff to JPEG 2
Png Image display Problem 1
Generic Error in GDI+ 2

Top