The process cannot access the file. How to solve the problem?

Why when re-entering the form, where is generated the picture gives this critical error?

The process cannot access the file "C:\Users\Владислав\AppData\Local\Temp\captcha.jpg" as this file is used by another process.

string tempPath = Path.GetTempPath() + "/captcha.jpg";
using (Bitmap bitmap = new Bitmap(200, 50))
 Brush white = new SolidBrush(Color.White);
 Brush blue = new SolidBrush(Color.DarkBlue);

 Rectangle rec = new Rectangle(0, 0, 200, 50);
 using (Graphics g = Graphics.FromImage(bitmap))
 g.FillRectangle(white, rec);
 g.DrawString("HELLO", new Font("Arial", 10), blue, PointF.Empty);

 if (File.Exists(tempPath))
img.Source = new BitmapImage(new Uri(tempPath));
June 26th 19 at 14:23
0 answer

Find more questions by tags C#