我想在我的网站中调整图像的大小,但是当我使用 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);