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?
– Angelo Simonato
@Sofia, what is this "IF" without parameters?
– FabioIn
@Fabioln is right, I was wrong to copy and edit the code.. That IF was the most! My mistake
– Sofia Rodrigues
@Angelosimonato the "clear" is of a
List<string>
, not from Picturebox. But it would put Disposis when pressing the button to open a TIFF?– Sofia Rodrigues
@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.
– Angelo Simonato
@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?
– Sofia Rodrigues