After you use Image.FromFile (string path) in C #, you are prompted that the file is being used by another process for the issue of XXX
after using Image.FromFile (string path) in C #, it is suggested that the file is being used by another process because the corresponding file is not unlocked until it is called, and the resulting image object is Disponse (). This causes the problem that the image cannot be manipulated (such as deletions, modifications, etc.) until the graphic is unlocked.
This issue can be resolved by using the following three methods.
Method 1: Destroy the Image object before you perform a file operation.
System.Drawing.Image Image = System.Drawing.Image.FromFile (FilePath);
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap (image);
image. Dispose ();
Method 2: When loading an image, replace it with a method
System.Drawing.Image img = System.Drawing.Image.FromFile (filepath);
System.Drawing.Image bmp = New System.Drawing.Bitmap (img. Width, IMG. Height, IMG. PixelFormat);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage (BMP);
g.drawimage (img, 0, 0);
G.flush ();
g.dispose ();
img. Dispose ();
Method 4:
System.IO.FileStream fs = new System.IO.FileStream (Filepath,io. FileMode.Open, IO. FileAccess.Read);
image image = System.Drawing.Image.FromStream (FS)
FS. Close ();
using the FromFile method of the image class does not close after opening the file, resulting in file locking, cannot be deleted, moved and other operations.
can be modified as follows:
//Read file stream
System.IO.FileStream fs = new System.IO.FileStream (FilePath, FileMode.Open, FileAccess.Read);
int bytelength = (int) fs. Length;
byte[] filebytes = new Byte[bytelength];
FS. Read (filebytes, 0, bytelength);
//file stream closed, file unlocked
FS. Close ();
image image = Image.fromstream (new MemoryStream (filebytes));
because the stream that the FromStream method parameter applies to must remain open, there is a file stream in the code that transforms into the Memeorystream stream, which closes the file stream and keeps the MemoryStream stream open.
After you use Image.FromFile (string path) in C #, you are prompted that the file is being used by another process for the issue of XXX