在背景位图顶部插入一系列较小的位图

发布于 2025-01-10 14:41:52 字数 747 浏览 6 评论 0原文

这可能是一个业余问题,但我仍然陷入困境......

我有一个背景位图图像,然后需要叠加一些较小的位图(主要是二维码)。第一次插入时一切正常,然后就崩溃了。它编译正常,但在新位图行上失败,并显示异常未处理消息 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();

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

this may be an amateur question but I'm still stuck...

I have a background bitmap image, and then need to super-impose a few smaller bitmaps (mostly qr codes). Things work for the 1st insert, and then it breaks. It compiles OK, but it fails on the new Bitmap line with a Exception Unhandled message System.ArgumentException: 'Parameter is not valid.'

The code is something like

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

Simple enough, and yet it doesn't work. I'm pretty sure the breakage is over the repeated "new" in the "new Bitmap" piece, but I don't know how to declare once and use many times when it comes to bitmaps... Like I said, amateur question...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

带刺的爱情 2025-01-17 14:41:52

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

我使用以下代码对一张大图像和 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();

The parts you posted from your code do not appear to be causing the problem. It's most likely caused by other parts of the code, such as the coordinates of the image insertion or something totally different.

I tested using the following code with one large image and 4 small images and it worked without problems:

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文