简体   繁体   English

图片库中未加载位图

[英]Bitmap not loading in picturebox

I am unable to load the bitmap image in the PictureBox. 我无法在PictureBox中加载位图图像。 It gives me an error saying the parameter is not valid. 它给我一个错误,指出参数无效。

        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
                } 
            }
        }

The size of my pictureBox is also 1000X1000. 我的pictureBox的大小也是1000X1000。 Can anyone tell me where am I going wrong? 谁能告诉我我要去哪里错了?

EDIT 1: Error Description: 编辑1:错误说明:

System.ArgumentException: Parameter is not valid. System.ArgumentException:参数无效。 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) 在系统System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode模式)在System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)在System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode模式)在System.Drawing.Image.get_Size()在System.Windows.Forms.Control.WmPaint(Message&m)(位于System.Windows.Forms的System.Windows.Forms.Control.WmProint(Message&m))处的.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e,Int16层) System.Windows.Forms上的Control.ControlNativeWindow.OnMessage(Message&m)System.Windows.Forms.NativeWindow.Callback上的Control.ControlNativeWindow.WndProc(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

Remove using statements on bmp . 删除bmp上的using语句。 Because, your bitmap is disposed after pictureBox1.Image = bmp; 因为,您的位图被放置在pictureBox1.Image = bmp;之后pictureBox1.Image = bmp; and you get an error on paint event. 并且您在绘画事件上收到错误消息。

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
    } 
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM