简体   繁体   English

在C#中加载1 mb的tga文件时获取OutOfMemoryException

[英]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. 我可以加载小的jpeg文件,但是当我尝试加载tga文件时,出现异常。 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+. OutOfMemoryException是System.Drawing.Image.FromFile拒绝可卸载文件(例如,损坏的文件或不受支持的像素格式等)的典型方式。该异常非常令人困惑,可能与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 请参阅此MSDN页面上的异常部分: http : //msdn.microsoft.com/zh-cn/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+. 如果问题是在单个图像上发生的,则可以尝试使用更宽松的图形设计工具(如果可以打开!)来重新保存图像,或者可以尝试使用本机.Net方法之外的其他图片处理库。不依赖GDI +。

AFAIK, most of the time when the exception occurs there is no direct simple solution to "force" the loading of the picture. AFAIK,在大多数情况下,发生异常时,没有直接简单的解决方案可以“强制”加载图片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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