0

I have a qr code asp.net web form and I am getting an error

System.IO.IOException: 'The process cannot access the file 'C:\Users\Asus.DESKTOP-BTB81TA\Desktop\bikestop\bikestop\images\qr code stuff\fileName.png' because it is being used by another process.'

in one of my lines :

File.Delete(filePath2);

Here is the full code :

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.IO; //For MemoryStream
using System.Web.Services; //For WebMethod attribute
using Bytescout.BarCode;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.Text;
using System.IO;
using AForge;
using AForge.Video;
using AForge.Video.DirectShow;
using ZXing;
using ZXing.Aztec;


namespace bikestop
{

    public partial class bookRide : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        // int i = 0;
        protected void Scan_Click(object sender, EventArgs e)
        {


            Rectangle rect = new Rectangle(0, 0, 2000, 1000);
            string filePath = AppDomain.CurrentDomain.BaseDirectory;
            string filePath2;
            filePath2 = @"" + filePath + "\\images\\qr code stuff\\fileName.png";


            //do stuff
            using (Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb))
            {
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
                }
                if (System.IO.Directory.Exists(filePath2) == true)
                {
                    bmp.Save(filePath2, ImageFormat.Png);
                    bmp.Dispose();
                }
                else
                {
                    File.Delete(filePath2);
                    bmp.Save(filePath2, ImageFormat.Png);
                    bmp.Dispose();
                }

                try
                {
                    IBarcodeReader barcodeReader = new BarcodeReader();
                    var barcodeBitmap = (Bitmap)Bitmap.FromFile(filePath2);
                    var barcodeResult = barcodeReader.Decode(barcodeBitmap);
                    Output.Text = barcodeResult.Text;
                    Output.NavigateUrl = barcodeResult.Text;

                }
                catch
                {
                    Output.Text = "No barcode found";
                }
            }
        }
    }
}
nTIAO
  • 415
  • 1
  • 6
  • 15
  • Possible duplicate of [IOException: The process cannot access the file 'file path' because it is being used by another process](https://stackoverflow.com/questions/26741191/ioexception-the-process-cannot-access-the-file-file-path-because-it-is-being) – Tetsuya Yamamoto Nov 13 '18 at 02:52
  • I have read up on the post and most of them said to use the dispose function which unfortunately isn't working for me – nTIAO Nov 13 '18 at 02:58

0 Answers0