未在图片框

时间:2018-06-13 16:14:46

标签: c# winforms bitmap 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:参数无效。在   System.Drawing.Image.get_Size()中的System.Drawing.Image.get_Width()   在   System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode   模式)在System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)   在System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs   System.Windows.Forms.Control.WmPaint(消息& m)中的e,Int16层)   在System.Windows.Forms.Control.WndProc(Message& m)at   System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)   在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&   m)在System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32)   msg,IntPtr wparam,IntPtr lparam)

1 个答案:

答案 0 :(得分:3)

删除bmp上的使用语句。因为,您的位图在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
    } 
 }