Parameter is not Valid - Picturebox in C#

Asked

Viewed 52 times

2

I am making a TIFF file viewer in Visualstudio 2017, in C#language. I can open the TIFF file, if multipage will separate it by pages and I can show each page and browse through them. My problem is when I’m gonna open another TIFF.

I close the file and when I open another, I get the error

Parameter is not Valid.

on the line made visible by PictureBox. Could you tell me what’s wrong with my code or whether I should do things differently? Thank you in advance.

private void openToolStripMenuItem_Click(object sender, EventArgs e)  
{  
    pbtiff.Hide();  
    OpenFileDialog dialogo = new OpenFileDialog();  
    dialogo.Title = "Search files";   
    dialogo.InitialDirectory = @"E:\";   
    dialogo.Filter = "PDF Files(.pdf)|*.pdf|Image Files (.bmp,.jpg,.png,.tiff,.tif) |*.bmp;*.jpg;*.png;*tiff;*tif";   
    DialogResult answer = dialogo.ShowDialog();  
    if (answer == DialogResult.OK)  
    {  
        string fullPath = dialogo.FileName;   
        openFile = fullPath;  
        using (var fileStream = new FileStream(ficheiroaberto, FileMode.Open))  
        {  
            type = Path.GetExtension(caminhoCompleto);  
            if (type == ".tif" || type == ".tiff")  
            {  
                up.Visible = true;  
                up.Enabled = false;  
                down.Visible = true;  
                pbtiff.Show(); //-> o erro está nesta linha, quando a abro pela segunda vez
                if (pbtiff.Image != null)  
                {  
                    pbtiff.Image.Dispose();  
                }  
                pbtiff.Image = System.Drawing.Image.FromStream(fileStream);  
                SplitTiffFinal(fileStream);  
                filestiff = GetFilesFinal();  
                up.Visible = true;  
                up.Enabled = false;  
                down.Visible = true;  
             }  
         }  
     }  
 }

And the code to my close button:

private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (pbtiff.Image != null)
    {
            pbtiff.Image.Dispose();
            pbtiff.InitialImage = null;
            pbtiff.Hide();
            up.Visible = false;
            down.Visible = false;
            filestiff.Clear();
            idx = 0;
            type = "";

            DirectoryInfo path = new DirectoryInfo(@"temporarioviewer");
            DeletingFiles(path); 
    }
    else
        {
            MessageBox.Show("Deve abrir um ficheiro primeiro para poder fechá-lo.", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
  • Sofia, good morning! You wouldn’t have to give Dysplasia every time instead of clear and then start the process again?

  • @Sofia, what is this "IF" without parameters?

  • @Fabioln is right, I was wrong to copy and edit the code.. That IF was the most! My mistake

  • @Angelosimonato the "clear" is of a List<string>, not from Picturebox. But it would put Disposis when pressing the button to open a TIFF?

  • @Sofiarodrigues, Ispose would be to discard the object, then you fill it again as you did in the first round. I’m looking at the line that gave the error and that you signaled.

  • @Angelosimonato but I already have an "IF" to do Ispose if Picturebox has an image, and a Ispose when I close the file... Where would I put another Dysplasia, if I already have one at home event?

Show 1 more comment

1 answer

1


The mistake is why you’re calling the method Show() after making Dispose() in the Image property of the picturebox (through the close button).

I can’t explain what’s causing the mistake internally, but if you call Show() after inserting a new image you will no longer have this problem:

if (pbtiff.Image != null)  
{  
    pbtiff.Image.Dispose();  
}  
pbtiff.Image = System.Drawing.Image.FromStream(fileStream);
pbtiff.Show(); //-> o erro está nesta linha, quando a abro pela segunda vez
  • That’s right, it was my very misguided mistake... thank you for your help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.