【问题标题】:Release image from process form PictureBox从处理窗体 PictureBox 中释放图像
【发布时间】: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


【解决方案1】:

查看接收文件路径的Bitmap Constructor Documentation

在处理位图之前,文件保持锁定状态。

由于在 PictureBox 中使用了位图,它尚未被释放,因此文件仍被锁定,导致您的异常。

一个解决方法是从第一个创建一个新的位图,然后允许第一个处理:

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

【讨论】:

    【解决方案2】:

    加载图像而不持有它的最佳方法是这样的

    Image tempImage = Image.FromFile("image.jpg");
    Bitmap tempBitmap = new Bitmap(tempImage);
    pictureBox.Image = tempBitmap;
    

    答案的原始来源在这里https://www.codeproject.com/Questions/492654/bc-dplusdeleteplusimagepluswhichplusisplusope

    【讨论】:

      【解决方案3】:

      正如 Idle_Mind 建议的那样,而不是 Picturebox1.Image = null; 你可以使用 图片框1.Image.Dispose();

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-22
        • 1970-01-01
        • 1970-01-01
        • 2013-11-14
        • 1970-01-01
        • 1970-01-01
        • 2021-07-19
        • 1970-01-01
        相关资源
        最近更新 更多