Out of Memory when placing image in Picturebox c#

Asked

Viewed 147 times

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".
Aqui está um screenshot do erro

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.

  • What size image are you trying to put on PictureBox?

  • @Could Vik put a full example as an answer please? The image is valid and the formats are correct...

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

1 answer

2


You’re creating an instance of FileStream and you don’t get to use it. Or load the image through the method Image.FromStream() and then you need the var fileStream that you have in the code, or directly load from the file with the method Image.FromFile()

Fromstream:

using (var 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 (*.*)|*.*";

    if (dialogo.ShowDialog() == DialogResult.OK)
    {
        string caminhoCompleto = dialogo.FileName;

        using (var fileStream = new FileStream(caminhoCompleto, FileMode.Open))
        {
            // ...          

            if (pb.Image != null)
            {
                pb.Image.Dispose();
            }
            pb.Image = Image.FromStream(fileStream);

            // Com o uso de 'using' não precisas de fazer *.Close()
            // fileStream.Close();

            //...
        }
    }
}

Fromfile:

using (var 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 (*.*)|*.*";

    if (dialogo.ShowDialog() == DialogResult.OK)
    {
        string caminhoCompleto = dialogo.FileName;

        // ...                          

        if (pb.Image != null)
        {
            pb.Image.Dispose();
        }
        pb.Image = Image.FromFile(caminhoCompleto);

        // ...
    }
}
  • Using the Image.FromStream() worked out perfectly, thank you very much.

Browser other questions tagged

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