简体   繁体   中英

Insert series of smaller bitmaps on top of background bitmap

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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