【发布时间】:2015-11-04 00:17:19
【问题描述】:
我遇到错误“System.IO.IOException:该进程无法访问文件“I:\User\Image\BarCodes\QTY.png”,因为它正在被另一个进程使用。在 System.IO.__Error .WinIOError(Int32 errorCode, String maybeFullPath)"
我知道这个错误是因为同一程序的其他程序正在使用该进程,或者至少我是这么认为的。
这是导致此错误的按钮
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;
但没有运气
如有任何帮助,我将不胜感激。
另外,如果你能很好地指出 cmets 上的任何好的做法,那将帮助我。
编辑(来自@HansPassant 的帮助)更改了类中的 InsertBardCode
public Image InsertBarCode(string barCodeString)
{
Bitmap barCodeImage = CreateBarCode("*" + barCodeString + "*");
return barCodeImage;
}
接缝效果很好
【问题讨论】:
-
这开始于一个错误,它应该是
void InsertBarCode(string quantity, out Image quantityImage)。或者更好:Image InsertBarCode(string quantity)。您接下来为解决该设计错误所做的工作给您带来了麻烦。 -
请参阅编辑@HansPassant,如果您愿意并在此发布答案,以便我可以给您???谢谢
标签: c# visual-studio file-io picturebox