Page 1 of 1

drawimage help please

Posted: Wed Feb 12, 2014 8:28 pm
by Dummy1912
it seems that this won't work :(

what are we doing wrong even i have no clue it seems working fine in vb.net this way but not in c#
Code: Select all
if (_clicked == false)
      {
          g.DrawImage(pictureBox2.Image, new Rectangle(0, 0, 39, 41));
      }
      else
      {
          g.DrawImage(pictureBox1.Image, new Rectangle(0, 0, 39, 41));
      }
Error is
System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
thank you

Re: drawimage help please

Posted: Wed Feb 12, 2014 8:48 pm
by XTechVB
Make sure that g is initialized like so
Code: Select all
Graphics g = e.Graphics;
//-- Or like this
Graphics g = this.CreateGraphics();
And pictureBox1 and pictureBox2 have an image set. And also try not to draw images in DesignMode (it tends to slow down the Designer).

Re: drawimage help please

Posted: Wed Feb 12, 2014 8:49 pm
by smashapps
make sure you have this at the very top of your class code, then everything else below it.
Code: Select all
public Form1()
        {
            InitializeComponent();
        }

Re: drawimage help please

Posted: Wed Feb 12, 2014 10:22 pm
by Dummy1912
thanks you #smashapps this did the job :)