4

While i am trying to save Image getting this error A generic error occurred in GDI+ I searched for this error and checked write permissions for this folder ,and also checked that image file is not in use by anything else (including my code) But While I am trying to save Image still getting same error,Where is the mistake

public partial class AdsMaster : Form
    {
        OpenFileDialog ofd=null;
        public AdsMaster()
        {
            InitializeComponent();
        }

    private void browseButton_Click(object sender, EventArgs e)
    {
        ofd = new OpenFileDialog();
        ofd.Filter = "image files|*.jpg;*.jpeg;*.gif;*.png;*.bmp";
        DialogResult dr = new DialogResult();
        if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            Image img = new Bitmap(ofd.FileName);//create the bitmap
            string imgName = ofd.SafeFileName;
            txtImagePath.Text = imgName;
            pictureBox1.Image = img.GetThumbnailImage(350, 350, null, new IntPtr());
            ofd.RestoreDirectory = true;
            img.Dispose();
        }
    }

    private void saveImage_Click(object sender, EventArgs e)
    {
        String str = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        string path = str + "\\Image\\";
        Image img = new Bitmap(ofd.FileName);
        string imgName = ofd.SafeFileName;
        try
        {
               img.Save(path + imgName); //getting error at this line 
               MessageBox.Show("Image is saved");
               img.Dispose();  // dispose of your image                   
        }
        catch(Exception err)
        {
            MessageBox.Show(err.Message.ToString());
        }

      }        
} 
Durga
  • 1,283
  • 9
  • 28
  • 54
  • Check the solutions here: http://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream?rq=1 – Mathew Thompson Sep 03 '13 at 08:10
  • Could it be that you are trying to save the image to the same file it was loaded from (is the image file in the application directory)? – Rotem Sep 03 '13 at 08:11
  • I tried saving it from another Folder ,still getting same error – Durga Sep 03 '13 at 08:17

2 Answers2

4

I think the problem here may be due to the fact that a lock is placed on the original image when you call

Image img = new Bitmap(ofd.FileName);

The following should do the trick

private void saveImage_Click(object sender, EventArgs e)
{
    string str = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
    string path = str + "\\Image\\";
    string imgName = ofd.SafeFileName;

    try
    {
        File.Copy(ofd.FileName, path + imgName);

        MessageBox.Show("Image is saved");
    }
    catch (Exception err)
    {
        MessageBox.Show(err.Message.ToString());
    }
}        
Teppic
  • 2,506
  • 20
  • 26
  • Thanks for that, helped me out! Though all I had to do was make a new Bitmap from the existing one. – Kyle Sep 08 '14 at 18:50
  • http://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream/33324011#33324011 – Gaurang s Oct 24 '15 at 22:27
0

I resolve this error by changing the file name and path to where the file is stored. You needs to check either one of them-

1- FileName should be different/ should not get matched with other files name which have been already present on that location where new file/image you want to store.

2- change the (save ) location of the new file from operating system drive. try to avoid to store the new file/Image on a drive on which operating system has been already installed.

Below code works for me

         IWebdriver driver;
         driver = new ChromeDriver("G:\\Selenium_Csharp\\Jar\\chromedriver_win32"); 
         driver.Navigate().GoToUrl("http://www.gmail.com");
         driver.Manage().Window.Maximize();

         Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
        ss.SaveAsFile("e:\\pande", System.Drawing.Imaging.ImageFormat.Jpeg);
Avinash Pande
  • 1,510
  • 19
  • 17