简体   繁体   中英

Bitmap not loading in picturebox

I am unable to load the bitmap image in the 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. Can anyone tell me where am I going wrong?

EDIT 1: Error Description:

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)

Remove using statements on bmp . Because, your bitmap is disposed after 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
    } 
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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