473,735 Members | 2,307 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
http://www.unittesting.com
Jul 21 '05 #1
4 48235
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/
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.

--------------------
From: "Michael Kennedy [UB]" <mk******@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: <uO************ **@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


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 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-******@online.m icrosoft.com> wrote in message
news:Bb******** ******@cpmsftng xa06.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/ 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.

--------------------
From: "Michael Kennedy [UB]" <mk******@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: <uO************ **@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 ofimage processing. Occasionally, I get the following error:

A generic error occurred in GDI+.
System.Runtime .InteropService s.ExternalExcep tion: A generic error occurredin 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 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:\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.

--------------------From: "Michael Kennedy [UB]" <mk******@REMOV ETHIS.unitedbin ary.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.publi c.dotnet.genera l
NNTP-Posting-Host: 130.191.240.187
Path: cpmsftngxa07.ph x.gbl!cpmsftngx a10.phx.gbl!TK2 MSFTNGXA05.phx. gbl!TK2MSFTNGP0 8
..phx.gbl!TK2MS FTNGP11.phx.gblXref: 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 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 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(rendere r.SceneImage))) ;
}
}

What could be going on here? Any ideas?

Thanks,
Michael

"Peter Huang [MSFT]" <v-******@online.m icrosoft.com> wrote in message
news:Bb******* *******@cpmsftn gxa06.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

/
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.
--------------------
>From: "Michael Kennedy [UB]" <mk******@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: <uO************ **@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 erroroccurred >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 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
banks362001
1 New Member
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
Jul 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1685
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 fmaltese@nospam.libero.it
2
22310
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 below. I am running IIS5 on WinXP Pro. Server Error in '/Chapter04' Application. ---------------------------------------------------------------------------- ---- A generic error occurred in GDI+. Description: An unhandled exception occurred...
3
5875
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 web (using ASP.NET). However, when I generate the Image object using FromStream (passing in the MemoryStream containing image bytes), an exception of "A generic error occurred in GDI+" is thrown when performing the conversion/save for display....
2
4883
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 occurred in GDI+ The line causing the problem is outputImage.Save, but I really don't understand why ...::CODE::. System.Drawing.Bitmap outputImage = new System.Drawing.Bitmap(origBitmap, newWidth, newHeight)...
3
674
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+. System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
15
5361
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 path of the uploaded image, and resize it with the provided dimensions. My function is below. The current function is returning an error when run from the upload function: A generic error occurred in GDI+. Not sure what exactly that means. From what...
0
2612
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
4108
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: System.Drawing.Graphics.CheckErrorStatus(Int32 status) System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format) System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, PointF...
0
2235
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, ParameterList); Code below.
0
8958
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9193
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4555
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4821
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3269
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2186
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.