繁体   English   中英

在背景之上插入一系列较小的位图 bitmap

[英]Insert series of smaller bitmaps on top of background bitmap

这可能是一个业余问题,但我仍然被困住了......

我有一张bitmap的背景图,然后需要叠加几个小一点的位图(主要是二维码)。 事情适用于第一个插入,然后它会中断。 它编译正常,但在新的 Bitmap 行上失败,并显示异常未处理消息 System.ArgumentException:“参数无效”。

代码是这样的

        Bitmap Background_bmp= new Bitmap(File_name);
        Graphics Background_gfx = Graphics.FromImage(Background_bmp);

        for (i=1;i<=4;i++)
        {
            Bitmap Insert_image = new Bitmap(File_name[i]); 
            Print_doc_gfx.DrawImage(Insert_image, blablabla (scaling and positioning);
            Insert_image.Dispose();
        }

        Background_bmp.Save("C:\\Total image.bmp");
        Background_gfx.Dispose();
        Background_bmp.Dispose();

很简单,但它不起作用。 我很确定破损是在“新位图”片段中重复的“新”,但我不知道如何声明一次并多次使用位图......就像我说的,业余问题...

您从代码中发布的部分似乎没有引起问题。 这很可能是由代码的其他部分引起的,例如图像插入的坐标或完全不同的东西。

我使用以下代码对一张大图像和 4 张小图像进行了测试,并且没有问题:

string File_name = "background.png";
string[] File_names = new string[] { "img1.png", "img2.png", "img3.png", "img4.png" };
Bitmap Background_bmp = new Bitmap(File_name);
// can also use empty all-black image like the following line:
// Bitmap Background_bmp = new Bitmap(800, 600, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
         
Graphics Background_gfx = Graphics.FromImage(Background_bmp);

for (int i = 1; i <= File_names.Length; i++)
{
   Bitmap Insert_image = new Bitmap(File_names[i - 1]);
   Background_gfx.DrawImage(Insert_image, i * 150, i * 100);
   Insert_image.Dispose();
}

Background_gfx.Dispose();
Background_bmp.Save("Total_image.png", System.Drawing.Imaging.ImageFormat.Png);
Background_bmp.Dispose();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM