views:

38

answers:

4

For some reason, this code does not actually draw my bitmap file... or show the form.

namespace GraphicsEngine
{
public partial class Form1 : Form
{

    Bitmap[] dude = new Bitmap[3];
    Bitmap dude0 = new Bitmap(@"C:\Directory.bmp");
    Point renderpoint = new Point(1, 1);

    public Form1()
    {

        dude[0] = new Bitmap(@"C:\Directory.bmp");
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        MainLoop();
    }

    private void MainLoop()
    {
        double FPS = 30.0;
        long ticks1 = 0;
        long ticks2 = 0;
        double interval = (double)Stopwatch.Frequency / FPS;


        while (!this.IsDisposed)
        {

            ticks2 = Stopwatch.GetTimestamp();
            if (ticks2 >= ticks1 + interval)
            {
                ticks1 = Stopwatch.GetTimestamp();

                this.Invalidate(); 
            }

            Thread.Sleep(1);

        }
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {

        Graphics g = e.Graphics;

        g.DrawImage(dude0, renderpoint);
    }



}
}

Any ideas?

A: 

Form1_Paint is not called unless if you have UserPaint set to true. Try this as your constructor

public Form1() {
  dude[0] = new Bitmap(@"C:\Directory.bmp");
  InitializeComponent();

  this.SetStyle( System.Windows.Forms.ControlStyles.UserPaint, true );
  this.SetStyle( System.Windows.Forms.ControlStyles.AllPaintingInWmPaint, true );
  this.SetStyle( System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer, true );
}

Just beware, when you do this, you may be responsible for all the paints on the form.

JDMX
Is there another method that I can use then?EDIT: it also does not recognize ControlStyles.
Bloodyaugust
Instead of doing the UserPaint, have you tried the property Form.BackgroundImage and Form.BackgroundImageLayout
JDMX
Those would be fine, except i will be rendering multiple (unknown) amounts of different .bmp files, so that doesn't work for me.
Bloodyaugust
Why not... just change the BackgroundImage property each time you need a new image. That will force a new paint and you will not have to do the UserPaint option. If you need to paint out multiple images at once, then you are stuck with UserPaint.
JDMX
Exactly. Sorry, forgot to specify at once. Why does it not recognize ControlStyles?
Bloodyaugust
look at the edits above for the constructor. I had no issue with this compiling within my environment.
JDMX
The edits work, don't know why the previous on didn't...
Bloodyaugust
A: 

Try replacing your call to this.Invalidate(); with this.Refresh();.

Zach Johnson
The form still does not even show.
Bloodyaugust
+1  A: 

Your problem ought to be a bit more obvious than not seeing the bitmap, you should not see the form either. That's because you never complete the Load event. You could use the Shown event instead.

Check this thread for the code for a true game loop.

Hans Passant
A: 

Looks like your MainLoop() could be an infinite loop. You can put Console.Out.WriteLine(ticks1); in your while loop to validate this.

It gets stuck in the Load event handler and none of the main form validation occurs. It definitely draws the picture if you comment out the call to MainLoop().

Ken
Ah. Thanks for the save, but now how else will I call my MainLoop()? And it is infinite, but there should be no problem there... And it does not draw the bitmap after commenting out the MainLoop() call.
Bloodyaugust
Shown event handler probably, as @nobugz suggested.
Ken