我正在开发一个使用移动设备进行拍照,并使用一个Web服务发送一个应用程序。不过,我已经采取了4张照片后,我正在一个 OutOfMemoryException异常
在下面的code。我打过电话 GC.Collect的()
,但它并没有帮助的。也许有人在这里可以给我一个建议如何处理这个问题。
公共静态位图TakePicture()
{
VAR对话框=新CameraCaptureDialog
{
分辨率=新的大小(1600,1200),
StillQuality = CameraCaptureStillQuality.Default
};
dialog.ShowDialog();
//如果文件名是空的,用户没有采取任何图片
如果(string.IsNullOrEmpty(dialog.FileName))
返回null;
//(!)的OutOfMemoryException异常被抛出在这里(!)
VAR位图=新位图(dialog.FileName);
File.Delete(dialog.FileName);
返回的位图;
}
该函数的调用由一个事件处理程序:
私人无效_pictureBox_Click(对象发件人,EventArgs的)
{
_takePictureLinkLabel.Visible = FALSE;
VAR图像= Camera.TakePicture();
如果(像== NULL)
返回;
图像= Camera.CutBitmap(图像,2.5);
_pictureBox.Image =图像;
_image = Camera.ImageToByteArray(图像);
}
解决方案
我怀疑你持有到引用。作为一个次要的原因,请注意对话时,不要使用不处理自己的的ShowDialog
,所以你应该使用
对话(虽然我希望GC仍然在收集,但未予非参考对话框)。
同样,你应该是使用
的形象,却又:不知道我会预计这将制造或中断;值得一试,虽然...
公共静态位图TakePicture()
{
字符串的文件名;
使用(VAR对话框=新CameraCaptureDialog
{
分辨率=新的大小(1600,1200),
StillQuality = CameraCaptureStillQuality.Default
}){
dialog.ShowDialog();
文件名= dialog.FileName;
}
//如果文件名是空的,用户没有采取任何图片
如果(string.IsNullOrEmpty(文件名))
返回null;
//(!)的OutOfMemoryException异常被抛出在这里(!)
VAR位图=新位图(文件名);
File.Delete(文件名);
返回的位图;
}
私人无效_pictureBox_Click(对象发件人,EventArgs的)
{
_takePictureLinkLabel.Visible = FALSE;
使用(VAR图像= Camera.TakePicture()){
如果(像== NULL)
返回;
图像= Camera.CutBitmap(图像,2.5);
_pictureBox.Image =图像;
_image = Camera.ImageToByteArray(图像);
}
}
我也想成为的 CutBitmap
等有些谨慎,以确保事情是尽快释放。
I'm developing an application that uses a mobile device to take a photo and send it using a webservice. But after I've taken 4 photos I am getting an OutOfMemoryException
in the code below. I tried calling GC.Collect()
but it didn't help either. Maybe someone here could be give me an advice how to handle this problem.
public static Bitmap TakePicture()
{
var dialog = new CameraCaptureDialog
{
Resolution = new Size(1600, 1200),
StillQuality = CameraCaptureStillQuality.Default
};
dialog.ShowDialog();
// If the filename is empty the user took no picture
if (string.IsNullOrEmpty(dialog.FileName))
return null;
// (!) The OutOfMemoryException is thrown here (!)
var bitmap = new Bitmap(dialog.FileName);
File.Delete(dialog.FileName);
return bitmap;
}
The function is called by an event handler:
private void _pictureBox_Click(object sender, EventArgs e)
{
_takePictureLinkLabel.Visible = false;
var image = Camera.TakePicture();
if (image == null)
return;
image = Camera.CutBitmap(image, 2.5);
_pictureBox.Image = image;
_image = Camera.ImageToByteArray(image);
}
解决方案
I suspect you are holding onto references. As a minor cause, note that dialogs don't dispose themselves when using ShowDialog
, so you should be using
the dialog (although I would expect GC to still collect an undisposed but non-referenced dialog).
Likewise, you should probably be using
the image, but again: not sure I'd expect this to make-or-break; worth a try, though...
public static Bitmap TakePicture()
{
string filename;
using(var dialog = new CameraCaptureDialog
{
Resolution = new Size(1600, 1200),
StillQuality = CameraCaptureStillQuality.Default
}) {
dialog.ShowDialog();
filename = dialog.FileName;
}
// If the filename is empty the user took no picture
if (string.IsNullOrEmpty(filename))
return null;
// (!) The OutOfMemoryException is thrown here (!)
var bitmap = new Bitmap(filename);
File.Delete(filename);
return bitmap;
}
private void _pictureBox_Click(object sender, EventArgs e)
{
_takePictureLinkLabel.Visible = false;
using(var image = Camera.TakePicture()) {
if (image == null)
return;
image = Camera.CutBitmap(image, 2.5);
_pictureBox.Image = image;
_image = Camera.ImageToByteArray(image);
}
}
I'd also be a little cautious of the CutBitmap
etc, to ensure that things are released ASAP.