0

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)

1
  • 1
    Add error description Commented Jun 13, 2018 at 16:15

1 Answer 1

3

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
    } 
 }
Sign up to request clarification or add additional context in comments.

1 Comment

@JustCurious glad to help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.