What causes the 'file used by another process' exception in my file.MoveTo() method and how can I solve it?

huangapple go评论73阅读模式
英文:

What causes the 'file used by another process' exception in my file.MoveTo() method and how can I solve it?

问题

我更新了项目,现在读取 QR 的部分在一个方法里,我现在使用 IronOcr 来读取 QR 码,因为它有助于提高图像质量,但我肯定引起了一个锁定,但我不知道如何解决它。

public string leggiQrTramiteTesseract(string filename)
{
    imgAnteprima.Source = new BitmapImage(new Uri(filename));
    var input = new OcrInput((filename));
    input.DeNoise();
    input.Deskew();

    var ocrTesseract = new IronTesseract();
    ocrTesseract.Configuration.ReadBarCodes = true;
    var ocrResult = ocrTesseract.Read(input);
    input.Dispose();

    if (ocrResult.Barcodes.Length > 0)
    {
        var barcode = ocrResult.Barcodes[0];
        return barcode.ToString();
    }
    return "";
}
英文:

i updated the project, now the part that reads qr is in a metod, i use IronOcr now to read the qr code because it helps improving image quality, i definetly cause a lock but i don't know how to solve it

public string leggiQrTramiteTesseract(string filename)
    {           
        imgAnteprima.Source = new BitmapImage(new Uri(filename));
        var input = new OcrInput((filename));
        input.DeNoise();
        input.Deskew();

        var ocrTesseract = new IronTesseract();
        ocrTesseract.Configuration.ReadBarCodes = true;
        var ocrResult = ocrTesseract.Read(input);
        input.Dispose();
                                                                          
        if (ocrResult.Barcodes.Length > 0)
        {
            var barcode = ocrResult.Barcodes[0];
            return barcode.ToString();
        }
        return "";  

答案1

得分: 1

以下是要翻译的内容:

Image.FromFile() 的文档说明:

> 文件保持锁定状态,直到图像被释放为止。

这意味着,如果您将尝试移动文件的代码放在获取位图的 using 块之外,您的问题很可能会得到解决。

英文:

The documentation for Image.FromFile() states:

> The file remains locked until the Image is disposed.

Which means your issue is likely resolved if you put the code that tries to move the file outside the using block that acquires the bitmap.

答案2

得分: 1

// Bitmap 类持有通过内部流打开的文件。在移动文件之前,您需要首先将其释放。
// 通过合并分支并使用 File.Move 和 Path.Combine,您还可以清理代码。
for (int i = 0; i < files.Count; i++)
{
    Result barcodeResult;
    using (Bitmap barcodeBitmap = (Bitmap)Bitmap.FromFile(files[i]))
    {
        var barcodeReader = new BarcodeReader();
        barcodeResult = barcodeReader.Decode(barcodeBitmap);
    }

    // 逻辑本身有点可疑:我认为您只想在结果不为 null 时移动文件,因此您可以在 if 开头创建 "date" 值。
    if (barcodeResult != null)
    {
        date = DateTime.Now.ToString("dd_MM_yyyy_HH-mm-ss");
        date = Path.Combine(destDir, date);
        Directory.CreateDirectory(date);
        File.Move(files[i], date);
    }
}
英文:

The Bitmap class is holding the file open via an internal stream. You need to dispose it first before moving the file.

You can also cleanup the code by merging branches and using File.Move and Path.Combine.

for (int i = 0;i&lt;files.Count;i++)
{
    Result barcodeResult;
    using (Bitmap barcodeBitmap = (Bitmap)Bitmap.FromFile(files[i]))
    {
        var barcodeReader = new BarcodeReader();
        barcodeResult = barcodeReader.Decode(barcodeBitmap);
    }

    File.Move(files[i], date);
    if (barcodeResult != null)
    {
        date = DateTime.Now.ToString(&quot;dd_MM_yyyy_HH-mm-ss&quot;);
        date = Path.Combine(destDir, date);
        Directory.CreateDirectory(date);
    }
}

The logic itself is a bit suspect: I would have thought you would only want to move the file if the result was not null, and that you could therefore create the date value at the beginning of the if.

huangapple
  • 本文由 发表于 2023年5月25日 20:52:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定