我正在嘗試裁剪來自字節數組的圖像。不幸的是,我在我的cropImage函數中得到了OutofMemory異常。這部分展示瞭如何將它寫在文件上。OutofMemory異常(裁剪圖像時)
System.IO.MemoryStream ms = new System.IO.MemoryStream(strArr);
System.Drawing.Rectangle oRectangle = new System.Drawing.Rectangle();
oRectangle.X = 50;
oRectangle.Y = 100;
oRectangle.Height = 180;
oRectangle.Width = 240;
System.Drawing.Image oImage = System.Drawing.Image.FromStream(ms);
cropImage(oImage, oRectangle);
name = DateTime.Now.Ticks.ToString() + ".jpg";
System.IO.File.WriteAllBytes(context.Server.MapPath(name), strArr);
context.Response.Write("http://local.x.com/test/" + name);
,這部分是我的裁剪圖像功能,這是很明顯它是做什麼..
private static System.Drawing.Image cropImage(System.Drawing.Image img, System.Drawing.Rectangle cropArea)
{
System.Drawing.Bitmap bmpImage = new System.Drawing.Bitmap(img);
System.Drawing.Bitmap bmpCrop = bmpImage.Clone(cropArea,
bmpImage.PixelFormat);
return (System.Drawing.Image)(bmpCrop);
}
,這是怎麼構建我的strArr
System.IO.Stream str = context.Request.InputStream;
int strLen = Convert.ToInt32(str.Length);
byte[] strArr = new byte[strLen];
str.Read(strArr, 0, strLen);
string st = String.Concat(Array.ConvertAll(strArr, x => x.ToString("X2"))); // try 4
知道cropArea值是什麼會很有趣。我在想它可能有一些非常大的上限,導致系統嘗試爲bmpImage.Clone(...)的返回值分配一個巨大的圖像。我建議你調試你的應用程序並檢查這個cropArea變量。 – 2012-07-13 09:20:47
請閱讀我的答案。我們在生產中遇到了這個問題。除了使用其他圖像API之外,沒有任何解決方案 – 2012-07-13 09:22:35
@SimonEjsing,它實際上是我在代碼頂部創建的矩形(oRectangle) – 2012-07-13 09:23:15