Secretario Financiero
Secretario Financiero

Reputation: 95

Why do I keep getting a 'System.OutOfMemoryException'?

Here is my Code, I am trying to stitch two pictures together and as soon as I get to line 42 "Image img = Image.FromFile(file.FullName);" I get an out of memory error. What should I do?

namespace Practicing_Stiching
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void cmdCombine_Click(object sender, EventArgs e)
        {
            //Change the path to location where your images are stored.
            DirectoryInfo directory = new DirectoryInfo(@"C:\Users\Elder Zollinger\Desktop\Images");
            if (directory != null)
            {
                FileInfo[] files = directory.GetFiles();
                CombineImages(files);
            }

        }
        private void CombineImages(FileInfo[] files)
        {
            //change the location to store the final image.
            string finalImage = @"C:\Users\Elder Zollinger\Desktop\Images";
            List<int> imageHeights = new List<int>();
            int nIndex = 0;
            int width = 0;
            foreach (FileInfo file in files)
            {
                Image img = Image.FromFile(file.FullName);
                imageHeights.Add(img.Height);
                width += img.Width;
                img.Dispose();
            }
            imageHeights.Sort();
            int height = imageHeights[imageHeights.Count - 1];
            Bitmap img3 = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(img3);
            g.Clear(SystemColors.AppWorkspace);
            foreach (FileInfo file in files)
            {
                Image img = Image.FromFile(file.FullName);
                if (nIndex == 0)
                {
                    g.DrawImage(img, new Point(0, 0));
                    nIndex++;
                    width = img.Width;
                }
                else
                {
                    g.DrawImage(img, new Point(width, 0));
                    width += img.Width;
                }
                img.Dispose();
            }
            g.Dispose();
            img3.Save(finalImage, System.Drawing.Imaging.ImageFormat.Jpeg);
            img3.Dispose();
            imageLocation.Image = Image.FromFile(finalImage);
        }
    }
}

Upvotes: 1

Views: 702

Answers (2)

justin.m.chase
justin.m.chase

Reputation: 13665

Get more memory!

But seriously, you don't appear to be filtering out files that are not image files. There are some hidden files in folders that you may be trying to open as an image accidentally, such as "Thumbs.db". Make sure that the file is an image with something like if (Path.GetExtension(file.FullPath) != ".png") continue;.

Also, on objects that you are calling .Dispose() on, consider wrapping them in a using() instead. For example:

using(var img = Image.FromFile(file.FullPath))
{
    // ...
}

Upvotes: 0

Luaan
Luaan

Reputation: 63732

That's likely GDI playing tricks on you.

You see, when GDI encounters an unknown file, it will quite likely cause an OutOfMemoryException. Since you're not filtering the input images at all, I'd expect that you're simply grabbing a non-image file (or an image type that GDI doesn't understand).

Oh, and a bit sideways - make sure you set JPEG quality when saving JPEGs - the default is something like 75, which is rather bad for a lot of images. And please, do use using - it's very handy to ensure proper and timely clean-up :)

Upvotes: 4

Related Questions