Displaying images on a form using code

I was looking if someone can give me some help with my code.
I need to know what the best way would be to have a dialog box that returns an array of image filenames and use that array to create a new picture box for each image that was selected and display it on the form Should i use a for loop or a foreach loop I think I just need help figuring out the best way to create a new picture box for each image.
What I plan to do after the images are displayed on the form is let the user select which images he would like removed. Once all the he wants are selected and displayed on the form I will then copy the images to a new directory.

Here is what I have so far...

private void btnAddImages_Click(object sender, EventArgs e)

{

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

{

   foreach (string images in dlgBoxSelectImage.FileNames)

   {

         //Not Sure what should go here...

   }

}

}




Answer this question

Displaying images on a form using code

  • Joe Mirwaisi

    Thanks for the help, I was thinking i had to create an array of pictureboxes and add eachone to the array through the loop.
    I do have one question about your solution, doesn't it create multiple 'pb' objects and all with the same name

    I also have another code question, I couldn't find out how to do this anywhere online, but how do you find the number of filenames that were selected when you allow a dialog box to have multiple selections

    -You have been very helpful
    -Thanks

    -Mike

  • reyghie

    Here is the resulting code you helped me with, thanks. I also posted a resulting picture of the code on my blog: http://learningcode.blogspot.com/ 

    Also, is there an easier way of retrieving the number of image files besides keeping a tally in the foreach loop

    Thanks


    private
    void btnAddImages_Click(object sender, EventArgs e)

    {

    int selectedImage = 0;

    int imageLocationX = 13;

    int imageLocationY = 132;

    PageGenerator ActivePage = new PageGenerator();

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

    {

    foreach (string newImage in dlgBoxSelectImage.FileNames)

    {

    //Creating a new image and displaying it

    PictureBox pb = new PictureBox();

    pb.Image = Image.FromFile(newImage);

    this.Controls.Add(pb);

    //Changing the picture properties

    pb.Location = new Point(imageLocationX, imageLocationY);

    pb.SizeMode = PictureBoxSizeMode.Zoom;

    pb.Size = new Size(88, 88);

    //View number of items selected

    Console.WriteLine(selectedImage);

    //Setting Properties for next image

    ++selectedImage;

    if (selectedImage%4==0)

    {

    imageLocationY += 90;

    imageLocationX = 13;

    }

    else

    {

    imageLocationX += 90;

    }

    }

    }

    }



  • Srivatsn

    Hi,


    Here's a code that Displays a picture in the picturebox given a filename....

    pictureBox1.Image = Drawing.Image.FromFile("C:\\myPic.jpg");


    It's your decision if you try to loop, But I suggest just display it in a listbox... let the user choose on what he wants to view...





    cheers,


    Paul June A. Domag

  • Chris Ward

    Hi,


    Here's code for Location and size:


    pictureBox1.Location = Drawing.Point(100,100);
    pictureBox1.Size = Drawing.Size(100,100);




    cheers,


    Paul June A. Domag

  • windozer_

     Mike F wrote:
    I was looking if someone can give me some help with my code.
    I need to know what the best way would be to have a dialog box that returns an array of image filenames and use that array to create a new picture box for each image that was selected and display it on the form Should i use a for loop or a foreach loop I think I just need help figuring out the best way to create a new picture box for each image.
    What I plan to do after the images are displayed on the form is let the user select which images he would like removed. Once all the he wants are selected and displayed on the form I will then copy the images to a new directory.

    Here is what I have so far...

    private void btnAddImages_Click(object sender, EventArgs e)

    {

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

    {

       foreach (string images in dlgBoxSelectImage.FileNames)

       {

             //Not Sure what should go here...

       }

    }

    }


    Mike,

       What you want to do is create a new PictureBox, add it to the form, and then set the image.  So, you can do this in your loop:


    PictureBox pb = new PictureBox();pb.Image = Image.FromFile("filename");this.Controls.Add(pb);
     


       You will also want to add code that will set the position of the imageboxes, so that they are aligned nicely.

       Hope this helps.

              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard.caspershouse.com

  • celina

    Hi,

      Sorry to interrupt the thread.

     You will also want to add code that will set the position of the imageboxes, so that they are aligned nicely.
    --> How do you automatically align them nicely Mind to share with me please Smile

    Thanks.

  • Displaying images on a form using code