在WinMobile和.NET CF中调整和保存图像会抛出OutOfMemoryException

时间:2009-07-10 16:39:11

标签: image windows-mobile compact-framework

我有一个WinMobile应用程序,允许用户使用相机拍摄照片,然后用于各种事物。照片可以在1600x1200,800x600或640x480下拍照,但必须始终将照片调整为400px以获得最长尺寸(另一种是当然比例)。这是代码:

private void LoadImage(string path)
{
    Image tmpPhoto = new Bitmap(path);

    // calculate new bitmap size...
    double width = ...
    double height = ...

   // draw new bitmap
   Image photo = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
   using (Graphics g = Graphics.FromImage(photo))
   {
       g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, photo.Width, photo.Height));
       int srcX = (int)((double)(tmpPhoto.Width - width) / 2d);
       int srcY = (int)((double)(tmpPhoto.Height - height) / 2d);
       g.DrawImage(tmpPhoto, new Rectangle(0, 0, photo.Width, photo.Height), new Rectangle(srcX, srcY, photo.Width, photo.Height), GraphicsUnit.Pixel);
   }
   tmpPhoto.Dispose();
   // save new image and dispose
   photo.Save(Path.Combine(config.TempPath, config.TempPhotoFileName), System.Drawing.Imaging.ImageFormat.Jpeg);
   photo.Dispose();
}

现在的问题是应用程序在photo.Save调用中断了,出现OutOfMemoryException。而且我不知道为什么,因为我尽快处理了tempPhoto(来自相机的原始照片),并且我也处理了Graphics obj。为什么会这样?对我来说似乎不可能无法用相机拍照并调整大小/保存它而不会让它崩溃:(我应该为这么简单的东西重新安装C ++吗? 感谢。

1 个答案:

答案 0 :(得分:0)

您是否了解了每一步的内存使用情况,以确切了解您最常使用的位置?您省略了宽度和高度的计算,但假设它们是正确的,您最终会得到400x300x3(24位)== 360k的位图数据本身的照片,这不是非常大。

我的猜测是,即使您正在调用Dispose,如果您多次调用此方法,资源也不会被激活,尤其是。 CF使用Bitmaps以意想不到的方式运行。 I call it a bugThe CF team doesn't

相关问题