我正在尝试裁剪来自字节数组的图像。不幸的是,我在cropImage 函数中得到了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