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.
To save an
Imageobject 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:
And while saving:
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:
and
You can find more info in this SO question.