1

我正在使用 Compact Framework (Windows Mobile 6.1)为PDA开发应用程序。我需要制作照片并显示图像。因此,要制作照片,我正在使用该CameraCaptureDialog课程:

using (var dialog = new CameraCaptureDialog())
{
    if (dialog.ShowDialog() == DialogResult.OK)
    {
    }
}

GC.Collect();
GC.WaitForPendingFinalizers();

并使用 a 显示图像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如果我不拍照片,也不例外。

为什么?我该如何解决这个问题?

4

0 回答 0