简体   繁体   English

c#openFileDialog IndexOutofRange错误

[英]c# openFileDialog IndexOutofRange error

I have an issue getting a openFileDialog to even show. 我有一个问题甚至无法显示openFileDialog。

Here is my current situation: 这是我目前的情况:

I have a WinForms program with multiple forms, but they all run in the same thread. 我有一个具有多种形式的WinForms程序,但是它们都在同一线程中运行。 Currently, I have two forms that has a saveFileDialog and openFileDialog each. 当前,我有两种形式,每种形式都有一个saveFileDialog和openFileDialog。

For the first form, which is the one that opens upon starting, both the saveFileDialog and openFileDialog works fine, but for the second form, the openFileDialog refuses to even open. 对于第一种形式(即在启动时打开的形式),saveFileDialog和openFileDialog都可以正常工作,但是对于第二种形式,openFileDialog甚至拒绝打开。

Here is the stack trace I am getting right now: 这是我现在得到的堆栈跟踪:

A first chance exception of type 'System.IndexOutOfRangeException' occurred in System.Windows.Forms.dll at System.Windows.Forms.OpenFileDialog.OpenFile() 类型为“ System.IndexOutOfRangeException”的第一次机会异常发生在System.Windows.Forms.OpenFileDialog.OpenFile()的System.Windows.Forms.dll中

Here is the code where the issue occurs: 这是发生问题的代码:

private void loadScreenshotToolStripMenuItem_Click(object sender, EventArgs e)
    {
        try
        {
            bmpOpenFileDialog = new OpenFileDialog();
            bmpOpenFileDialog.Filter = "Bitmap|*.bmp;*.dib|Exchangable Image Format|*.exif|Icon|*.ico|JPEG|*.jpg;*.jpeg;*" +
    ".jpe;*.jfif|GIF|*.gif|PNG|*.png|All files|*.*";
            this.bmpSaveFileDialog.Title = "Load Screenshot";
            bmpOpenFileDialog.OpenFile();
        }
        catch (Exception ex)
        {
            MessageBox.Show("\nReport this error to the creator:\n\n" + ex);
            System.Diagnostics.Debug.WriteLine(ex.StackTrace);
        }
    }

If the user clicks ok, this would have been triggered: 如果用户单击“确定”,则将被触发:

private void bmpOpenFileDialog_FileOk(object sender, CancelEventArgs e)
    {
        Image tempIMG = Image.FromFile(bmpOpenFileDialog.FileName);
        oriBmp = new Bitmap(tempIMG);
        prntscrPictureBox.Image = oriBmp;
        saveScreenshotToolStripMenuItem.Enabled = true;
        zoomInToolStripMenuItem.Enabled = true;
        zoomOutToolStripMenuItem.Enabled = true;
        originalZoomToolStripMenuItem.Enabled = true;
        fullSizeToolStripMenuItem.Enabled = true;
        customToolStripMenuItem.Enabled = true;
        zToolStripStatusLabel.Text = "Zoom Level: " + zoomFactor.ToString("2F");
    }
  • I originally used a openFileDialog straight out of the Toolbox, with its properties changed, before attempting to try another way as you can see now, after it gave me the issue. 我最初是在工具箱外直接使用openFileDialog,并更改了其属性,然后在尝试解决此问题之前尝试尝试另一种您可以看到的方式。

  • Another error it gave me at some point was about the file not found, as if the openfiledialog was trying to open a nonexistent file before even showing. 它在某个时候给我的另一个错误是关于找不到文件,好像openfiledialog在显示之前试图打开一个不存在的文件。

  • I am also using quite a fair bit of p/Invokes in the other forms, as well as a few in the form where the code above resides in. They deal with bitmaps and the screens though. 我还使用其他形式的相当多的p / Invokes,以及上面代码所在的形式的一些p / Invokes。尽管它们处理位图和屏幕。

  • I hope this is not too much information regarding the issue I am having. 我希望这不是关于我遇到的问题的太多信息。 If it is not enough, please tell me what else you would like me to provide. 如果还不够,请告诉我您还希望我提供什么。

  • I am aware this question may seem similar to this question ( Stackoverflow/CLR Error in C# OpenFileDialog ), but that question has no accepted answer yet. 我知道这个问题似乎类似于这个问题( C#OpenFileDialog中的Stackoverflow / CLR错误 ),但是该问题还没有被接受的答案。 But unlike his issue, though I do have 2 saveFileDialogs, and 1 other openFileDialogs, they do not fail at all. 但是与他的问题不同,尽管我确实有2个saveFileDialogs和1个其他openFileDialogs,但它们完全不会失败。

You haven't prompted the user to select a file... you need to show the OpenFileDialog . 您尚未提示用户选择文件...您需要显示OpenFileDialog

...
bmpOpenFileDialog.ShowDialog();  // <-- you forgot this line
bmpOpenFileDialog.OpenFile();

Also, OpenFile() doesn't do much by itself. 另外, OpenFile()本身并不能做很多事情。 You're not doing anything with the Stream it creates. 您对它创建的Stream不做任何事情。

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

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