简体   繁体   中英

c# openFileDialog IndexOutofRange error

I have an issue getting a openFileDialog to even show.

Here is my current situation:

I have a WinForms program with multiple forms, but they all run in the same thread. Currently, I have two forms that has a saveFileDialog and openFileDialog each.

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.

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()

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.

  • 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.

  • 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.

  • 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. But unlike his issue, though I do have 2 saveFileDialogs, and 1 other openFileDialogs, they do not fail at all.

You haven't prompted the user to select a file... you need to show the OpenFileDialog .

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

Also, OpenFile() doesn't do much by itself. You're not doing anything with the Stream it creates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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