一次在四个图片框中显示四个图像

时间:2014-05-04 16:58:01

标签: c# picturebox openfiledialog menustrip

我想使用(打开文件对话框和

)同时在四个图片框中显示四个图像

菜单条)在c#中,我使用此代码打印图片但不正确

private void fileToolStripMenuItem_Click(object sender, EventArgs e)
    {
        string str = null;
        string str2 = null;
        Bitmap img,img2;
        int n=1;

        OpenFileDialog opendialog1 = new OpenFileDialog();

        opendialog1.InitialDirectory = "D:\\frames";

        opendialog1.Filter = "Image File|*.bmp;";

        opendialog1.Title = " Open Image file";

        if (opendialog1.ShowDialog() == DialogResult.OK)
        {  
            img = new Bitmap(opendialog1.FileName);
            pictureBox1.Image = img;
            str = opendialog1.FileName;

            string name = (n++).ToString().PadLeft(4, '0');
            img2 = new Bitmap("D:\\frames"+name+".bmp");
            pictureBox2.Image = img2;
            str2 = opendialog1.FileName;

            name = (n++).ToString().PadLeft(4, '0');
            img2 = new Bitmap("D:\\frames" + name + ".bmp");
            pictureBox3.Image = img2;
            str2 = opendialog1.FileName;

            name = (n++).ToString().PadLeft(4, '0');
            img2 = new Bitmap("D:\\frames" + name + ".bmp");
            pictureBox4.Image = img2;
            str2 = opendialog1.FileName;
        }

我需要一种方法一次在四个图片框中显示四个图像

1 个答案:

答案 0 :(得分:1)

你的变量名为name,str,str2,img2和n都是你想要实现的多余的。

试试这个:

 private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {

            Bitmap img;

            OpenFileDialog opendialog1 = new OpenFileDialog();

            opendialog1.InitialDirectory = "D:\\frames";

            opendialog1.Filter = "Image File|*.bmp;";

            opendialog1.Title = " Open Image file";

            if (opendialog1.ShowDialog() == DialogResult.OK)

            {

                img = new Bitmap(opendialog1.FileName);
                pictureBox1.Image = img;

                img = new Bitmap("D:\\frames\\0001.bmp");
                pictureBox2.Image = img;

                img = new Bitmap("D:\\frames\\0002.bmp");
                pictureBox3.Image = img;

                img = new Bitmap("D:\\frames\\0003.bmp");
                pictureBox4.Image = img;

            }

        }

您甚至可以消除img变量并直接分配图片框图像:

pictureBox1.Image = new Bitmap(opendialog1.FileName);
pictureBox2.Image = new Bitmap("D:\\frames\\0001.bmp");
pictureBox3.Image = new Bitmap("D:\\frames\\0002.bmp");     
pictureBox4.Image = new Bitmap("D:\\frames\\0003.bmp");