0

I'm developing a project of image merging.(Image Combine) 1st foreground image is on the(0,0) position of background image. The position is changing by using a numericUpdown Control. The code is below

private void nudXPositions_ValueChanged(object sender, EventArgs e)
    {

        {

            lock (typeof(FrmImageMerge))
            {
                nudXPositions.Maximum = imgBackWidth;

                if (pbMergeImagePreview.Image != null)
                    pbMergeImagePreview.Image.Dispose();

                pbMergeImagePreview.InitialImage = null;

                posX = decimal.ToInt16(nudXPositions.Value);
                posY = decimal.ToInt16(nudYPosition.Value);
                MergeImages(tbxBackImage.Text, tbxForeImage.Text);
                lblMergeImagePreview.Text = "";
                pbBackgroundImagePreview.Refresh();
                try
                {
                  Thread.Sleep(100);
                    Image image1 = Image.FromFile(tempName);
                    this.pbMergeImagePreview.Image = image1;
                    pbBackgroundImagePreview.Refresh();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                btnSaveImageAs.Enabled = true;
            }

        }

    }

then call MergeImage() method. That method is below

 private void MergeImages(string ImageBack,string ImageFore)
    {
         pbMergeImagePreview.Image = null;
         pbMergeImagePreview.Refresh();
        // try
         //{
             backExtension = Path.GetExtension(tbxBackImage.Text);
            System.Drawing.Graphics myGraphic = null;
             Image imgB;
             imgB = Image.FromFile(ImageBack);
             Image imgF;
             imgF = Image.FromFile(ImageFore);
            Image m;
            m = Image.FromFile(ImageBack);
            myGraphic = System.Drawing.Graphics.FromImage(m);
            myGraphic.DrawImageUnscaled(imgB,0,0);
             myGraphic.DrawImageUnscaled(imgF,posX,posY);

            myGraphic.Save();
            getMyDocument = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
           tempName = string.Format("{0}{1}{2}{3}{4}{5}",getMyDocument,"\\","Merge Image","\\","Imageback",backExtension);

     if (File.Exists(tempName))
        {
             File.Delete(tempName);
         }
        m.Save(tempName);       

               imgB.Dispose();
               imgF.Dispose();
               m.Dispose();
               myGraphic.Dispose();


         //}
        //catch (Exception ex)
    //  {
    //      MessageBox.Show(ex.Message);
    //}
   }

When program is execute and when change the position foreimage is change according to the position. It's save to a temporary image. Then If we change the position again temporary image is recreated and it replace to the old image. But when I do this procedure for a some time there will be an exception; "ExternalExcetion has Occurred". Also this is a "A generic error occurred in GDI+." I tried a lot and search browser. It I couldn't solve my problem. Thank You

H H
  • 263,252
  • 30
  • 330
  • 514
Lakshani
  • 213
  • 1
  • 3
  • 6
  • Can I suggest putting in usings() instead of all the disposes at the end? It might make it a little clearer and ensure you're dispoing of everything correctly. What's the memory usage of your application like one you've ran this method a lot? – Ian Aug 24 '11 at 09:40
  • what do you mean by usings()? is that a method which can be used instead of dispose()? about memory usage: in the beggining the memory usage was 6mb and when the exception occurs it was 18mb. – Lakshani Aug 24 '11 at 10:04
  • 'using(Image imgB = Image.FromFile(ImageBack)) { // more code in here }' will automatically dispose of imgB when it leaves scope. – Ian Aug 24 '11 at 10:07

1 Answers1

2

The Generic Error in GDI+ is a horrible error because it gives no information about what error occurred.

However, a common cause is that loading a bitmap from a file can leave the file open, so any subsequent attempt to open it results in an Access Denied.

See these related answers:
- A generic error occurred in GDI+
- Generic GDI+ Error

Just search for "Generic Error GDI+" in StackOverflow and you'll find quite a few hits.

Community
  • 1
  • 1
Jason Williams
  • 56,972
  • 11
  • 108
  • 137