2

I want to resize the image in my website, but when I using Bitmap to load a image of 14032*19864(png extension), an OutOfMemoryException is thrown. My compiler configuration is any cpu. I was doubting whether the running environment is x64. the code is below:

public ActionResult BimDWGViewer()
{
    Viewer.Uri uri = null;
    string url = Request.Params["u"];
    uri = new Viewer.Uri("image@"+url);
    int width = Int32.Parse(Request.Params["w"]);
    int height = Int32.Parse(Request.Params["h"]);
    Nebula.Nexus.Helpers.ModelUriTranslator.TranslateUri(uri);
    if (uri.IsFileProtocol)
    {
        string path = uri.Path;
        System.Drawing.Bitmap image_source = new System.Drawing.Bitmap(path);
        System.Drawing.Bitmap image_result = new System.Drawing.Bitmap(width,height);
        using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image_result))
        {
            g.DrawImage(image_source, new System.Drawing.Rectangle(0, 0, width, height), new System.Drawing.Rectangle(0, 0, image_source.Width, image_source.Height), System.Drawing.GraphicsUnit.Pixel);
        }
        MemoryStream output = new MemoryStream();
        image_result.Save(output, System.Drawing.Imaging.ImageFormat.Png);
        byte[] res = output.ToArray();
        output.Dispose();
        image_source.Dispose();
        image_result.Dispose();
        return new FileContentResult(res, "image/png");
    }

}

The exception occurs in the line of

System.Drawing.Bitmap image_source = new System.Drawing.Bitmap(path);
13
  • Have you tried to open the Task Manager and see how much memory your program uses? Commented Jan 3, 2016 at 14:43
  • It is possible, Graphics wasnt created to handle such images. Use ImageMagick imagemagick.org/script/index.php Commented Jan 3, 2016 at 14:48
  • Is the path valid? Invalid paths will throw this error in my experience Commented Jan 3, 2016 at 14:52
  • Do what PhotoShop does and use image tiling Commented Jan 3, 2016 at 14:53
  • the memory is enough. Commented Jan 3, 2016 at 14:54

2 Answers 2

2

Make sure you have the gcAllowVeryLargeObjects element set to true in your config file.

There's a 2 GB max for individual allocations in .NET (even when running as a 64-bit process) and it's very possible that one of the classes you're using is doing something internally that bumps into this limit. It's a pretty common problem, and fixing your config file should get you around it.

Update: Per the comments below, the problem that @majing ran into was that Visual Studio was launching his web app in a 32-bit edition of IIS Express. Configuring VS to launch IIS as a 64-bit process fixed the issue.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot. Now I figure out that It throw such an exception when I allocate 400MB memory. And I set the gcAllowVeryLargeObjects element to true in web.config, but it dosen't work.My complier configuration is "any cpu" and I disabled the "Prefer 32 bit".What should I do next?
So you're running this out of a web app then? If you're launching out of Visual Studio right now then the default version of IIS Express that VS launches is 32-bit. You can make VS launch a 64-bit server in Tools->Options->ProjectsAndSolutions->WebProjects and toggle the "Use the 64 bit version of IIS Express..." checkbox.
...and if you still aren't sure what architecture you're using then just open up Windows Task Manager and look at your app's iisexpress.exe (or w3wp.exe) process. On Win7 and earlier your process will have a *32 suffix in the detail tab if you're running as a 32-bit process.... and on newer operating systems like Windows 8/8.1/10, the first "Processes" tab should tell you the architecture in parentheses after each process. If you're actually running 64-bit after fixing VS and your config then there's probably a bug in the libraries you're using.
It seems that the IIS Express is 32-bit. But I don't find such a checkbox in vs2012? Does vs2012 not support 64-bit iis express?
Thank you very much. I upgrade the vs2012 to vs2013 and it works.
0

Have you disabled "Prefer 32 bit"?

See http://www.neovolve.com/2015/07/31/disable-prefer-32-bit/

1 Comment

I turely disabled "Prefer 32 bit"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.