By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,701 Members | 2,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,701 IT Pros & Developers. It's quick & easy.

[GDI] DrawImage unexpectedly slow

P: n/a
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.

--
www.CL4.org
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

"Paul E Collins" <fi******************@CL4.org> wrote in message
news:bm*********@hercules.btinternet.com...
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.

--
www.CL4.org

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.