我正在使用Compact Framework(Windows Mobile 6.1)为PDA开发应用程序。我需要拍照并显示图像。所以,要制作一张我正在使用CameraCaptureDialog
类的照片:
using (var dialog = new CameraCaptureDialog())
{
if (dialog.ShowDialog() == DialogResult.OK)
{
}
}
GC.Collect();
GC.WaitForPendingFinalizers();
要显示图像,请使用Form
:
public partial class FormImageViewer : Form
{
public FormImageViewer(string filename)
{
if (!File.Exists(filename))
throw new FileNotFoundException();
InitializeComponent();
// Here I have an OutOfMemoryException!
image = new Bitmap(fileName);
}
private Bitmap image;
private void menuItemOk_Click(object sender, EventArgs e)
{
if(image != null)
image.Dispose();
GC.Collect();
Close();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
double ratio = Math.Max((double)image.Width / Width, (double)image.Height / Height);
int x = (Width - (int)(image.Width / ratio)) / 2;
int y = (Height - (int)(image.Height / ratio)) / 2;
Rectangle destRect = new Rectangle(x, y, (int)(image.Width / ratio), (int)(image.Height / ratio));
Rectangle srcRect = new Rectangle(0, 0, image.Width, image.Height);
e.Graphics.DrawImage(image, destRect, srcRect, GraphicsUnit.Pixel);
}
}
问题是OutOfMemoryException
只有在我第一次使用CameraCaptureDialog
类时才会抛出。如果我不拍照,则没有任何例外。
为什么呢?我该如何解决这个问题?