Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I loaded an image into a picturebox using:

picturebox1.Image = Image.FromFile()

And save it by using:

Bitmap bm = new Bitmat(pictureBox1.Image);<br />
bm.Save(FileName, ImageFormat.Bmp);


It works perfectly fine when creating a new file, but the moment you try to replace the existing image I get thrown a runtime error- A generic error occurred in GDI+.
So what can I do to solve this problem??
Posted
Updated 17-Aug-11 23:40pm
v2

I don't see why your code shouldn't work. But try using passing a stream instead, might work who knows.
C#
Stream stream = new FileStream(FileName, FileMode.Create, FileAccess.Write, FileShare.None);
Bitmap bm = new Bitmat(pictureBox1.Image);
bm.Save(stream, ImageFormat.Bmp);
 
Share this answer
 
Comments
Jeremy Shin Woo 18-Aug-11 6:00am    
Thanx.. but I don't work :'(
Check if the file already exists.
If so, delete it.
Then write the new file.
 
Share this answer
 
Comments
Jeremy Shin Woo 18-Aug-11 7:22am    
Can I do it by the code?
BoxyBrown 18-Aug-11 8:00am    
I think it wouldn't work also because of locking file while using Image.FromFile(). You'll get error on delete.
Image.FromFile() lock file.

"The file remains locked until the Image is disposed."
http://msdn.microsoft.com/en-us/library/stf701f5.aspx[^]

C#
FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
picturebox1.Image = Image.FromStream(fs);
fs.Close();

Bitmap bm = new Bitmap(picturebox1.Image);

bm.Save(FileName, ImageFormat.Bmp);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900