-2

I have A program where i want to print an image to the form( in this case a coin) But The Program wont let me do that. it stops at the last line the crash is ArgumentException

heres The Code:

Random _Random = new Random();
            Image _CoinImage;
            int height;
            int width;
            Graphics g;
            public Coin(int Height,int Width,Graphics gr)
            {
                _CoinImage = Properties.Resources.Coin;
                height = Height;
                width = Width;
                g = gr;//gr = the form paint
                CoinRec = new Rectangle(0, 0, width, Height);
                g.DrawImage(_CoinImage, CoinRec); //Crashes Here
            }
TheNormalPerson
  • 551
  • 5
  • 15

1 Answers1

2

The only overload of DrawImage that takes an Image and a Rectangle raises ArgumentNullException if the image is null:

ArgumentNullException image is null.

Either check that the image has been loaded correctly and don't call DrawImage or trap the exception. Which you do depends on how likely it is that the image is null.

As Marcelo points out in his comment you aren't loading the image correctly which is why it's null.

As the accepted answer to that question shows you need to load it like this:

var bmp = new Bitmap(WindowsFormsApplication1.Properties.Resources.myimage);

Community
  • 1
  • 1
ChrisF
  • 134,786
  • 31
  • 255
  • 325