How to combine an array of png images like layers using C#?
I have an array of images named
image_<somenumber>_trans.png
All these images have transparent areas. The idea is that when put one on top each other they will form a nice looking image. But I have been getting a weird GDI+ related error (“A generic error occurred in GDI+”) and I have been going crazy. The code I'm using now can be viewed as below;
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 Bi开发者_StackOverflowtmap("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();
}
Am I doing something wrong? Thank you for your help in advance...
My suggestion is to only save the image when the whole process is completed.
Image i = new Image(...)
Graphics g = Graphics.FromImage(i)
for(...)
{
g.Draw(...)
}
i.Save(...)
Writing new Bitmap(filename)
will lock the file until you dispose the Bitmap
.
Therefore, you can't overwrite the file.
精彩评论