blah blah blah is here! blah blah » Close

up0down
link

Hello I have a problem and not sure how to fix it.

I've created an inventory application using C#.net

A user is able to pull up a part with info and with a picture of that part.
I load the picture into a picturebox using the code....

Image ing = Image.FromFile(FILELOCATION);
PICTUREBOX.Image = ing;

works great.

The problem is when the user clicks SAVE to save it all back it won't do it.
I get an error that says "A GENERIC ERROR OCCURRED IN GDI+"
I understand what it means... it means that I can't right back to that folder that has the loaded image. The image is locked. How do I unlock the image(picture) once I loaded into my application(picture-box).

Please help. Code same would be great.

thank you so so much

last answered 8 months ago

3 answers

up0down
link

use

ing.Dispose();

to free up the resources

Hello Foamy. I tried using ing.Dispose() but I get an error msg that says 'Invalid parameter used" Any other suggestions.

foamy
1822

Post the code and let's have a look :)

strimage = (ds.Tables["Inventory"].Rows[0]["Image1"]).ToString(); txtImageName.Text = strimage; Image ing = Image.FromFile(@"C:\Documents and Settings\Joe\My Documents\My Pictures\"+strimage+" "); pbpart.Image = ing; ing.Dispose(); Let me explain how this works...... I keep the name of the pictures in an access database. The names are kept in a table called Image1. When the user goes into the application and looks up a part it checks the table for the name of the part and then it goes into a folder that holds the pictures and it pulls it up.... that works great... but now if the user changes the picture of some other information and clicks on save it won't... cause the code has the picture locked... As of now I'm just testing the code... so I'm the only one accessing the program. I'm running as admin so I don't think it has anything to do with the permission of the folder. Please help

additional note: pbpart is the name of my picturebox

foamy
1822

You shouldn't call Dispose until you're done with the image :) This means you will need to keep a reference to it in your class and call Dispose when the user clicks 'save'

Still not working... I place... pbpart.Image.Dispose(); in the "Save" button code.... right before the line of code... pbpart.Image.Save("C:\\Documents and Settings\\Joe\\My Documents\\My Pictures\\"+txtImageName.Text+" ", ImageFormat.Jpeg); Still getting the same error msg. Other suggestions, please????

MadHatter
1699

after you create the image, create a second instance using img.Clone (you'll have to cast it back to an image), because it loads and locks the original file when you load the image from file. open the image, clone it, dispose of it, use the cloned image to assign to the picture box, then use that one to save.

Hello MadHatter, I understand what you are saying. I'm just not sure how to write this code. Can you please help me out. thank you

Can some please help me out with the code. How does Clone() work ????????? PLEASE

up0down
link

Image image = Image.FromFile(@"C:\Documents and Settings\Joe\My Documents\My Pictures\"+strimage+" ");
Bitmap bmp = new Bitmap(image);
image.Dispose();
bmp.Save(@"C:\Documents and Settings\Joe\My Documents\My Pictures\"+strimage+" ");
pbpart.Image = bmp;

up0down
link

Image orig = Image.FromFile(@"C:\Documents and Settings\Joe\My Documents\My Pictures\"+strimage+" ");
Image img = (Image)orig.Clone();
orig.Dispose();
pictureBox.Image = img
img.Save(@"C:\Documents and Settings\Joe\My Documents\My Pictures\"+strimage+" ");

Feedback