Solved

A generic error occurred in GDI+. Bitmap.Save

Posted on 2013-01-16
6
1,507 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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