如何使用C#组合像图层的png图像数组? [英] How to combine an array of png images like layers using C#?

查看:160
本文介绍了如何使用C#组合像图层的png图像数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列图像名为

image_<somenumber>_trans.png

所有这些图像都有透明区域。这个想法是,当把一个放在彼此顶部
时,它们将形成漂亮的图像。但我得到了一个奇怪的GDI +相关错误(GDI +中发生了一般错误)
我一直在发疯。我现在使用的代码可以在下面查看;

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

我做错了什么?感谢您的帮助...

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(...)

这篇关于如何使用C#组合像图层的png图像数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆