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

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.