OutOfMemory例外(裁剪图像时) [英] OutofMemory Exception (when cropping an Image)
本文介绍了OutOfMemory例外(裁剪图像时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图裁剪,从一个字节数组来的图像。倒霉,我得到了我的cropImage功能OutOfMemory例外。这部分节目我怎么写上的文件。
System.IO.MemoryStream毫秒=新System.IO.MemoryStream(strArr);System.Drawing.Rectangle oRectangle =新System.Drawing.Rectangle();
oRectangle.X = 50;
oRectangle.Y = 100;
oRectangle.Height = 180;
oRectangle.Width = 240;为System.Drawing.Image oImage = System.Drawing.Image.FromStream(毫秒);
cropImage(oImage,oRectangle);
名称= DateTime.Now.Ticks.ToString()+.JPG;
System.IO.File.WriteAllBytes(context.Server.MapPath(名称),strArr);
context.Response.Write(http://local.x.com/test/+名称);
和这部分是我的裁剪图像功能,很明显它是做什么..
私有静态System.Drawing.Image对象cropImage(System.Drawing.Image对象IMG,System.Drawing.Rectangle cropArea)
{
System.Drawing.Bitmap bmpImage =新System.Drawing.Bitmap(IMG);
System.Drawing.Bitmap bmpCrop = bmpImage.Clone(cropArea,
bmpImage.PixelFormat);
回报(System.Drawing.Image对象)(bmpCrop);
}
这就是我如何构造我的strArr
的System.IO.Stream海峡= context.Request.InputStream;
INT STRLEN = Convert.ToInt32(str.Length);
字节[] = strArr新的字节[STRLEN]
str.Read(strArr,0,strlen的);
串ST = String.Concat(Array.ConvertAll(strArr中,x => x.ToString(X2))); //尝试4
解决方案
我直接从字节数组裁剪它,它只是工作:)谢谢大家谁想尽办法帮助我。
字节公众[] CropImage(INT X,INT Y,INT W,INT小时,字节[] imageBytes)
{
使用(MemoryStream的毫秒=新的MemoryStream(imageBytes,0,imageBytes.Length))
{
ms.Write(imageBytes,0,imageBytes.Length);
为System.Drawing.Image IMG = System.Drawing.Image.FromStream(MS,真);
位图bmpCropped =新位图(W,H);
图形G = Graphics.FromImage(bmpCropped); 矩形rectDestination =新的Rectangle(0,0,bmpCropped.Width,bmpCropped.Height);
矩形rectCropArea =新的Rectangle(X,Y,W,H); g.DrawImage(IMG,rectDestination,rectCropArea,GraphicsUnit.Pixel);
g.Dispose(); MemoryStream的流=新的MemoryStream();
bmpCropped.Save(流System.Drawing.Imaging.ImageFormat.Jpeg);
返回stream.ToArray();
}
}
I am trying to crop an Image that coming from a byte array. Unlucky, I get the OutofMemory Exception in my cropImage function. this part show how I write it on a file.
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);
and this part is my crop Image function which is obvious what it is doing..
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);
}
and this is how I construct my 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
解决方案
I cropped it from byte array directly and it just works :) thanks everyone who tried their best to help me.
public byte[] CropImage(int x, int y, int w, int h, byte[] imageBytes)
{
using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
ms.Write(imageBytes, 0, imageBytes.Length);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms, true);
Bitmap bmpCropped = new Bitmap(w, h);
Graphics g = Graphics.FromImage(bmpCropped);
Rectangle rectDestination = new Rectangle(0, 0, bmpCropped.Width, bmpCropped.Height);
Rectangle rectCropArea = new Rectangle(x, y, w, h);
g.DrawImage(img, rectDestination, rectCropArea, GraphicsUnit.Pixel);
g.Dispose();
MemoryStream stream = new MemoryStream();
bmpCropped.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
return stream.ToArray();
}
}
这篇关于OutOfMemory例外(裁剪图像时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文