Getting rid of lag with pictureBox
-
Hello, I am currently trying to develop a video game using .NET C++. The one thing I am having trouble with is when I move (with a Key_Down event) a pictureBox with an image set, there is always a lag, and thus a piece of the image's last position always appears. How would I get rid of this?? I had tried making it invisible, changing the pictureBox's location, then making it visible again, but that makes the image blink as it moves. Any help would be greatly appreciated. Thank you.:-D Michael
-
Hello, I am currently trying to develop a video game using .NET C++. The one thing I am having trouble with is when I move (with a Key_Down event) a pictureBox with an image set, there is always a lag, and thus a piece of the image's last position always appears. How would I get rid of this?? I had tried making it invisible, changing the pictureBox's location, then making it visible again, but that makes the image blink as it moves. Any help would be greatly appreciated. Thank you.:-D Michael
Hi, in a video game you shouldn't use a picture box. Remove it and paint the bitmap yourself onto the form/control/whatever by overriding the Paint method (don't forget to call the base implementation) and calling e.Graphics.DrawImage. This should be by far faster than the picturebox.
-
Hi, in a video game you shouldn't use a picture box. Remove it and paint the bitmap yourself onto the form/control/whatever by overriding the Paint method (don't forget to call the base implementation) and calling e.Graphics.DrawImage. This should be by far faster than the picturebox.
Hey thanks, I tried that and it worked a lot better, however the image still jumps and is blurry as I move it. Would there be a way to get rid of that? I know that in java they use a buffering strategy, however I do not know how to do that in .NET C++. Thanks for the help so far. Michael
-
Hey thanks, I tried that and it worked a lot better, however the image still jumps and is blurry as I move it. Would there be a way to get rid of that? I know that in java they use a buffering strategy, however I do not know how to do that in .NET C++. Thanks for the help so far. Michael
(I'll use C# syntax as Im not very familiar with C++) A simple double buffering can be enabled by adding this in the constructor of your form/control:
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);Does your image have the correct size or do you resize it while painting? If it has already the correct size use the DrawImageUnscaled function. If not resize it somewhere in the beginning:
Bitmap resizedBitmap = new Bitmap(wantedWidth, wantedHeight);
Graphics g = Graphics.FromImage(resizedBitmap);
g.DrawImageUnscaled(oldBitmap, 0, 0, wantedWidth, wantedHeight);
g.Dispose();
//resizedBimtap is ready to useEDIT: You should also search for 'double buffering' here on CodeProject. There are some articles which might be interesting for you.
-
(I'll use C# syntax as Im not very familiar with C++) A simple double buffering can be enabled by adding this in the constructor of your form/control:
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);Does your image have the correct size or do you resize it while painting? If it has already the correct size use the DrawImageUnscaled function. If not resize it somewhere in the beginning:
Bitmap resizedBitmap = new Bitmap(wantedWidth, wantedHeight);
Graphics g = Graphics.FromImage(resizedBitmap);
g.DrawImageUnscaled(oldBitmap, 0, 0, wantedWidth, wantedHeight);
g.Dispose();
//resizedBimtap is ready to useEDIT: You should also search for 'double buffering' here on CodeProject. There are some articles which might be interesting for you.
the C# syntax isn't that different (I just used :: instead of .) however the buffer didn't appear to do anything. I guess it isn't possible to get rid of the flicker when painting an image, however I still do not know how it is done in java, but can't be done in .NET C++. Also, did you mean to make a paint event for the form, and then e->Graphics->DrawImage it, because that is what I did. Anyways, thanks again for the help. Michael
-
the C# syntax isn't that different (I just used :: instead of .) however the buffer didn't appear to do anything. I guess it isn't possible to get rid of the flicker when painting an image, however I still do not know how it is done in java, but can't be done in .NET C++. Also, did you mean to make a paint event for the form, and then e->Graphics->DrawImage it, because that is what I did. Anyways, thanks again for the help. Michael
No I meant overriding the OnPaint method of the form (but that shouldn't make a difference). I made a simple text which work fine for me (when the image is very big it slows down). Probably it helps you:
public class Form1 : System.Windows.Forms.Form
{
private Point _curPos = new Point(0, 0);
private Image _image;public Form1() { base.KeyPress += new KeyPressEventHandler(Form1\_KeyPress); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.DoubleBuffer, true); \_image = new Bitmap("C:\\\\test2.jpg"); } private void Form1\_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { switch (e.KeyChar) { case 'w': \_curPos.Y -= 5; break; case 's': \_curPos.Y += 5; break; case 'a': \_curPos.X -= 5; break; case 'd': \_curPos.X += 5; break; } Invalidate(); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint (e); e.Graphics.DrawImageUnscaled(\_image, \_curPos); }
}
Note that I'm calling the Invalidate method instead of Refresh. This is always better because the Refresh will repaint the form immediatly while Invalidate will wait until there is time to do so. If you have much more other things on your form it would also be a good idea to give a Rectangle into the form which specifies which particular region should be repainted.