Tom*_*Tom 1 c# memory imaging bitmap overflow
我必须从许多图像中导入大量的图像裁剪,这些图像都已准备好存储在我的数据库中.我每次尝试使用语句并处理我的位图对象.但我仍然得到一个内存溢出异常,我的系统内存不足.
以下是我正在做的一些示例代码.
public void CropImage(List<ImageClass> data)
{
foreach (var obj in data)
{
//I have a data base method that returns a data object that
//contains the file bytes of the image id in data: 'file'
//My List<ImageClass> data contains an ID of the original image
//start x,y coords for the upper left corner of the rectangle,
//and the width and height of the rectangle.
Image img = Image.FromStream(new MemoryStream(file.Data));
Bitmap bmp = new Bitmap((Bitmap)img);
Rectangle cropArea = new Rectangle(obj.x_coordinate,
obj.y_coordinate,
obj.width,
obj.height);
Bitmap cropImage = bmp.Clone(cropArea, bmp.PixelFormat);
SaveFile(cropImage, file, obj.scanID);
img.Dispose();
bmp.Dispose();
cropImage.Dispose();
}
}
public void SaveFile(Bitmap cropImage, FileData file, int OCRscanID)
{
EncoderParameters encoderParams = new EncoderParameters();
encoderParams.Param[0] = new EncoderParameter(
System.Drawing.Imaging.Encoder.Quality,
50L);
ImageCodecInfo codecInfo = GetEncoderInfo("image/jpeg");
MemoryStream newImage = new MemoryStream();
cropImage.Save(newImage, codecInfo, encoderParams);
byte[] newData = newImage.ToArray();
//Saving data bytes to database of the cropped image
}
private ImageCodecInfo GetEncoderInfo(string mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以修剪一些代码,比如搜索编码器来使用image/jpeg.但我有另一个应用程序,这个代码另一个项目.我似乎无法通过内存溢出.
我需要循环大约20k图像.
| 归档时间: |
|
| 查看次数: |
1625 次 |
| 最近记录: |