title: c# A generic error occurred in GDI+
body: <p>I have this problem when I drag to select region of the image in the picturebox more than two times and run the scanning. This is an OCR system.</p>
  <h1>region OCR(Tab4_Component)</h1>
  <pre><code>    //When user is selecting, RegionSelect = true
      private bool RegionSelect = false;
      private int x0, x1, y0, y1;
      private Bitmap bmpImage;
  
      private void loadImageBT_Click(object sender, EventArgs e)
      {
          try
          {
              OpenFileDialog open = new OpenFileDialog();
              open.InitialDirectory = @&quot;C:\Users\Shen\Desktop&quot;;
  
              open.Filter = &quot;Image Files(*.jpg; *.jpeg)|*.jpg; *.jpeg&quot;;
  
              if (open.ShowDialog() == DialogResult.OK)
              {
                  singleFileInfo = new FileInfo(open.FileName);
                  string dirName = System.IO.Path.GetDirectoryName(open.FileName);
                  loadTB.Text = open.FileName;
                  pictureBox1.Image = new Bitmap(open.FileName);
                  bmpImage = new Bitmap(pictureBox1.Image);
              }
              
          }
          catch (Exception)
          {
              throw new ApplicationException(&quot;Failed loading image&quot;);
          }
      }
  
      //User image selection Start Point
      private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
      {
          RegionSelect = true;
  
          //Save the start point.
          x0 = e.X;
          y0 = e.Y;
      }
  
      //User select image progress
      private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
      {
          //Do nothing it we're not selecting an area.
          if (!RegionSelect) return;
  
          //Save the new point.
          x1 = e.X;
          y1 = e.Y;
  
          //Make a Bitmap to display the selection rectangle.
          Bitmap bm = new Bitmap(bmpImage);
          
  
          //Draw the rectangle in the image.
          using (Graphics g = Graphics.FromImage(bm))
          {
              g.DrawRectangle(Pens.Red, Math.Min(x0, x1), Math.Min(y0, y1), Math.Abs(x1 - x0), Math.Abs(y1 - y0));
          }
  
          //Temporary display the image.
          pictureBox1.Image = bm;
      }
  
      //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 &lt; 1) || (hgt &lt; 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;
          area.Save(@&quot;C:\Users\Shen\Desktop\LenzOCR\TempFolder\tempPic.jpg&quot;);
          singleFileInfo = new FileInfo(&quot;C:\\Users\\Shen\\Desktop\\LenzOCR\\TempFolder\\tempPic.jpg&quot;);
      }
  
  
             private void ScanBT_Click(object sender, EventArgs e)
      {
          var folder = @&quot;C:\Users\Shen\Desktop\LenzOCR\LenzOCR\WindowsFormsApplication1\ImageFile&quot;;
  
          DirectoryInfo directoryInfo;
          FileInfo[] files;
          directoryInfo = new DirectoryInfo(folder);
          files = directoryInfo.GetFiles(&quot;*.jpg&quot;, SearchOption.AllDirectories);
  
          var processImagesDelegate = new ProcessImagesDelegate(ProcessImages2);
          processImagesDelegate.BeginInvoke(files, null, null);     
  
          //BackgroundWorker bw = new BackgroundWorker();
          //bw.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
          //bw.RunWorkerAsync(bw);
          //bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
      }
      
      private void ProcessImages2(FileInfo[] files)
      {
          var comparableImages = new List&lt;ComparableImage&gt;();
  
          var index = 0x0;
  
          foreach (var file in files)
          {
              if (exit)
              {
                  return;
              }
  
              var comparableImage = new ComparableImage(file);
              comparableImages.Add(comparableImage);
              index++;
          }
  
          index = 0;
  
          similarityImagesSorted = new List&lt;SimilarityImages&gt;();
          var fileImage = new ComparableImage(singleFileInfo);
  
          for (var i = 0; i &lt; 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&lt;SimilarityImages&gt;(similarityImagesSorted);
  
          var buttons =
              new List&lt;Button&gt;
                  {
                      ScanBT
                  };
  
          if (similarityImages[0].Similarity &gt; 70)
          {
              con = new System.Data.SqlClient.SqlConnection();
              con.ConnectionString = &quot;Data Source=SHEN-PC\\SQLEXPRESS;Initial Catalog=CharacterImage;Integrated Security=True&quot;;
              con.Open();
  
              String getFile = &quot;SELECT ImageName, Character FROM CharacterImage WHERE ImageName='&quot; + similarityImages[0].Destination.ToString() + &quot;'&quot;;
              SqlCommand cmd2 = new SqlCommand(getFile, con);
              SqlDataReader rd2 = cmd2.ExecuteReader();
  
              while (rd2.Read())
              {
                  for (int i = 0; i &lt; 1; i++)
                  {
                      string getText = rd2[&quot;Character&quot;].ToString();
                      Action showText = () =&gt; ocrTB.AppendText(getText);
                      ocrTB.Invoke(showText);
                  }
              }
              con.Close();
          }
          else
          {
              MessageBox.Show(&quot;No character found!&quot;, &quot;Error!&quot;, MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
  
      }
      #endregion
  </code></pre>
  <p>i understand the reason it occur is that the image has been duplicated. But I have no idea how to solve it.</p>