c# game

Started by
48 comments, last by phil67rpg 5 years, 2 months ago

************** Exception Text **************
System.ArgumentException: Parameter is not valid.
   at System.Drawing.Bitmap..ctor(String filename, Boolean useIcm)
   at WindowsFormsApp1.Form1.Form1_Paint(Object sender, PaintEventArgs e) in C:\Users\Owner\documents\visual studio 2017\Projects\WindowsFormsApp1\WindowsFormsApp1\Form1.cs:line 32
   at System.Windows.Forms.Control.OnPaint(PaintEventArgs e)
   at System.Windows.Forms.Form.OnPaint(PaintEventArgs e)
   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.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.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)
 

Advertisement

The problem is your BMP cannot be found. Put it in the Release or Debug folder (depending on which you're running) and it will work.

I just tried it and it works fine.

TestGraphics.zip

Developer since 1994. Programming since '80. Still like it.

how do I get the app to find the bmp, yes it does  exist on the desktop.

1 minute ago, phil67rpg said:

how do I get the app to find the bmp, yes it does  exist on the desktop.

see the project I attached in the previous message.

It can't magically find the image on your desktop :) How does it know it's there? Either provide the full path to it, or move it as I suggested.

Developer since 1994. Programming since '80. Still like it.

I moved as suggested and yes it works, thanks for all the help, I was looking in the wrong folder.

Just now, phil67rpg said:

I moved as suggested and yes it works, thanks for all the help, I was looking in the wrong folder.

No problem :) Keep at it. I did quite a bit of graphics on WinForms not so long ago (although it's outdated, it's still very useful for learning and rapid development). I'm prototyping a game in it right now.

Developer since 1994. Programming since '80. Still like it.

I am still working on my blackjack game but I am using graphics. thanks for all the  help.

9 minutes ago, phil67rpg said:

I am still working on my blackjack game but I am using graphics. thanks for all the  help.

No problem. BTW you should dispose() the BMP you created. Rule of thumb is to dispose resources you create (at the right time) and not those you don't create.

Developer since 1994. Programming since '80. Still like it.

here is my updated code, is there anyway I can streamline my code so far.


        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Bitmap bitmap_two = new Bitmap("c02.bmp", true);
            Bitmap bitmap_ace = new Bitmap("c01.bmp", true);
            g.DrawImage(bitmap_two, 20, 20);
            g.DrawImage(bitmap_ace, 40, 40);
            g.Dispose();
        }

 

20 minutes ago, phil67rpg said:

here is my updated code, is there anyway I can streamline my code so far.



        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Bitmap bitmap_two = new Bitmap("c02.bmp", true);
            Bitmap bitmap_ace = new Bitmap("c01.bmp", true);
            g.DrawImage(bitmap_two, 20, 20);
            g.DrawImage(bitmap_ace, 40, 40);
            g.Dispose();
        }

 

Yes but beware premature optimization :)

Don't dispose the graphics object. You didn't create it, you're only aliasing it by assigning to another variable.

About the only thing I'd add at this point, is to surround the bitmap creation/usage in a using() statement (but not the Graphics g = part).

edit: ok another general thing, don't use hard-coded strings like "c02.bmp". I know it's only early but I think this is worth mentioning early so that you apply that to all your code as you write it. Either create a constant for it, or better, store and look it up somehow. I'll leave that to you.

Developer since 1994. Programming since '80. Still like it.

This topic is closed to new replies.

Advertisement