joonshen
joonshen

Reputation: 179

c# how to set beginInvoke end?

I have code my system to save a temporary image file to a folder from the pictureBox then I have to make sure that the beginInvoke end and code the system to delete the temporary image file of that folder. Anyone know how to do that?

This is my code:

    //Image Selection End Point
    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        // Do nothing it we're not selecting an area.
        if (!RegionSelect) return;
        RegionSelect = false;

        //Display the original image.
        pictureBox1.Image = bmpImage;

        // Copy the selected part of the image.
        int wid = Math.Abs(x0 - x1);
        int hgt = Math.Abs(y0 - y1);
        if ((wid < 1) || (hgt < 1)) return;

        Bitmap area = new Bitmap(wid, hgt);
        using (Graphics g = Graphics.FromImage(area))
        {
            Rectangle source_rectangle = new Rectangle(Math.Min(x0, x1), Math.Min(y0, y1), wid, hgt);
            Rectangle dest_rectangle = new Rectangle(0, 0, wid, hgt);
            g.DrawImage(bmpImage, dest_rectangle, source_rectangle, GraphicsUnit.Pixel);
        }

        // Display the result.
        pictureBox3.Image = area;
        Bitmap bm = new Bitmap(area);
        bm.Save(@"C:\Users\Shen\Desktop\LenzOCR\TempFolder\tempPic.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
        singleFileInfo = new FileInfo(@"C:\Users\Shen\Desktop\LenzOCR\TempFolder\tempPic.jpeg");
    }

    private void ScanBT_Click(object sender, EventArgs e)
    {
        var folder = @"C:\Users\Shen\Desktop\LenzOCR\LenzOCR\WindowsFormsApplication1\ImageFile";

        DirectoryInfo directoryInfo;
        FileInfo[] files;
        directoryInfo = new DirectoryInfo(folder);
        files = directoryInfo.GetFiles("*.jpg", SearchOption.AllDirectories);

        var processImagesDelegate = new ProcessImagesDelegate(ProcessImages2);
        processImagesDelegate.BeginInvoke(files, processImagesDelegate.EndInvoke, null);
    }

    private void ProcessImages2(FileInfo[] files)
    {
        var comparableImages = new List<ComparableImage>();

        var index = 0x0;

        var operationStartTime = DateTime.Now;

        foreach (var file in files)
        {
            if (exit)
            {
                return;
            }

            var comparableImage = new ComparableImage(file);
            comparableImages.Add(comparableImage);
            index++;
        }

        index = 0;

        similarityImagesSorted = new List<SimilarityImages>();

        operationStartTime = DateTime.Now;

        var fileImage = new ComparableImage(singleFileInfo);

        for (var i = 0; i < comparableImages.Count; i++)
        {
            if (exit)
                return;

            var destination = comparableImages[i];
            var similarity = fileImage.CalculateSimilarity(destination);
            var sim = new SimilarityImages(fileImage, destination, similarity);
            similarityImagesSorted.Add(sim);
            index++;
        }

        similarityImagesSorted.Sort();
        similarityImagesSorted.Reverse();
        similarityImages = new BindingList<SimilarityImages>(similarityImagesSorted);

        con = new System.Data.SqlClient.SqlConnection();
        con.ConnectionString = "Data Source=SHEN-PC\\SQLEXPRESS;Initial Catalog=CharacterImage;Integrated Security=True";
        con.Open();

        String getImage = "SELECT ImageName, ImagePath FROM CharacterImage WHERE ImageName='" + similarityImages[0].Destination.ToString() + "'";
        String getImage1 = "SELECT ImageName, ImagePath FROM CharacterImage WHERE ImageName='" + similarityImages[1].Destination.ToString() + "'";
        String getImage2 = "SELECT ImageName, ImagePath FROM CharacterImage WHERE ImageName='" + similarityImages[2].Destination.ToString() + "'";

        SqlCommand cmd = new SqlCommand(getImage, con);
        SqlDataReader rd = cmd.ExecuteReader();

        while (rd.Read())
        {
             getPath = rd["ImagePath"].ToString();
             pictureBox4.Image = Image.FromFile(getPath);
        }
        rd.Close();

        SqlCommand cmd1 = new SqlCommand(getImage1, con);
        SqlDataReader rd1 = cmd1.ExecuteReader();

        while (rd1.Read())
        {
            getPath1 = rd1["ImagePath"].ToString();
            pictureBox5.Image = Image.FromFile(getPath1);
        }
        rd1.Close();

        SqlCommand cmd2 = new SqlCommand(getImage2, con);
        SqlDataReader rd2 = cmd2.ExecuteReader();

        while (rd2.Read())
        {
            getPath2 = rd2["ImagePath"].ToString();
            pictureBox6.Image = Image.FromFile(getPath2);
        }
        con.Close();
     }

    private void pictureBox4_Click(object sender, EventArgs e)
    {
        if (pictureBox4.Image == null)
        {
            MessageBox.Show("Nothing has been scanned yet!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        else
        {
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = "Data Source=SHEN-PC\\SQLEXPRESS;Initial Catalog=CharacterImage;Integrated Security=True";
            con.Open();
            String getText = "SELECT ImagePath, Character FROM CharacterImage WHERE ImagePath='" + getPath + "'";
            SqlCommand cmd = new SqlCommand(getText, con);
            SqlDataReader rd = cmd.ExecuteReader();

            while (rd.Read())
            {
                for (int i = 0; i < 1; i++)
                {
                    String imageChar = rd["Character"].ToString();
                    Action showText = () => ocrTB.AppendText(imageChar);
                    ocrTB.Invoke(showText);
                }
            }
            rd.Close();
        }

    }

Upvotes: 0

Views: 691

Answers (1)

driis
driis

Reputation: 164341

BeginInvoke returns you with an IAsyncResult which you can use to wait for the operation to complete:

IAsyncResult result = processImagesDelegate.BeginInvoke(files, processImagesDelegate.EndInvoke, null);

You can then check whether the operation completed using the result.IsCompleted property, or you can wait on the WaitHandle returned from result.AsyncWaitHandle.

Upvotes: 1

Related Questions