OutOfMemory exception when loading an image in .Net

Posted by Ben on Stack Overflow See other posts from Stack Overflow or by Ben
Published on 2010-04-19T20:03:29Z Indexed on 2010/04/19 20:13 UTC
Read the original article Hit count: 528

Filed under:
|

Hi,

Im loading an image from a SQL CE db and then trying to load that into a PictureBox.

I am saving the image like this:

if (ofd.ShowDialog() == DialogResult.OK)
            {
                picArtwork.ImageLocation = ofd.FileName;
                using (System.IO.FileStream fs = new System.IO.FileStream(ofd.FileName, System.IO.FileMode.Open))
                {
                    byte[] imageAsBytes = new byte[fs.Length];
                    fs.Read(imageAsBytes, 0, imageAsBytes.Length);
                    thisItem.Artwork = imageAsBytes;
                    fs.Close();
                }
            }

and then saving to the Db using LINQ To SQL.

I load the image back like so:

using (FileStream fs = new FileStream(@"C:\Temp\img.jpg", FileMode.CreateNew ,FileAccess.Write ))
                {
                    byte[] img = (byte[])encoding.GetBytes(ThisFilm.Artwork.ToString());
                    fs.Write(img, 0, img.Length);
                }

but am getting an OutOfMemoryException. I have read that this is a slight red herring and that there is probably something wrong with the filetype, but i cant figure what.

Any ideas?

Thanks picArtwork.Image = System.Drawing.Bitmap.FromFile(@"C:\Temp\img.jpg");

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET