none
'A generic error occurred in GDI+' RRS feed

  • Question

  • Hi
    With the following code, I Show an image in picturebox


    ConfigForm.pictureBox1.Image = new Bitmap(System.IO.Directory.GetCurrentDirectory() + "\\Some.jpg");


    and in somewhere else in my program, I want save on exactly this file (Some.jpg) with


    Image.Save(System.IO.Directory.GetCurrentDirectory() + "\\Some.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);


     , But the 'A generic error occurred in GDI+' occurred.

    What's wrong with this code?

     


    New Ideas, Superior Solutions Www.SalamGroup.Net
    Tuesday, March 15, 2011 9:10 PM

Answers

  • GDI+ keeps a lock on files from which an image was contructed.  To avoid the lock, construct the image from a MemorySteam:

        MemoryStream ms = new MemoryStream(File.ReadAllBytes(fileName));
        
    Image img = Image.FromStream(ms);

    • Marked as answer by Engineer Mahdi Wednesday, March 16, 2011 2:57 AM
    Tuesday, March 15, 2011 10:23 PM

All replies