c# - 如何将多个 pictureBox 图像添加到面板的背景图像中?

标签 c# winforms panel

您好 stackoverflow 社区。

我不想将每个 pictureBoxes 图像绘制到 Panel1,而是想将每个 pictureBox 图像(pictureBox1、pictureBox2、pictureBox3)添加到位图,然后将该位图设置为 Panel1 的背景图像。

这可能吗? 谢谢 enter image description here

最佳答案

这是我发现的将多个图像组合成一个位图的方法

public static System.Drawing.Bitmap CombineBitmap(Image[] files)
        {
            //read all images into memory
            List<System.Drawing.Bitmap> images = new List<System.Drawing.Bitmap>();
            System.Drawing.Bitmap finalImage = null;

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

                foreach (Image image in files)
                {
                    //create a Bitmap from the file and add it to the list
                    System.Drawing.Bitmap bitmap = new System.Drawing.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 System.Drawing.Bitmap(width, height);

                //get a graphics object from the image so we can draw on it
                using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage))
                {
                    //set background color
                    g.Clear(System.Drawing.Color.Black); // Change this to whatever you want the background color to be, you may set this to Color.Transparent as well

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

                return finalImage;
            }
            catch (Exception ex)
            {
                if (finalImage != null)
                    finalImage.Dispose();

                throw ex;
            }
            finally
            {
                //clean up memory
                foreach (System.Drawing.Bitmap image in images)
                {
                    image.Dispose();
                }
            }
        }

您需要创建一个图像数组才能使其工作,然后您可以尝试这个

        Image[] Files = new Image[3]; // Create a new array of maximum 3 indices
        Files[0] = pictureBox1.Image;
        Files[1] = pictureBox2.Image;
        Files[2] = pictureBox3.Image;
        panel1.BackgroundImage = CombineBitmap(Files); //Add the combined bitmap to the BackgroundImage

谢谢

祝您有美好的一天,抱歉造成误解:)

关于c# - 如何将多个 pictureBox 图像添加到面板的背景图像中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12771817/

相关文章:

c# - 如何将消息放入死信并在azure函数中经过一段时间后处理它

c# - 在不使用 fiddler 的情况下将 HttpWebRequest 视为 GetResponse 之前的字符串

c# - 在 LINQ to SQL 查询中从多个表检索数据

ajax - Firefox HTTP 连接在阻塞阶段花费大量时间

javascript - 如何通过javascript动态创建 Bootstrap 面板?

c# - 在 asp.net 网站中禁用 *.java 文件编译

c# - 从 Windows 窗体应用程序 C# 控制控制台应用程序

c++ - 关于windows窗体中多线程和后台工作线程的一些问题

Extjs:两个面板在窗口调整大小时相应地调整大小

sql-server - 如何从 VB.NET 表单设置日期格式