views:

70

answers:

2
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        NumberOfBets++;

        if ((e.X >= 40 && e.X <= 125) && (e.Y >= 0 && e.Y <= 26))
        {
            bettingStatus[0]++;
            for (int x; x < 10; x++)
            {
                Graphics graphic = (???)
            }
        }

I'm trying to draw an image in this MouseDown method. I have NO clue what goes in the "(???)" part.

A: 

You need to get the Graphics from somewhere, probably pictureBox1 in your case:

Graphics graphic = pictureBox1.CreateGraphics();

...But are you sure you want to be drawing in a MouseDown event handler? It won't be repainted if any part gets redrawn. You'd probably be better off doing all your drawing in a Paint event handler and setting a flag in MouseDown instead. Then invalidate the region you want to be redrawn to draw the new image.

Or, if your images are going to be more static, you can create a Bitmap, draw on that, then set the Image of your PictureBox to point to the Bitmap instead. For example:

Bitmap bmp = new Bitmap(200, 100);
Graphics graphics = Graphics.FromImage(bmp);
//do drawing here
pictureBox1.Image = bmp;
lc
+1  A: 
// Create a Graphics object for the pictureBox1 control.
Graphics g = pictureBox1.CreateGraphics();

Read more: MSDN: Graphics Class

You should draw in the Paint event, because if you don't, you will loose your drawings if the control is repainted. The PaintEventArgs passed to the Paint event handler has a Property named Graphics (of type System.Drawing.Graphics) which you can draw on.

M. Jahedbozorgan