【问题标题】:I want to merge two .png images我想合并两个 .png 图像
【发布时间】:2011-08-10 12:44:10
【问题描述】:

我想合并两个 .png 图像。但是当我保存它们时,发生了一个错误,称为 gdi+ 中发生的一般错误。我想尽快继续我的项目。请帮我。谢谢

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

            }
        }
    }

【问题讨论】:

  • 您遇到了什么错误(异常/错误消息)?当您说“合并” 2 PNG 时,您到底想实现什么?
  • 不要将文件读入变量m,而是尝试创建具有所需尺寸的新Bitmap,然后在该位图上创建Graphics。然后,您可以将两个图像都绘制到新的位图上,最后将其保存到磁盘。
  • @Yahila 错误消息是..“GD+ 中发生一般错误。我的意思是,我给了一个图像并想将另一个图像加入到基础图像中。我想你明白了跨度>
  • @Ntini.. 位图 tempBitmap = new Bitmap(imgB.Width, imgB.Height);我也使用此代码。但是最终的图像分辨率是有问题的。

标签: c#


【解决方案1】:

你没有告诉我们这个异常是在哪一行抛出的,所以我要在这里猜测。当您尝试加载/保存的图像的路径不正确时,通常会发生此错误,因此请检查ImageBackImageFore 的路径。 还要确保您尝试加载/保存到的文件未被包括您的应用程序在内的其他进程打开。

这里值得一提的是,您应该在使用完图像/图形后处理它们,例如使用using。喜欢

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

【讨论】:

  • 感谢贾拉勒。路径不正确。因为它为 .jpeg 图像和 .png 图像执行。即使我找不到错误
  • It execute for a .jpeg image and .png image 是什么意思?只有当图像格式是别的东西时才会抛出异常?
  • 我的应用程序仅针对 .jpeg 和 .png 格式正确运行。但对于其他格式,它会给出运行时错误..“GDI++ 中发生一般错误
  • 其他格式是什么?因为那时Image 类可能并不真正支持它们..
  • 如果两张图片格式相同(比如都是 .jpeg 或 .png)或者一张是 .gif 而另一张是 .png 则无法很好地执行.. if (listFilesToProtect.Items.Count> 0) { foreach(listFilesToProtect.Items 中的对象) { MergeImages(it.ToString(), txtFileProtector.Text); } } 我认为这段代码有错误
猜你喜欢
  • 2012-03-11
  • 2019-03-12
  • 1970-01-01
  • 1970-01-01
  • 2015-06-08
  • 2011-01-20
  • 1970-01-01
  • 2023-04-04
相关资源
最近更新 更多