Click here to Skip to main content
15,911,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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

C#
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 ImageFore 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;

External Exception 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
Posted
Updated 23-Aug-11 23:48pm
v4

You should load images in your memory. Image.FromFile lock it thats why you can't save it.

Read image using for eg. FileStream and then use Image.FromStream. It might help you.
 
Share this answer
 
v2
It could be a permissions problem (trying to save the file to a folder you don't have privileges for) when trying to write the file, or that you're trying to save the image back to the same file if you didn't actually close the stream used to load the file to begin with.
 
Share this answer
 
v2
Go through http://support.microsoft.com/?id=814675[^] and use the workaround mentioned there.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900