none
Cannot open a closed file. RRS feed

  • Question

  • Ok, I am new to C# and I am trying to make a basic picture viewer that does nothing but view a picture. Here is my code so far:

     

     

    public partial class Form1 : Form

    {

    Stream myStream = null;

    public Form1()

    {

    InitializeComponent();

    }

    public void openToolStripMenuItem1_Click(object sender, EventArgs e)

    {

    OpenFileDialog myOpenDialog = new OpenFileDialog();

    string myPicturesFilesPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);

    myOpenDialog.InitialDirectory = myPicturesFilesPath;

    myOpenDialog.Filter = "All files (*.*)|*.*";

    myOpenDialog.FilterIndex = 1;

    myOpenDialog.RestoreDirectory = true;

    if (myOpenDialog.ShowDialog() == DialogResult.OK)

    {

    try

    {

    if ((myStream = myOpenDialog.OpenFile()) != null)

    {

    using (myStream)

    {

    int Length = 256;

    Byte[] buffer = new Byte[Length];

    int n = myStream.Read(buffer, 0, Length);

    }

    }

    }

    catch (Exception ex)

    {

    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);

    }

    }

    }

    private void picture_Click(object sender, EventArgs e)

    {

    Image.FromStream(myStream);

    }

    }


    It runs just fine without any errors, but when I run it and click the picture box, it gives me a error that says this: "Unhandled exception has occurred in you rapplication. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will close immediately. Cannot access a closed file."

     

    Any help would be appreciated.

    Thanks.

     

     

    Sunday, May 20, 2007 3:59 AM

Answers

  • Have you checked FileName property of the OpenFileDialog? Something like this:

    Code Snippet

    if (myOpenDialog.ShowDialog() == Dialogresult.OK)
    {
        this.fileName = myOpenDialog.FileName;
    } // if



    best,
    Sunday, May 20, 2007 3:55 PM

All replies

  • What exactly are you trying to do? If you only click on the picture, than stream is not opened at all, since it is opened in the first handler. Also, why do you read 256 bytes of image in the first handler?

    If you need two handlers, than in the first you just show the open file dialog and remember the filename of the selected image (this should also be a class variable!). In the second handler, you could then use selected image name in Image.FromFile to read the image.

    cheers,
    Sunday, May 20, 2007 2:04 PM
  • Ok, then how would I have it remember the filename that was clicked into a variable?

     

    Here is what I have now:

     

     

    Code Snippet

    public partial class Form1 : Form

    {

    string fileName;

    public Form1()

    {

    InitializeComponent();

    }

    public void openToolStripMenuItem1_Click(object sender, EventArgs e)

    {

    OpenFileDialog myOpenDialog = new OpenFileDialog();

    string myPicturesFilesPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);

    myOpenDialog.InitialDirectory = myPicturesFilesPath;

    myOpenDialog.Filter = "All files (*.*)|*.*";

    myOpenDialog.FilterIndex = 1;

    myOpenDialog.RestoreDirectory = true;

    if (myOpenDialog.ShowDialog() == DialogResult.OK)

    {

    }

    }

    }

     

     

     Thanks

    Sunday, May 20, 2007 2:39 PM
  • Have you checked FileName property of the OpenFileDialog? Something like this:

    Code Snippet

    if (myOpenDialog.ShowDialog() == Dialogresult.OK)
    {
        this.fileName = myOpenDialog.FileName;
    } // if



    best,
    Sunday, May 20, 2007 3:55 PM