System.ArgumentException:创建新位图c#时参数无效

时间:2017-11-22 00:45:59

标签: c# bitmap argument-error

多次调用此函数时,任何一个try块都会继续抛出错误。这在随机时间发生,特别是在重复呼叫时。

//CameraCanvas: Class bitmap variable
//Canvas: Class bitmap imported through open file dialog

public void UpdateCamera()
    {
        try
        {
            CameraCanvas = new Bitmap(CameraWidthHeight.X, CameraWidthHeight.Y, PixelFormat.Format32bppArgb);
        }
        catch(Exception ex)
        {
            throw ex;
        }
        Graphics g = Graphics.FromImage(CameraCanvas);
        g.InterpolationMode = InterpolationMode.NearestNeighbor;

        //Draw it
        Bitmap drawImage = Canvas;
        g.DrawImage(drawImage, new PointF(0f,0f));
        try
        {
            CameraCanvas = new Bitmap(CameraWidthHeight.X, CameraWidthHeight.Y, g);
        }
        catch(Exception ex)
        {
            throw ex;
        }
        g.Dispose();
    }

我尝试了很多东西,但没有一个能奏效。这些代码都不涉及流或类似的东西。下面是通过窗体构造函数中的打开文件对话框将Canvas导入为位图的代码。

        Bitmap source;
        try
        {
            OpenFileDialog of = new OpenFileDialog();
            if(of.ShowDialog() == DialogResult.OK)
            {
                source = new Bitmap(of.FileName);
                Canvas = source;
                this.TopLevel = true;
            }
        }
        catch(Exception ex)
        {
            throw ex;
        }

0 个答案:

没有答案