Image.FormFile引起的若干問題

來源:互聯網
上載者:User

1.檔案正由另一進程使用,因此該進程無法訪問該檔案

問題描述:

this.pictureBox1.Image=Image.FormFile(sourceFile);

...

把sourceFile移動到另外一個檔案夾

if(this.pictureBox1.Image!=null)

{

  this.picutreBox1.Image.Dispose();

}

File.Copy(sourceFile,destinatFile);

File.Delete(sourceFile);

結果時不時的在File.Delete(sourceFile);處拋出異常:檔案正由另一進程使用,因此該進程無法訪問該檔案。

File.Copy(sourceFile,destinatFile);

File.Delete(sourceFile);

換成:File.Move(sourceFile,destinatFile);問題依然如故。

問題應該在this.pictureBox1.Image=Image.FormFile(sourceFile);在msdn上Image.FromFile(string)

有這樣的描述:在釋放 Image 之前,此檔案一直保持鎖定。

解決辦法:

Image img1=Image.FromFile(sourceFile);

Image img2=new Bitmap(img1);

img1.Dispose();

this.pictureBox1.Image=img2;

2.記憶體不足,或GDI+發生了一般性錯誤。

問題描述:在Image.FromFile(souceFile)出拋出System.OutOfMemoryException"記憶體不足"異常。

出現這種問題是由於souceFile檔案被破壞,或者是空圖片。

可以直接catch掉或在catch中做個標識。另外用

FileInfo fi=new FileInfo(souceFile);

if(fi.Length>0)

{

}

做個判斷。

 附:http://blog.csdn.net/Haliput/archive/2008/11/20/3339611.aspx,我嘗試了該貼的情況

我直接執行Image img=Image.FromFile(Application.StartPath+“\\”+souceFileName);

則不會報異常,

我按照該帖作者的代碼執行 :按Ctrl+F5執行,會報出該異常。如果我直接按F5調試執行者不會報該異常。

轉自:http://www.cnblogs.com/shunliy/archive/2009/12/15/1624901.html

轉自:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.