title: Release image from process form PictureBox
body: <p>I having trouble with error "System.IO.IOException: The process cannot access the file 'I:\User\Image\BarCodes\QTY.png' because it is being used by another process.at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)"</p>
  
  <p>I know this error is caused because some other program of the same program is using the process, or at least that's what I think.</p>
  
  <p>Here is the button that is causing this error</p>
  
  <pre><code>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());
          }
      }
  </code></pre>
  
  <p>Class</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>So the error happens when the click event happens a second time, on line</p>
  
  <pre><code>if (System.IO.File.Exists(BARCODEQUANTITYNAMERUTE))
              System.IO.File.Delete(BARCODEQUANTITYNAMERUTE);
  </code></pre>
  
  <p>its trying to delete the previous image of the first click event, how can i stop the process so it is able to delete the image and re create it with the current Text value and show it on the PictureBox???</p>
  
  <p>I'm using</p>
  
  <pre><code>PictureBox1.Image = null;
  </code></pre>
  
  <p>but no luck</p>
  
  <p>Any help on this I would appreciate it.</p>
  
  <p>Also if you could be nice enough to point out any good practices on the comments, it would help me out.</p>
  
  <p>Edit (Help from @HansPassant) changed InsertBardCode in Class</p>
  
  <pre><code>public Image InsertBarCode(string barCodeString)
      {
          Bitmap barCodeImage = CreateBarCode("*" + barCodeString + "*");
  
          return barCodeImage;
      }
  </code></pre>
  
  <p>seams to work pretty good </p>