所选的多个文件未显示在图片框中 [英] multiple files selected are not shown in picture boxes

查看:75
本文介绍了所选的多个文件未显示在图片框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//--form is having one flowLayoutPanel1 one fileopen dialog box and 1 button

private void Form1_Load(object sender, EventArgs e)
{
    InitializeOpenFileDialog();
}
private void InitializeOpenFileDialog()
{
    // Set the file dialog to filter for graphics files.
    this.openFileDialog1.Filter =
        "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" +
        "All files (*.*)|*.*";
    // Allow the user to select multiple images.
    this.openFileDialog1.Multiselect = true;
    this.openFileDialog1.Title = "My Image Browser";
}
private void selectFilesButton_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);
            }
        }
    }

推荐答案

您的代码对我来说很好.确保您的FlowLayoutPanel正确地锚定或停靠,将AutoSize设置为True,或者布局面板足够大以适合所有图片框,否则将看不到它们.
以下是一些您可以尝试的示例.
Your code works fine for me. Make sure your FlowLayoutPanel is either Anchored or Docked properly, you have AutoSize set to True or your layout panel is large enough to fit all your picture boxes onto it or you will not see them all.
Here are some examples of some things you can try.
//Set anchor to all sides
flowLayoutPanel1.Anchor = 
    AnchorStyles.Bottom | AnchorStyles.Top | 
    AnchorStyles.Left | AnchorStyles.Right;







or

//set dock to entire form
flowLayoutPanel1.Dock = DockStyle.Fill;







or

flowLayoutPanel1.AutoSize = true; //set AutoSize to True


VS 2008 C#运行最后一个图像的程序
VS 2008 C# PROG ON RUNNING SHOWS LAST IMAGE


这篇关于所选的多个文件未显示在图片框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆