C # loading image-(C #) the image. fromfile method locks the file and provides possible solutions.

Source: Internet
Author: User
Image. after fromfile is used, the corresponding file will not be unlocked until the image object generated by it is called by disponse, which causes a problem, the image cannot be operated (such as deleting or modifying) before it is unlocked ). the following three methods have been used in the previous process to solve the problem. the first method is to destroy the image object before file operations. picturebox picbox;  If (Picbox. image! = Null  ) Picbox. image. disponse (); the second method is to replace system. Drawing. Image IMG when loading images. = System. Drawing. image. fromfile (filepath); system. Drawing. Image BMP = New  System. Drawing. Bitmap (IMG. Width, IMG. Height, system. Drawing. imaging. pixelformat. format32bppargb); system. Drawing. Graphics g = System. Drawing. Graphics. fromimage (BMP); G. drawimage (IMG, 0 , 0  ); G. Flush (); G. Dispose (); IMG. Dispose ();  //  The following uses BMP as the displayed image object.  System. Drawing. Image img = System. Drawing. image. fromfile (filepath); system. Drawing. Image BMP = New  System. Drawing. Bitmap (IMG); IMG. Dispose (); Method 4: FS = New system. Io. filestream ( "  C: \ winnt \ WEB \ wallpaper \ fly away.jpg  " , Io. filemode. Open, Io. fileaccess. Read) picturebox1.image = The fromfile method of the system. Drawing. image. fromstream (FS) fs. Close () image class does not close the file after opening the volume. As a result, the file is locked and cannot be deleted or moved. The fromstream method is used,CodeAs follows:  //  Read File streams Filestream = New  Filestream (iconpath, filemode. Open, fileaccess. Read );  Int Bytelength = ( Int  ) Filestream. length;  Byte [] Filebytes = New  Byte  [Bytelength]; filestream. Read (filebytes,  0  , Bytelength );  //  File stream off, file unlocked  Filestream. Close (); pictrue. Image = Image. fromstream ( New  Memorystream (filebytes); because the stream of the fromstream method parameter application must be kept open, a file in the code can be converted to the memeorystream stream to disable the file stream, keep the memorystream stream open. 

Http://blog.sina.com.cn/s/blog_9908653401014elg.html

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.