How to overlay images in C#?

There is a PictureBox, a background image is loaded into it. You need to add a few more images on top, and they should not completely overlap the background. All images are in. png format, they show various geometric shapes in black, the rest of the space is a transparent background. All images are the same size. Basically, I need to implement the ability to show different layers as in image editors. No ideas yet =(

Author: PastoriXx, 2014-06-06

2 answers

Perhaps this option will suit you. Overlay one PictureBox on another. Place the pictureBox1 on the form and add the code to the form upload:

        // создаем слой
        PictureBox pictureBox2 = new PictureBox
        {
            // задаем размер контейнера, как у родительского контейнера
            Size = pictureBox1.Size,
            // можно управлять положением слоя, относительно родительского контейнера
            Location = new Point(0, 0), 
            // задаем прозрачность фону
            BackColor = Color.Transparent,
            // изображение слоя
            Image = Resources.neddedImage 
        };

        // добавляем слой в родительский контейнер
        pictureBox1.Controls.Add(pictureBox2);

Here's another example. I Googled it, found a function that will suit you, and changed it a little.

    public static Bitmap CombineBitmap(IEnumerable<string> files)
    {
        //read all images into memory
        List<Bitmap> images = new List<Bitmap>();
        Bitmap finalImage = null;

        try
        {
            int width = 0;
            int height = 0;

            foreach(string image in files)
            {
                // create a Bitmap from the file and add it to the list
                Bitmap bitmap = new Bitmap(image);

                // update the size of the final bitmap
                width += bitmap.Width;
                height = bitmap.Height > height ? bitmap.Height : height;

                images.Add(bitmap);
            }

            // create a bitmap to hold the combined image
            finalImage = new Bitmap(width, height);

            // get a graphics object from the image so we can draw on it
            using(Graphics g = Graphics.FromImage(finalImage))
            {
                // set background color
                g.Clear(Color.Transparent);

                // go through each image and draw it on the final image
                foreach(Bitmap image in images)
                {
                    g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height));
                }
            }

            return finalImage;
        }
        catch(Exception)
        {
            if(finalImage != null) finalImage.Dispose();
            throw;
        }
        finally
        {
            // clean up memory
            foreach(Bitmap image in images)
            {
                image.Dispose();
            }
        }
    }

Click on the button or upload the form:

        // объединяем картинки
        Bitmap bitmap = CombineBitmap(new[] { @"a:\1.png", @"a:\2.png" });

        // загружаем результат в PictureBox
        pictureBox1.Image = bitmap;

        // сохраняем в файл
        bitmap.Save(@"a:\3.png");

I tested it, everything works as you need. Use Google ;)

 4
Author: spart, 2014-06-06 17:02:58

I would dynamically create a PictureBox for each image, set the PictureBox property.BackColor = Color. Transparent, the order of display on the form was controlled using the BringToFront(), SendToBack () methods.

 0
Author: Chaek, 2014-06-07 06:18:53