我有一个名为
的图像数组image_<somenumber>_trans.png
所有这些图像都有透明区域。这个想法是当把一个放在另一个上面他们会形成一个好看的形象。但我一直得到一个奇怪的GDI+相关的错误("一个通用的错误发生在GDI+")我都快疯了我现在使用的代码如下所示:
number_of_photos = 30;
Bitmap temp = new Bitmap("background.png");//some white background 640x480 pixels
temp.Save("temp.png", ImageFormat.Png);
temp.Dispose();
for (int photo_no = 0; photo_no < number_of_photos; photo_no++)
{
Bitmap temp1 = new Bitmap("temp.png");
Graphics gra = Graphics.FromImage(temp1);
Bitmap new_layer = new Bitmap("image_" + photo_no + "_trans.png");
//the images image_<photo_no>_trans.png are also 640x480 pixels
gra.DrawImage(new_layer,0,0);
temp1.Save("temp.png");//error: A generic error occurred in GDI+.
temp1.Dispose();
}
我做错了什么吗?
我的建议是只有在整个过程完成后才保存图像。
Image i = new Image(...)
Graphics g = Graphics.FromImage(i)
for(...)
{
g.Draw(...)
}
i.Save(...)
写入new Bitmap(filename)
将锁定文件,直到您释放Bitmap
。
因此,您不能覆盖文件。