2015-11-04 46 views
0

我遇到錯誤「System.IO.IOException:進程無法訪問文件'I:\ User \ Image \ BarCodes \ QTY.png',因爲它正在被另一個進程使用。在System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath)「從處理表單發佈圖像PictureBox

我知道這個錯誤是由於同一程序的其他程序正在使用該過程,或至少這是我的想法。

下面是導致此錯誤的按鈕

private void createbtn_Click(object sender, EventArgs e) 
    { 
     InsertBarCodeImage(); 

    } 

private void InsertBarCodeImage() 
    { 
     try 
     { 
      if (qtytxt.Text != String.Empty) 
      { 
       Picturebox1.Image = null; 

       BarCode insertBarCode = new BarCode(); 

       insertBarCode.InsertBarCode(qtytxt.Text, Picturebox1.Image); 

       Picturebox1.Image = new Bitmap(insertBarCode.BARCODEQUANTITYNAMERUTE); 

       Picturebox1.SizeMode = PictureBoxSizeMode.StretchImage; 

       MessageBox.Show("Label created"); 

      } 
      else 
      { 
       MessageBox.Show("Please enter qty", "Verify", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
      } 
     } 
     catch (Exception e) 
     { 
      MessageBox.Show(e.ToString()); 
     } 
    } 

class BarCode 
{ 
    public string BARCODEQUANTITYNAMERUTE { get; set; } 

    public void InsertBarCode(string quantity, Image quantityImage) 
    { 

     BARCODEQUANTITYNAMERUTE = @"I:\User\Image\BarCodes\QTY.png"; 

     try 
     { 

      Bitmap quantityBarCode = CreateBarCode("*" + quantity + "*"); 

      if (System.IO.File.Exists(BARCODEQUANTITYNAMERUTE)) 
       System.IO.File.Delete(BARCODEQUANTITYNAMERUTE); 

      quantityBarCode.Save(BARCODEQUANTITYNAMERUTE, System.Drawing.Imaging.ImageFormat.Png); 
      quantityImage = new Bitmap(BARCODEQUANTITYNAMERUTE); 

     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

    private Bitmap CreateBarCode(string text) 
    { 
     Bitmap barcode = new Bitmap(1, 1); 
     const string freeThreeOfNine = "Free 3 of 9"; 
     Font fontthreeofnine = new Font(freeThreeOfNine, 40, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); 
     Graphics graphics = Graphics.FromImage(barcode); 

     SizeF datasize = graphics.MeasureString(text, fontthreeofnine); 

     barcode = new Bitmap(barcode, datasize.ToSize()); 

     graphics = Graphics.FromImage(barcode); 

     graphics.Clear(Color.White); 

     graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel; 

     graphics.DrawString(text, fontthreeofnine, new SolidBrush(Color.Black), 0, 0); 

     graphics.Flush(); 

     fontthreeofnine.Dispose(); 
     graphics.Dispose(); 

     return barcode; 
    } 
} 

所以,當點擊事件發生第二次錯誤發生,在線

if (System.IO.File.Exists(BARCODEQUANTITYNAMERUTE)) 
      System.IO.File.Delete(BARCODEQUANTITYNAMERUTE); 

它試圖刪除第一個點擊事件的前一個圖像,我該如何停止該過程以便它能夠刪除圖像,並用當前的文本值重新創建它並在PictureBox上顯示它?

我使用

PictureBox1.Image = null; 

,但沒有運氣

任何幫助,在此我將不勝感激。

此外,如果你可以很好地指出評論的任何良好做法,它會幫助我。

編輯(幫助從@HansPassant)在類改變InsertBardCode

public Image InsertBarCode(string barCodeString) 
    { 
     Bitmap barCodeImage = CreateBarCode("*" + barCodeString + "*"); 

     return barCodeImage; 
    } 

接縫的工作還不錯

+0

這始於一個錯誤,應該是'無效InsertBarCode(字符串量,出圖像量圖像)'。還是更好一些:'Image InsertBarCode(string quantity)'。你在旁邊爲解決這個設計錯誤所做的事情讓你陷入困境。 –

+0

查看編輯@HansPassant,如果你喜歡併發佈一個關於這個答案,所以我可以把它給你?謝謝 – MoralesJosue

回答

2

見接收一個文件路徑Bitmap Constructor Documentation

文件保持鎖定直到位圖被丟棄。

由於位圖正在PictureBox中使用,因此它尚未處理,因此該文件仍處於鎖定狀態,從而導致您的異常。

一個解決方法是從所述第一創建一個新的位圖,然後允許第一處置:

using (var img = new Bitmap(insertBarCode.BARCODEQUANTITYNAMERUTE)) 
{ 
    Picturebox1.Image = new Bitmap(img); 
} 
0
作爲

建議Idle_Mind,代替 Picturebox1.Image =空的; 你可以使用 Picturebox1.Image.Dispose();