問題情境:圖片文件加載到pdf中,程序沒有退出,再次加載該圖片文件,提示被占用。
解決辦法:
1.加載文件會鎖定該文件,fromfile方法會導致占用內存較大,不使用該方法。
FileStream fileStream = new FileStream(filePath, FileMode.Open,FileAccess.Read); int byteLength = (int)fileStream.Length; byte[] fileBytes = new byte[byteLength]; fileStream.Read(fileBytes, 0, byteLength); //文件流關閉,文件解除鎖定 fileStream.Close(); ImageList1.Images.Add(Image.FromStream(new MemoryStream(fileBytes)));