Domanda

I want to show four image in four picture box at a same time using (open file dialog and

menu strip) in c#, I used this code to print picture but not correct

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;
        }

I need a method to appear four image at four picture box in one time

È stato utile?

Soluzione

Your variables called name, str, str2, img2 and n are all superfluous to what you are trying to achieve.

Try this:

 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;

            }

        }

You could even eliminate the img variable and assign the picture box images directly:

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");
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top