Your process is the one that uses file, you need to set image to null use something like this,
using(var img = Image.FromFile(filename).GetThumbnailImage(GetWitdth(filename, GetHeight(filename, 200)), GetfHeight(filename, 200), null, new IntPtr()))
pictureBox1.Image = img;
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
If you want to repalce the file if it exists, rather than deleting it then copying it, try changing it to this:
File.Copy(sourceFileName, destFileName, true);
The boolean at the end sets whether files can be replaced. By setting it to true it should overwrite the file if it already exists :)
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
had a quick search around the Micrsoft Support site, if you use FromFile in code to set an image file to a picturebox it locks the file. You can avoid this by using a filestream to retrieve the image:
System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
PictureBox1.Image = System.Drawing.Image.FromStream(fs);
fs.Close();
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
Have you implemented the code i gave you? This solves the problem as it reads the file without locking it.
Your code should look like this:
PictureBox pb = new PictureBox();
System.IO.FileStream fs = new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read);
Image loadedImage = Image.FromStream(fs);
fs.Close();
pb.Height = 245;
pb.Width = 245;
pb.BorderStyle = BorderStyle.None;
pb.Image = loadedImage;
pb.Name = "pic" + j.ToString();
pb.Click += new EventHandler(picture_click);
pb.SizeMode = PictureBoxSizeMode.StretchImage;
flowLayoutPanel1.Controls.Add(pb);
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
Aah, no problem.
I take it the code fixed the issue then? If so, remember to mark the thread as solved.
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246