选择多张图片并将其设置在不同的图片框中

时间:2015-10-06 11:10:45

标签: c# picturebox

我想选择多个图像并将它们显示在不同的图片框中。为此,我的代码是

 private void btnChoose_Click(object sender, EventArgs e)
    {
       DialogResult dr = this.openFileDialog1.ShowDialog();
       if (dr == System.Windows.Forms.DialogResult.OK)
       {
           // Read the files
           foreach (String file in openFileDialog1.FileNames)
           {
               // Create a PictureBox.
               try
               {
                   PictureBox pb = new PictureBox();
                   Image loadedImage = Image.FromFile(file);
                   pb.Height = loadedImage.Height;
                   pb.Width = loadedImage.Width;
                   pb.Image = loadedImage;
                   flowLayoutPanel1.Controls.Add(pb);
               }
               catch (SecurityException ex)
               {
                   // The user lacks appropriate permissions to read files, discover paths, etc.
                   MessageBox.Show("Security error. Please contact your administrator for details.\n\n" +
                       "Error message: " + ex.Message + "\n\n" +
                       "Details (send to Support):\n\n" + ex.StackTrace
                   );
               }
               catch (Exception ex)
               {
                   // Could not load the image - probably related to Windows file system permissions.
                   MessageBox.Show("Cannot display the image: " + file.Substring(file.LastIndexOf('\\'))
                       + ". You may not have permission to read the file, or " +
                       "it may be corrupt.\n\nReported error: " + ex.Message);
               }
           }
       }

    }

但它不适合我。我有8 pictureboxes其中7 picturebox是小尺寸,1 picturebox尺寸是width = 319,height= 277,所以请帮助我选择多个图像并在不同的图片框中显示它们。pictureboxes中有7个小FlowlayoutPanelpicturebox不在任何面板中

0 个答案:

没有答案