Results 1 to 4 of 4

Thread: Load Large Image (30Meg)

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Load Large Image (30Meg)

    Ok I need to load large images up to 30Meg (PNG) and manipulate them when I use the following code I get an Out Of Memory Exception.

    Anyone have any sample code for loading a large file via stream maybe?

    Code:
    //Now load the image back in and grab the size. 
                    Image FullSizeImage = Image.FromFile(path + "\\high\\" + curPub.Image); //Error Here
    
                    int width = FullSizeImage.Width;
                    int height = FullSizeImage.Height;
    
                    //Generate the thumb first.
                    float ratio = width / 75;
    
                    int thumbWidth = 75;
                    int thumbHeight = Convert.ToInt32(height / ratio);
    
                    Image ThumbnailImage = FullSizeImage.GetThumbnailImage(thumbWidth, thumbHeight, null, IntPtr.Zero);
                    ThumbnailImage.Save(path + "\\images\\thumbnail\\" + curPub.Image, System.Drawing.Imaging.ImageFormat.Png);

  2. #2
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: Load Large Image (30Meg)

    You should not get "out of memory" for a 30meg file, If you just created/uploaded the file maybe it is not released and a file sharing issue is happening. I'm guessing because more details are needed.......

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Load Large Image (30Meg)

    Hey,

    I don't know if this will be much help or not, but these are the methods that I use for resizing images on the fly, I can't vouch for it working on a image of that size though, I have never tried it:

    Code:
            private static byte[] ResizeImageFile(byte[] imageFile, int targetSize)
            {
                using (Image oldImage = Image.FromStream(new MemoryStream(imageFile)))
                {
                    Size newSize = CalculateDimensions(oldImage.Size, targetSize);
                    using (Bitmap NewImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
                    {
                        using (Graphics canvas = Graphics.FromImage(NewImage))
                        {
                            canvas.SmoothingMode = SmoothingMode.AntiAlias;
                            canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
                            canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));
    
                            MemoryStream m = new MemoryStream();
                            NewImage.Save(m, ImageFormat.Jpeg);
                            return m.GetBuffer();
                        }
                    }
                }
            }
    Code:
            public override Stream ResizeImageFile(Stream imageFile, PhotoSize targetSize)
            {
                if (targetSize == PhotoSize.Original)
                {
                    //Return pic without resizing
                    return imageFile;
                }
                else
                {
                    Byte[] picBuff = StreamToBytes(imageFile);
    
                    switch (targetSize)
                    {
                        case PhotoSize.Large:
                            picBuff = ResizeImageFile(picBuff, 600);
                            break;
                        case PhotoSize.Medium:
                            picBuff = ResizeImageFile(picBuff, 198);
                            break;
                        case PhotoSize.Small:
                            picBuff = ResizeImageFile(picBuff, 100);
                            break;
                    }
    
                    return new MemoryStream(picBuff);
                }
            }
    Code:
        public enum PhotoSize
        {
            Small = 1,		// 100px
            Medium = 2,		// 198px
            Large = 3,		// 600px
            Original = 4	// Original Size
        }
    Code:
            private static Size CalculateDimensions(Size oldSize, int targetSize)
            {
                Size newSize = new Size();
    
                if (oldSize.Height > oldSize.Width)
                {
                    newSize.Width = Convert.ToInt32(oldSize.Width * Convert.ToSingle(targetSize / Convert.ToSingle(oldSize.Height)));
                    newSize.Height = targetSize;
                }
                else
                {
                    newSize.Width = targetSize;
                    newSize.Height = Convert.ToInt32(oldSize.Height * Convert.ToSingle(targetSize / Convert.ToSingle(oldSize.Width)));
                }
    
                return newSize;
            }
    Hope that might help!!

    Gary

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Load Large Image (30Meg)

    If this is happening right after an upload, chances are that the handle on the file hasn't been released, so you're attempting to read from a 0byte file.

    If you need to manipulate it, do it straight from the byte array so that the file save and file manipulation happen separately.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width