문제

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);
}
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top