0

I don't know what the problem is exactly. I can load small jpeg files, but when I tried loading a tga file, I get the exception. I tried resizing the image but that didn't help either.

public static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size)
{
    return (System.Drawing.Image)(new Bitmap(imgToResize, size));
}

private void imageToolStripMenuItem4_Click(object sender, EventArgs e)
{

    if (tabControl1.TabCount == 0)
    {
        MessageBox.Show("Please add a form first");
        return;
    }

    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Filter = "TGA (*.tga)|*.tga|JPEG (*.jpg)|*.jpg|BITMAP FILES (*.bmp)|*.bmp|PNG (*.png)|*.png";
    openFileDialog1.FilterIndex = 1;

    if (System.Windows.Forms.DialogResult.OK == openFileDialog1.ShowDialog())
    {
        BckImageRadioBtnGrp bimrbg=new BckImageRadioBtnGrp();
        bimrbg.ShowDialog();

        string result = bimrbg.getResult();

        if (result != null)
        {
            switch (result)
            {
                case "Center"   : (GetDesignSurface(tabControl1.SelectedTab) as System.Windows.Forms.UserControl).BackgroundImageLayout = ImageLayout.Center;   break;
                case "Zoom"     : (GetDesignSurface(tabControl1.SelectedTab) as System.Windows.Forms.UserControl).BackgroundImageLayout = ImageLayout.Zoom;     break;
                case "Tile"     : (GetDesignSurface(tabControl1.SelectedTab) as System.Windows.Forms.UserControl).BackgroundImageLayout = ImageLayout.Tile;     break;
                case "Stretch"  : (GetDesignSurface(tabControl1.SelectedTab) as System.Windows.Forms.UserControl).BackgroundImageLayout = ImageLayout.Stretch;  break;
                case "None"     : (GetDesignSurface(tabControl1.SelectedTab) as System.Windows.Forms.UserControl).BackgroundImageLayout = ImageLayout.None;     break;
            }
        }

         //getting exception here. I set a small resizing size just for testing if it works. it doesn't
        System.Drawing.Image img = resizeImage(System.Drawing.Image.FromFile(openFileDialog1.FileName), new Size(100, 100));

        (GetDesignSurface(tabControl1.SelectedTab) as System.Windows.Forms.UserControl).BackgroundImage = img;
        }
}
}

}

So, the question is, how do I load an image?

Paul
  • 121
  • 1
  • 3
  • 11
  • 3
    From the documentation on [`System.Drawing.Image.FromFile`](http://msdn.microsoft.com/en-us/library/stf701f5.aspx): `OutOfMemoryException` if The file does not have a valid image format. -or- GDI+ does not support the pixel format of the file. – Tim Schmelter May 22 '14 at 08:28
  • Worth a check: [Out of memory Image.FromFile](http://stackoverflow.com/a/8285521/352101) – Bolu May 22 '14 at 08:36
  • @Tim - Is there anyway to load TGA files apart from using custom made libraries? – Paul May 22 '14 at 08:45
  • @Paul: i don't know, i've never needed to load them but apparently not. – Tim Schmelter May 22 '14 at 08:50

1 Answers1

4

OutOfMemoryException is the typical way of System.Drawing.Image.FromFile to reject an unloadable file, e.g corrupted file or unsupported pixel format, etc. The exception is quite confusing and probably related to GDI+. The size of file doesn't matter at all.

See exception section on this MSDN page : http://msdn.microsoft.com/en-us/library/stf701f5.aspx

If the problem occurs on a single image, you can try to re-save the image with a more permissive graphic design tool (if you can open it !), or you can try another picture handling library than native .Net methods, which doesn't rely on GDI+.

AFAIK, most of the time when the exception occurs there is no direct simple solution to "force" the loading of the picture.

AFract
  • 8,868
  • 6
  • 48
  • 70
  • Like @Tim commented, the .NET framework does not support .tga files – Paul May 22 '14 at 08:36
  • Not at all ? I wasn't aware of that, probably never tried it. However all advices in my answer remains relevant. – AFract May 22 '14 at 08:39