Solved

A generic error occurred in GDI+. Bitmap.Save

Posted on 2013-01-16
6
1,467 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 74

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

706 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now