2
I’m trying to open a JPG, PNG, BMP or TIFF image on PictureBox
, but when I try to put this image, which was opened earlier, gives the error of "Out of memory".
My code is to open the image and put it in Picturebox, but it’s always giving that error...
My question is whether I should use some kind of Stream
or if I have an error in the code that makes this exception appear.
This is my code:
private void search_Click(object sender, EventArgs e)
{
pb.Hide();
wb.Hide();
OpenFileDialog dialogo = new OpenFileDialog();
dialogo.Title = "Procurar arquivos no computador";
dialogo.InitialDirectory = @"E:\";
dialogo.Filter = "Ficheiros PDF (.pdf)|*.pdf|Ficheiros de Imagem (.bmp,.jpg,.png,.tiff,.tif) |*.bmp;*.jpg;*.png;*tiff;*tif|Todos os arquivos (*.*)|*.*";
DialogResult resposta = dialogo.ShowDialog();
if (resposta == DialogResult.OK)
{
string caminhoCompleto = dialogo.FileName;
ficheiroaberto = caminhoCompleto;
using (var fileStream = new FileStream(ficheiroaberto, FileMode.Open))
{
tipo = Path.GetExtension(caminhoCompleto);
if (tipo == ".pdf")
{
upbut.Hide();
downbut.Hide();
pb.Hide();
wb.Show();
wb.Navigate(ficheiroaberto);
fileStream.Close();
}
else if (tipo == ".bmp" || tipo == ".jpg" || tipo == ".png")
{
upbut.Hide();
downbut.Hide();
wb.Hide();
pb.Show();
if(pb.Image != null)
{
pb.Image.Dispose();
}
pb.Image = Image.FromFile(ficheiroaberto);
fileStream.Close();
}
else if (tipo == ".tif" || tipo == ".tiff")
{
upbut.Show();
upbut.Enabled = false;
downbut.Show();
wb.Hide();
pb.Show();
if (pb.Image != null)
{
pb.Image.Dispose();
}
pb.Image = Image.FromFile(ficheiroaberto);
SplitTiffFinal(ficheiroaberto);
filestiff = GetFilesFinal();
upbut.Show();
downbut.Show();
fileStream.Close();
}
}
}
}
Thanks in advance.
dc. Image = Image.Fromstream(fileStream); should solve your problem... according to the documentation this error may be: The file does not have a Valid image format. -or- GDI+ does not support the pixel format of the file.
– vik
What size image are you trying to put on
PictureBox
?– João Martins
@Could Vik put a full example as an answer please? The image is valid and the formats are correct...
– Sofia Rodrigues
@Joãomartins The image size may vary, it is not always the same size.. I have tried 1024 x 768, 1400 x 934, 655 x 393, among others...
– Sofia Rodrigues