1

I have one image displayed in picture box control in my application. Now I want to update that Image when my application is running and image already has been used by the application.

I used this line of code to update my logo image:UPDATED CODE

  public void showRow()
  {
      string _fileName = Application.StartupPath + "\\Logo" + ".png";

      using (var stream = new FileStream(_fileName, FileMode.Open, FileAccess.Read))
      {
          var img = Image.FromStream(stream);
          pbStoreLogo.Image = img;
      }
  }

  private void btnsave_Click(object sender, EventArgs e)
  {
      using (var m = new MemoryStream())
      {
          pbStoreLogo.Image.Save(m,System.Drawing.Imaging.ImageFormat.Png);
          var img = Image.FromStream(m);
          img.Save(Application.StartupPath + "\\Logo" + ".png");
      }
  }

And it through exception: A generic error occurred in GDI+.

So please suggest me how to change my image at run time or provide me solution of this exception.

nawfal
  • 70,104
  • 56
  • 326
  • 368
Bhavik Patel
  • 752
  • 1
  • 5
  • 20
  • Update or write? Both are two different operations. Do you want to refresh the existing image in picturebox? Or do you want to save the pb image to some file? – nawfal Oct 30 '12 at 10:15
  • I want to save pb image to existing file which is currently used in running application. – Bhavik Patel Oct 30 '12 at 11:15

1 Answers1

1

To save an Image object from your application to a physical path in file system, you have to ensure the file is not being used by your application or any other application. Hence you have to take care not only how you are saving the file, but also how you're reading the image to picturebox.

To free up the physical resource, you have to read the file to a stream and then form the image from the stream and then assign the image to picture box. Like this:

string _fileName = Application.StartupPath + "\\Logo" + ".png";

using (var stream = new FileStream(_fileName, FileMode.Open, FileAccess.Read))
{
    var img = Image.FromStream(stream);
    pbStoreLogo.Image = img;
}

And while saving:

using (var m = new MemoryStream())
{
    pbStoreLogo.Image.Save(m, ImageFormat.Png);
    var img = Image.FromStream(m);
    img.Save(_fileName);
}

If you are having the file as a resource file in your project, then all these hassles shouldn't be there. You can just do:

pbStoreLogo.Image = Properties.Resources.Image1;

and

pbStoreLogo.Image.Save(_fileName);

You can find more info in this SO question.

Community
  • 1
  • 1
nawfal
  • 70,104
  • 56
  • 326
  • 368
  • When I am using this code, It will through same exception **A generic error occurred in GDI+.** at Save time. – Bhavik Patel Oct 31 '12 at 04:36
  • @BumbleBee you will get that error even if you are saving the file using the above code. The key here is to even load the picture to pb by the method I specify. Did you load to pb the way I specified? Then there should be no error. – nawfal Oct 31 '12 at 05:53
  • I loaded image in pb as you suggest and after that i was trying to save image at specified location, Although I got same error. – Bhavik Patel Oct 31 '12 at 06:35
  • @BumbleBee update your question to show us the part where you are loading the pb. That will do better. Otherwise its impossible to understand from here.. – nawfal Oct 31 '12 at 06:43
  • @BumbleBee I do not know why u get the error. It works for me fine. Do you have write access to the specified path? May be no. Where's Application.Startup path? Is it somewhere in C drive? – nawfal Oct 31 '12 at 09:22
  • I got that. The scenario in my case is: I am trying to change Logo image in child form but the logo is already used in parent form. Because of this I am not able to change logo image and it through an error. So Is it possible to change image from child form when it is used in parent form??? – Bhavik Patel Nov 01 '12 at 06:19
  • 1
    @BumbleBee you should use logo in parent form the way I described, I mean the loading part. If you have already done that, then I'm helpless as to know why it throws error. For me it works. Just ensure you load the image to parent form the way I described – nawfal Nov 01 '12 at 06:24
  • I had done it as your way. Anyways Thanks for your support and quick responses. – Bhavik Patel Nov 01 '12 at 06:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18900/discussion-between-nawfal-and-bumblebee) – nawfal Nov 01 '12 at 06:38