Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1770
  • Last Modified:

A generic error occurred in GDI+. Bitmap.Save

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
InquisitiveMind
Asked:
InquisitiveMind
  • 3
  • 2
2 Solutions
 
käµfm³d 👽Commented:
Are you running the application as a user who has permissions to the share?
0
 
InquisitiveMindAuthor Commented:
@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
 
sognoctCommented:
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
InquisitiveMindAuthor Commented:
@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
 
sognoctCommented:
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
 
InquisitiveMindAuthor Commented:
Thanks guys. The temporary file solution worked for me.
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now