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)"
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.
Here is the button that is causing this error
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
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;
}
}
So the error happens when the click event happens a second time, on line
if (System.IO.File.Exists(BARCODEQUANTITYNAMERUTE))
System.IO.File.Delete(BARCODEQUANTITYNAMERUTE);
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???
I'm using
PictureBox1.Image = null;
but no luck
Any help on this I would appreciate it.
Also if you could be nice enough to point out any good practices on the comments, it would help me out.
Edit (Help from @HansPassant) changed InsertBardCode in Class
public Image InsertBarCode(string barCodeString)
{
Bitmap barCodeImage = CreateBarCode("*" + barCodeString + "*");
return barCodeImage;
}
seams to work pretty good
See the Bitmap Constructor Documentation that receives a file path:
The file remains locked until the Bitmap is disposed.
Since the bitmap is being used in the PictureBox, it hasn't been disposed and therefore the file is still locked causing your exception.
One fix is to create a new Bitmap from the first, then allow the first to dispose:
using (var img = new Bitmap(insertBarCode.BARCODEQUANTITYNAMERUTE))
{
Picturebox1.Image = new Bitmap(img);
}