我想调整网站中图像的大小,但是当我使用Bitmap加载14032 * 19864(png扩展名)的图像时,OutOfMemoryException
会抛出。我的编译器配置为any cpu
。我怀疑运行环境是否合适x64
。代码如下:
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");
}
}
例外发生在
System.Drawing.Bitmap image_source = new System.Drawing.Bitmap(path);
确保在配置文件中将gcAllowVeryLargeObjects
元素设置为true。
.NET中的单个分配最大为2 GB(即使在作为64位进程运行时也是如此),并且很有可能您正在使用的一个类在内部做一些事情达到了这一限制。这是一个非常普遍的问题,修复您的配置文件应该可以解决这个问题。
更新:根据以下评论,@ majing遇到的问题是Visual Studio在32位版本的IIS Express中启动了他的Web应用程序。将VS配置为以64位进程启动IIS可解决此问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句