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