简体   繁体   English

C#OpenFileDialog无法打开文件

[英]C# OpenFileDialog not opening file

I'm trying to use openFileDialog to open a Bitmap image and place it on my form. 我正在尝试使用openFileDialog打开一个位图图像并将其放在我的窗体上。 My form construtor... 我的表格建设者...

 public Form1()
    {
        InitializeComponent();
        drawing = new Bitmap(drawingPanel.Width, drawingPanel.Height, drawingPanel.CreateGraphics());
        Graphics.FromImage(drawing).Clear(Color.White);

        // set default value for line thickness
        tbThickness.Text = "5";
    }

... opens a new form with a blank screen, and I can draw on it using the mouse and various color selector buttons. ...打开一个空白屏幕的新表单,我可以使用鼠标和各种颜色选择器按钮在其上进行绘制。 I then save the file with this method: 然后,我使用此方法保存文件:

private void btnSave_Click(object sender, EventArgs e)
    {
        // save drawing
        if (file == null)   // file is a FileInfo object that I want to use
                            // to check to see if the file already exists 
                            // I haven't worked that out yet
        {
            drawing.Save("test.bmp");
            //SaveBitmap saveForm = new SaveBitmap();
            //saveForm.Show();
        }
        else
        {
            drawing.Save(fi.FullName);
        }
    }

The image does save to the debug folder as a .bmp file. 该映像确实以.bmp文件的形式保存到调试文件夹中。 Then I use OpenFileDialog to open the file: 然后,我使用OpenFileDialog打开文件:

private void btnOpen_Click(object sender, EventArgs e)
    {
        FileStream myStream;
        OpenFileDialog openFile = new OpenFileDialog();
        openFile.Filter = "bmp files (*.bmp)|*.bmp";

        if (openFile.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if ((myStream = (FileStream)openFile.OpenFile()) != null)
                {
                    using (myStream)
                    {
                        PictureBox picBox = new PictureBox();
                        picBox.Location = drawingPanel.Location;
                        picBox.Size = drawingPanel.Size;
                        picBox.Image = new Bitmap(openFile.FileName);
                        this.Controls.Add(picBox);
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
    }

What happes is that OpenFileDialog box comes up. 遗憾的是出现了OpenFileDialog框。 When I select the file test.bmp, the screen goes away and then reappears, when I select it again, the OpenFileDialog window goes away and I'm back to my form with no image. 当我选择文件test.bmp时,屏幕消失,然后重新出现,当再次选择它时,OpenFileDialog窗口消失,并且我回到没有图像的表单。 Was hoping for some pointers. 希望有一些指示。 No compile or runtime errors. 没有编译或运行时错误。

Why are you calling ShowDialog() twice? 为什么要两次调用ShowDialog()

Just call ShowDialog once, so it doesn't open twice , like you indicated. 只需调用一次ShowDialog ,它就不会打开两次 ,就像您指出的那样。

From MSDN : MSDN

OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "bmp files (*.bmp)|*.bmp";

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
    try
    {
        if ((myStream = openFileDialog1.OpenFile()) != null)
        {
            using (myStream)
            {
                // Insert code to read the stream here.
                PictureBox picBox = new PictureBox();
                picBox.Location = drawingPanel.Location;
                picBox.Size = drawingPanel.Size;
                picBox.Image = new Bitmap (myStream);
                this.Controls.Add(picBox);
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
    }
}

You open a dialog panel, then when it closes you check to see if the result was OK; 打开一个对话框,然后关闭对话框,检查结果是否正确; then you open another new dialog in the using block; 然后在using块中打开另一个新对话框; then you assign the image result of that to the PictureBox , and then throw everything away when the using block disposes. 然后将其图像结果分配给PictureBox ,然后在using块放置时丢弃所有内容。

You're calling ShowDialogue twice which is likely the source of your problem. 您两次调用ShowDialogue ,这很可能是问题的根源。 Just use the following code, remove everything else from the method. 只需使用以下代码,从方法中删除所有其他内容。 Your use of using is also incorrect. 您对using也不正确。 it does clean up which is disposing of the results. 它确实清理正在处理的结果。 You need to refactor or remove the using statement. 您需要重构或删除using语句。

private void btnOpen_Click(object sender, EventArgs e)
{
     OpenFileDialog dlg = new OpenFileDialog()
     {
            dlg.Title = "Open Image";
            dlg.Filter = "bmp files (*.bmp)|*.bmp";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                PictureBox picBox = new PictureBox();
                picBox.Location = drawingPanel.Location;
                picBox.Size = drawingPanel.Size;
                picBox.Image = new Bitmap (dlg.FileName);
                this.Controls.Add(picBox);
            }
      }
  }

The code above works but is without clean up or error handling. 上面的代码有效,但是没有清理或错误处理。 I'll leave that to you. 我留给你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM