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();
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();
}
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