开发者

I want to merge two .png images

开发者 https://www.devze.com 2023-03-27 02:56 出处:网络
I want to merge two .png images. but when I ga to save them there occurred an error called a generic error occurred in gdi+. I want to continue my project as soon as possible. please help me. Thanks

I want to merge two .png images. but when I ga to save them there occurred an error called a generic error occurred in gdi+. I want to continue my project as soon as possible. please help me. Thanks

private void MergeImages(string ImageBack,string ImageFore)
        {
            try
            {
            System.Drawing.Graphics myGraphic = null;
        Image imgB;// =new Image.FromFile(ImageBack);
        imgB = Image.FromFile(ImageBack);
            //FileInfo fileInfoBack = new FileInfo(ImageBack);

        Image imgF;// =new Image.FromFile(ImageBack);
        imgF = Image.FromFile(ImageFore);
        //Bitmap tempBitmap = new Bitmap(imgB.Width,开发者_开发技巧 imgB.Height,imgB.PixelFormat );
          //  tempBitmap.Save("a"+fileInfoBack.Extension);

        Image m;
        m = Image.FromFile(ImageBack);
      //  m = Image.FromFile("a" + fileInfoBack.Extension);
        myGraphic = System.Drawing.Graphics.FromImage(m);
        myGraphic.DrawImageUnscaled(imgB,0,0);
        myGraphic.DrawImageUnscaled(imgF,posX,posY);
        myGraphic.Save();


        m.Save(ImageBack.Replace(".jpg",".jpeg"),System.Drawing.Imaging.ImageFormat.Jpeg);
        //m.Save(ImageBack, System.Drawing.Imaging.ImageFormat.Png);
      // m.Save("d.png", System.Drawing.Imaging.ImageFormat.Png);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

    private void btnFileProtector_Click(object sender, System.EventArgs e)
    {
        if (openFileDialog1.ShowDialog()==DialogResult.OK)
        {
            txtFileProtector.Text=openFileDialog1.FileName;
        }
    }

    private void btnFilesToProtect_Click(object sender, System.EventArgs e)
    {
        listFilesToProtect.Items.Clear();
        if (openFileDialog2.ShowDialog()==DialogResult.OK)
        {
            if (openFileDialog2.FileNames.Length>0)
            {
                for(int i=0;i<openFileDialog2.FileNames.Length;i++)
                {
                    listFilesToProtect.Items.Add(openFileDialog2.FileNames[i]);
                }
            }
        }
    }

    private void btnLoad2_Click(object sender, System.EventArgs e)
    {
        posX = int.Parse(textBox1.Text);
        posY = int.Parse(textBox2.Text);
       // heightBackImage = int.Parse(textBox3.Text);
       // widthBackImage = int.Parse(textBox4.Text);

        if (listFilesToProtect.Items.Count>0)
        {
            foreach(object it in listFilesToProtect.Items)
            {
                MergeImages(it.ToString(), txtFileProtector.Text);

            }
        }
    }


You didn't show us at which line this exception is thrown, So I am going to guess here. This error usually occurs when the path of the image that you are trying to load/save is not correct, so check the path for both ImageBack and ImageFore. Also make sure that the file you are trying to load/save to is not open by another process including your application.

It is worth to mentioned here that you should dispose the images/graphics when you finish from using them, for instance by use using. like

using(Image imgB = Image.FromFile(ImageBack))
{ 
    //the code that is using the imgB here
}
0

精彩评论

暂无评论...
验证码 换一张
取 消