Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 858931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:36:32+00:00 2026-05-15T08:36:32+00:00

I have been working on an image gallary. When the user uploads an Image

  • 0

I have been working on an image gallary. When the user uploads an Image I now check the size of the file. If it is less than 1MB I check to see that the file is actually an image type. Finally I resize the image to an appropriate gallary size, and create a small thumbnail of the image. However since adding in the code to check the types I have been experiancing and OutOfMemoryException.

Here’s my controller method:

        [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Upload(Image image, HttpPostedFileBase ImageFile)
    {

        if (ImageFile.ContentLength > 0)
        {
            // Get the size in bytes of the file to upload.
            int fileSize = ImageFile.ContentLength;

            // Allow only files less than 1,048,576 bytes (approximately 1 MB) to be uploaded.
            if (fileSize < 1048576)
            {
                string fileclass = "";

                using (BinaryReader r = new BinaryReader(ImageFile.InputStream))
                {
                    byte buffer = r.ReadByte();
                    fileclass = buffer.ToString();
                    buffer = r.ReadByte();
                    fileclass += buffer.ToString();
                    r.Close();
                }

                switch (fileclass)
                {
                    case "7137":
                    case "255216":
                    case "13780":
                        try
                        {
                            string path = Server.MapPath("~/Uploads/");

                            ImageFile.SaveAs(path + ImageFile.FileName);

                            ResizeImageHelper resizeImageHelper = new ResizeImageHelper();
                            resizeImageHelper.ResizeImage(path + ImageFile.FileName, path + ImageFile.FileName, 640, 480, false);
                            resizeImageHelper.ResizeImage(path + ImageFile.FileName, path + "thumb" + ImageFile.FileName, 74, 74, false);

                            image.imageLocation = ImageFile.FileName;
                            image.imageThumb = "thumb" + ImageFile.FileName;

                            imageRepository.Add(image);
                            imageRepository.Save();

                            return RedirectToAction("Index", "Home");
                        }
                        catch (Exception ex)
                        {
                            return View("Error");
                        }
                }

            }
            else
            {
                //If file over 1MB
                return View("Error");
            }
        }
        else
        {
            //If file not uploaded
            return View("Error");
        }

        return View("Error");
    }

And here is the Resize method I use:

public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
    {
        System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);

        // Prevent using images internal thumbnail
        FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
        FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

        if (OnlyResizeIfWider)
        {
            if (FullsizeImage.Width <= NewWidth)
            {
                NewWidth = FullsizeImage.Width;
            }
        }

        int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
        if (NewHeight > MaxHeight)
        {
            // Resize with height instead
            NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
            NewHeight = MaxHeight;
        }

        System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);

        // Clear handle to original file so that we can overwrite it if necessary
        FullsizeImage.Dispose();

        // Save resized picture
        NewImage.Save(NewFile);
    }

Can anyone advise with this? I am currently just playing around trying to learn new things 🙂

Thanks,

Jon

Progress I have narrowed it down to this block, when commented out things function as normal:

using (BinaryReader r = new BinaryReader(ImageFile.InputStream))
                {
                    byte buffer = r.ReadByte();
                    fileclass = buffer.ToString();
                    buffer = r.ReadByte();
                    fileclass += buffer.ToString();
                    r.Close();

                }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:36:32+00:00Added an answer on May 15, 2026 at 8:36 am

    I’m assuming this doesn’t happen the first run through, but after some time. Is this correct?

    edit: removed incorrect assumption, but there’s still an issue with IDisposable


    You’re not disposing of NewImage, and this will cause you issues in production.

    I’d normally say ‘just use a using’, but try/finally is the same thing. Refactor to us a using at your own discretion.

    System.Drawing.Image NewImage = null;
    System.Drawing.Image FullsizeImage = null;
    
    try
    {
        FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);
    
         [... snip ... ]
    
        NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
    
        // Clear handle to original file so that we can overwrite it if necessary
        FullsizeImage.Dispose();
    
        // Save resized picture
        NewImage.Save(NewFile);
    }
    finally
    {
        if (FullsizeImage != null)
            FullsizeImage.Dispose();
        if (NewImage != null)
            NewImage.Dispose();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK, I have been working on a random image selector and queue system (so
I have been working as a native C++ programmer for last few years. Now
I have been working on a web services related project for about the last
I have been working with Visual Studio (WinForm and ASP.NET applications using mostly C#)
I have been working with a string[] array in C# that gets returned from
We have been working with CVS for years, and frequently find it useful to
I have been working on some legacy C++ code that uses variable length structures
I have been working with relational databases for sometime, but it only recently occurred
I have been working with Struts for some time, but for a project I
I have been working on a childish little program: there are a bunch of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.