简体   繁体   中英

Getting OutOfMemoryException when I load a tga file of 1 mb in c#

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?

OutOfMemoryException is the typical way of System.Drawing.Image.FromFile to reject an unloadable file, eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM