Solved

A generic error occurred in GDI+. Bitmap.Save

Posted on 2013-01-16
6
1,522 Views
Last Modified: 2013-01-17
Hi Experts,

I have a console app that tries to save an image from a local directory to a UNC Path (Shared Folder on a Network Drive).

I am able to do save it without any issues on another local folder, but not to the Shared drive.

I get the error "A generic error occurred in GDI+." of an ExternalException.

Your help would be appreciated
0
Comment
Question by:InquisitiveMind
  • 3
  • 2
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38785271
Are you running the application as a user who has permissions to the share?
0
 

Author Comment

by:InquisitiveMind
ID: 38785288
@kaufmed:

I do not have an app.config for now. But yes I did try giving my credentials as

<system.web>
    <identity impersonate="true" userName="domain\myUserName" password="xxxxx"/>
  </system.web>

But my application is a console application. So, that wouldn't work.
0
 
LVL 9

Accepted Solution

by:
sognoct earned 500 total points
ID: 38786763
destroy the bitmap after save then redeclare it

Bitmap bm = new Bitmap(mypath);
bm.Save("Shared Folder\\out.bmp" ,System.Drawing.Imaging.ImageFormat.Bmp );
bm .dispose();
bm = null;

or you can save a temporary file on local folder then copy it to the shared drive (safer solution)
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:InquisitiveMind
ID: 38788739
@sognoct:

The error occurs exactly at the line where I call the Save method. I am also calling the Dispose() method as suggested by you and still get the error. Any other ideas?
0
 
LVL 9

Assisted Solution

by:sognoct
sognoct earned 500 total points
ID: 38789325
just for debugging try to save to local folder...

string appPath = Path.GetDirectoryName(Application.ExecutablePath);
string tempbmp = appPath + "\\out.bmp"
bm.Save(tempbmp ,System.Drawing.Imaging.ImageFormat.Bmp );
bm .dispose();
bm = null;
io.file.copy(tempbmp, "\\\\sharedfolder\\" + filename; 
 

Open in new window


if it throws io error maybe is a permission issue ... if it is still GDI error, there is another possibility. In fact Bitmap cause GDI error if it is originated by a filestream and the filestream has been closed.
In this case must create a new bitmap objects that has not a closed filestream. So if bm is the original bitmap:

      //create temporary
      Image t=new Bitmap(bm.Width,bm.Height);
      //get graphics
      Graphics g=Graphics.FromImage(t);
      //copy original
      g.DrawImage(bm,0,0);
      //close original
      i.Dispose();
      //Can now save
      t.Save(path)

Open in new window

0
 

Author Closing Comment

by:InquisitiveMind
ID: 38789580
Thanks guys. The temporary file solution worked for me.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question