在1个图片框中显示3个图像

时间:2017-01-07 13:02:56

标签: c# winforms

假设我有一个图片框(picturebox1),我想在该图片框中显示随机尺寸的3张图片......这怎么可能?我还没有尝试任何东西,因为我不知道从哪里开始。

2 个答案:

答案 0 :(得分:1)

见下面的代码。我使用矩形来演示而不是图像。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        const int ROWS = 3;
        const int COLUMNS = 4;
        const int WIDTH = 10;
        const int HEIGHT = 20;
        const int SPACE = 10;
        public Form1()
        {
            InitializeComponent();
            Panel panel = new Panel();
            panel.Width = COLUMNS * (WIDTH + SPACE);
            panel.Height = ROWS * (HEIGHT + SPACE);
            this.Controls.Add(panel);
            for (int rows = 0; rows < ROWS; rows++)
            {
                for (int cols = 0; cols < COLUMNS; cols++)
                {
                    PictureBox newPictureBox = new PictureBox();
                    newPictureBox.Width = WIDTH;
                    newPictureBox.Height = HEIGHT;
                    newPictureBox.Top = rows * (HEIGHT + SPACE);
                    newPictureBox.Left = cols * (WIDTH + SPACE);
                    panel.Controls.Add(newPictureBox);
                    newPictureBox.Paint +=new PaintEventHandler(pictureBox_Paint);

                }
            }
        }
        private void pictureBox_Paint(object sender, PaintEventArgs e) {
            Rectangle ee = new Rectangle(0, 0, WIDTH, HEIGHT);           
            using (Pen pen = new Pen(Color.Red, 2)) {
                e.Graphics.DrawRectangle(pen, ee);
            }
        }
     }
}

答案 1 :(得分:0)

每个图片框无法显示多个图像。你可以做的是创建一个Canvas组件,并在画布中放置三个图片框,每个图片框都有一个图像。