I am following this tutorial and downloaded source code to practice, and it works. The problem occurs when I rewrite the code: just one image is added instead of all the selected images. What am I doing wrong here?

private void button1_Click(object sender, EventArgs e)
{

    ofd.Filter = "Images (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" +
"All files (*.*)|*.*";
    ofd.Multiselect = true;

    if (ofd.ShowDialog() == DialogResult.OK)
    {

        foreach (string name in ofd.FileNames)
        {

            PictureBox imageControl = new PictureBox();
            imageControl.Width = 100;
            imageControl.Height = 100;
            Image.GetThumbnailImageAbort CallBck = new Image.GetThumbnailImageAbort(ThumbnailCallback);
            Bitmap myBitmap = new Bitmap(name);

            Image img = myBitmap.GetThumbnailImage(97, 97, CallBck, IntPtr.Zero);
            imageControl.Image = img;
            panel1.Controls.Add(imageControl);
        }
    }
}
有帮助吗?

解决方案

I'll bet they are all being added but they're just all going on top of each other at location (0,0) in the panel (you should step through your code to check this though).

The solution: Either manually specify a location for each new PictureBox, or use a layout control such as a FlowLayoutPanel.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top