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);
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.