Click here to Skip to main content
15,908,675 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void MergeImages(string ImageBack,string ImageFore)
		{
                       try
             {
                 string outputFile = Path.GetFileName(tbxImageName.Text.Trim());
                 string fullFile = Path.GetFullPath(tbxImageName.Text);
                 string backFile = Path.GetFileName(tbxBackImage.Text.Trim());
                 string fullBack = Path.GetFullPath(tbxBackImage.Text);
                  System.Drawing.Graphics myGraphic = null;
                 Image imgB;
		  imgB = Image.FromFile(ImageBack);
                 FileInfo fileInfoBack = new FileInfo(ImageBack);
                 FileInfo fileInfoForward=new FileInfo(ImageFore);
                 string aa= Path.GetFullPath(tbxImageName.Text);
                 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();
               int opnselctn = openFileDialog2.FilterIndex;
			    switch(opnselctn)
                {
                   case 1:
                       m.Save(ImageBack.Replace(fullBack, fullFile), System.Drawing.Imaging.ImageFormat.Jpeg);
                     break;
                  case 2:
                      m.Save(ImageBack.Replace(fullBack, fullFile), System.Drawing.Imaging.ImageFormat.Bmp);
                     break;
                  case 3:
                      m.Save(ImageBack.Replace(fullBack, fullFile), System.Drawing.Imaging.ImageFormat.Gif);
                    break;
                  case 4:
                    m.Save(ImageBack.Replace(fullBack, fullFile), System.Drawing.Imaging.ImageFormat.Png);
                    break;
                   }
                   imgB.Dispose();
                   imgF.Dispose();
                 m.Dispose();
           }
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}

1stly I merge 2 images by giving 2 images input and x,y position. Then merge image display. Again, if i want to merge the both previous images and the new x,y positions and save the image from previous saving name there is occurred an issue.
It is "A generic error occurred in GDI+"
My merge method is shown in above. What's reason for my trouble???
Posted
Updated 17-Aug-11 18:11pm
v2

1 solution

When I have come across this problem, it has always been because I've tried to save a file in a folder that doesn't exist.

An example:

C#
image.Save(@"c:\Documents\Test\Test.bmp",...)


This fails with 'A generic error occurred in GDI+' if the folders 'C:\Documents' or 'C:\Documents\Test' do not exist.

Verify the complete path you are trying to write to exists. You may wish to start by verifying that your string replacements are working as you expect.
 
Share this answer
 

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