By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,949 Members | 2,043 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,949 IT Pros & Developers. It's quick & easy.

Generic GDI+ Error when saving bitmaps

P: n/a
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
Jul 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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/de...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]" <mk******@REMOVETHIS.unitedbinary.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: <uO**************@tk2msftngp13.phx.gbl>
Newsgroups: microsoft.public.dotnet.general
NNTP-Posting-Host: 130.191.240.187
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.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.ExternalExceptio n: 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


Jul 21 '05 #2

P: n/a
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 [MSFT]" <v-******@online.microsoft.com> wrote in message
news:Bb**************@cpmsftngxa06.phx.gbl...
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/de...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]" <mk******@REMOVETHIS.unitedbinary.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: <uO**************@tk2msftngp13.phx.gbl>
Newsgroups: microsoft.public.dotnet.general
NNTP-Posting-Host: 130.191.240.187
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.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 ofimage processing. Occasionally, I get the following error:

A generic error occurred in GDI+.
System.Runtime.InteropServices.ExternalExceptio n: A generic error occurredin 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 whichjust worked previously and will work next time.

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

Thanks for any help.

Regards,
Michael

Michael Kennedy
http://www.unittesting.com

Jul 21 '05 #3

P: n/a
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]" <mk******@REMOVETHIS.unitedbinary.com>
References: <uO**************@tk2msftngp13.phx.gbl> <Bb**************@cpmsftngxa06.phx.gbl>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: <ef*************@TK2MSFTNGP11.phx.gbl>
Newsgroups: microsoft.public.dotnet.general
NNTP-Posting-Host: 130.191.240.187
Path: cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTN GXA05.phx.gbl!TK2MSFTNGP08
..phx.gbl!TK2MSFTNGP11.phx.gblXref: 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 addsthem 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 [MSFT]" <v-******@online.microsoft.com> wrote in message
news:Bb**************@cpmsftngxa06.phx.gbl...
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/de...-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]" <mk******@REMOVETHIS.unitedbinary.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: <uO**************@tk2msftngp13.phx.gbl>
>Newsgroups: microsoft.public.dotnet.general
>NNTP-Posting-Host: 130.191.240.187
>Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.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.ExternalExceptio n: A generic erroroccurred >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 imagewhich >just worked previously and will work next time.
>
>Does anyone know what's causing this? "A generic error" is quietunhelpful >tracking this down.
>
>Thanks for any help.
>
>Regards,
>Michael
>
>Michael Kennedy
>http://www.unittesting.com
>
>
>



Jul 21 '05 #4

P: 1
here's wat i found out;:
//-----------------------------------------------------------------------------------------//

Image1.Visible=true;
byte[] picture=picturesInfo.picture;
int newWidth=Convert.ToInt32(Image1.Width.Value),newHe ight=Convert.ToInt32(Image1.Height.Value);
System.IO.MemoryStream ms = new System.IO.MemoryStream(picture);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
try
{
string tempFile = "\\banks.jpg";
System.IO.Path.GetFileName(tempFile);
image.Save(tempFile);
string filePath=System.IO.Path.GetFullPath(tempFile);
Image1.ImageUrl=filePath.ToString();
image.Dispose();
}

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






//----------the string tempFile = "\\banks.jpg"; points to the C:\ of ur local machine..Make sure the C:\ has ASPNET permission...once u do dat ur image appears just like magic
Jul 17 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.