位图未加载到图片框中

Bitmap not loading in picturebox

我无法在 PictureBox 中加载位图图像。它给我一个错误,说参数无效。

        Image up = Image.FromFile("somePath");
        Image down = Image.FromFile("anotherPath");

        using (down)
        {
            using(var bmp = new Bitmap(1000, 1000))
            {
                using(var canvas = Graphics.FromImage(bmp))
                {
                    canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    canvas.DrawImage(up, 0, 0);
                    canvas.DrawImage(down, 0, 500);
                    canvas.Save();
                    pictureBox1.Image = bmp;// this line gives the error
                } 
            }
        }

我的pictureBox大小也是1000X1000。谁能告诉我哪里错了?

编辑 1: 错误说明:

System.ArgumentException: Parameter is not valid. at System.Drawing.Image.get_Width() at System.Drawing.Image.get_Size() at System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode) at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe) at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer) at System.Windows.Forms.Control.WmPaint(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

删除 bmp 上的 using 语句。因为,您的位图是在 pictureBox1.Image = bmp; 之后处理的,并且您在绘制事件时遇到错误。

Image up = Image.FromFile("somePath");
Image down = Image.FromFile("anotherPath");

using (down)
{
    var bmp = new Bitmap(1000, 1000);
    using(var canvas = Graphics.FromImage(bmp))
    {
        canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
        canvas.DrawImage(up, 0, 0);
        canvas.DrawImage(down, 0, 500);
        canvas.Save();
        pictureBox1.Image = bmp;// this line gives the error
    } 
 }