Pregunta

I'm making a very simple 2D game. Unfortunately I ran into an issue with refresh rate when I add images. I use Invalidate on a timer tick which works fine until I change DrawRectangle to DrawImage. With just 27 objects on the screen Paint refresh rate drops from stable 64 to 39 on average. Images that I use are only 2-3kb so they shouldn't cause an issue. Please help me to understand this. Why and How does it happen? What causes and issues and how to fix it?

Perfectly fine:

foreach (Ground _en in game.blocks)
{
    e.Graphics.FillRectangle(Brushes.Green,_en.rec);
}

Causes frame drop:

foreach (Ground _en in game.blocks)
{
    e.Graphics.DrawImage(Image.FromFile(_en.background), _en.rec);
}
¿Fue útil?

Solución

You're loading the file into memory from the hard disk each time you call .FromFile(). Store the image in your Ground class on instantiation (or whenever, just not when you are calling it in Draw()) and this should be fixed.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top