473,382 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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.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
4 48170
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Fabrizio | last post by:
Hi, in my environment i cannot use this sample because the saving process give me an error : GENERIC ERROR IN GDI+. Anyone knows what error is and how can I solve this? thank you! Fabrizio...
2
by: Alphonse Giambrone | last post by:
I am currently reading 'Programming The Web with Visual Basic .NET' and have so far found it to be excellent. Downloaded all the code from Apress and working in chapter 4, I get the error shown...
3
by: T. Davis | last post by:
In C#, I am able to successfully stream a TIFF image that comes from a BLOB field in a database, save it to file, then convert the pages within TIFF file into jpegs (using GDI+) and display on the...
2
by: Tim::.. | last post by:
Hi can someone please tell me why I keep getting the following error and how I might fix it ...::ERROR::. Exception Details: System.Runtime.InteropServices.ExternalException: A generic error...
3
by: Michael Kennedy [UB] | last post by:
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+....
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
0
by: news.microsoft.com | last post by:
Hi guys, This text looks long and complicate but it is not, and I really really need some help here. For the last 24hs I'm trying to resolve this issue (last night I dreamed
14
by: James Wong | last post by:
Hi everybody, I'm facing a serious trouble relating to GDI+ generic error. The error message is "A Generic error occured in GDI+" and the following information is stored in Excepton object:...
0
by: Adnan Chaudhry | last post by:
Hi I'm trying to open, edit and save an animated gif. When saving frame 2 - I get the following error. A generic error occured in GDI+ when i reach the following step. img.SaveAdd(img2,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.