[GDI] DrawImage unexpectedly slow

P

Paul E Collins

I'm writing a fairly simple two-player puzzle game. I have a background
image in a PNG file (44 KB, 160 x 320 pixels) which I load into an Image
object ...

imgCachedBackground = Image.FromFile(strImageFilename); // a PNG file

The Image is then painted onto two PictureBoxes several times a second ...

Graphics g = e.Graphics;
g.DrawImageUnscaled(imgCachedBackground, 0, 0);

I have provided the option of playing at double size (so that the
PictureBox, background, etc. double up to 320 x 640 pixels). Rather than
scaling the image in each Paint event, I store and use a double-size image,
like this.

imgCachedBackground = Image.FromFile(strImageFilename); // a PNG file

if (iSizeRatio > 1) // i.e. 2 for double size
{
imgCachedBackground = new Bitmap(
imgCachedBackground, new Size(
iSizeRatio * imgCachedBackground.Width,
iSizeRatio * imgCachedBackground.Height));
}

(The image is only cached once, not in every Paint event!)

The speed is fine for two players at single size and for one player at
double size, but when both PictureBoxes are at double size there is a major
drop in speed.

Can anyone explain this or suggest how I might do it more efficiently?

P.
 
B

Bob Powell [MVP]

Well Formed this month has an article on writing Windows Forms controls
using DirextX.

I think you'd get great performance if you did the blitting using DirectDraw
through the managed wrappers. GDI+ is notoriously slow, especially if you're
thinking of doing any sizable blitting more than about 5 times a second.

--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Blog http://bobpowelldotnet.blogspot.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top