Groups | Blog | Home
all groups > c# > february 2008 >

c# : Moving Graphics Question



Peter Duniho
2/1/2008 2:10:32 PM
On Fri, 01 Feb 2008 11:43:03 -0800, Brian Ward <brian.ward@zetnet.co.uk>=
=

[quoted text, click to view]
y =

[quoted text, click to view]

This is entirely wrong. The only thing your Tick handler should do is =

update whatever data structure controls the location of the image, and =

then invalidate the area of the control in which you want to draw. Then=
=

you should have a Paint handler (or override OnPaint(), if you've =

sub-classed the control) where you use the location information to draw =
=

the image in the appropriate spot.

Ironically, doing it correctly the issue of "clearing the image" between=
=

updates is trivial. It just happens for you without any additional =

effort. The only reason you ran into the problem in the first place was=
=

that you're misusing the .NET paint mechanism.

As for your question about WMF images, I don't know. I've found that =

..NET's support for Metafiles is somewhat lacking elsewhere, so it's =

possible it's also just poorly tested in this particular scenario as =

well. I suppose it's possible that you're doing something wrong, but I =
=

doubt it. A WMF should behave just like a Bitmap when used with any API=
=

that requires only an Image. The fact that you can get correct behavior=
=

just by converting the WMF to a Bitmap suggests that the API is broken.

Nicholas Paldino [.NET/C# MVP]
2/1/2008 3:38:36 PM
Brian,

I'm not sure about loading the wmf file and it not stretching. The only
thing I can think of in regards to that is to set the BackgroundImageLayout
BEFORE the BackgroundImage.

In regards to the animation, calling CopyFromScreen isn't going to help.
You could call Clear, passing the BackgroundImage, and THEN call DrawImage,
and it should work.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

[quoted text, click to view]
Brian Ward
2/1/2008 7:43:03 PM
I have a moving bitmap graphic, driven by a timer, on a form that has a
background image.
I want to clear the image after each movement to avoid a trail being
left behind. I can do this with a simple background colour by using
g.Clear() with the relevant colour .. but I'm not clear(sorry!) about
how to do it when there is a background image.
Some relevant code in the tick method is :
Size s1 = new Size(150,120);
Bitmap im1 = new Bitmap("myPic.gif" ,s1);
Graphics g = CreateGraphics();
g.DrawImage(im1, x,y);
g.Clear(this.BackgroundImage); // no .. not this!
g.CopyFromScreen(x,y,x,y,s1); // is this perhaps nearly right?
The last line gives a partial solution .. is this the right way?
----
Another thing I don't understand .. if I set the background image (a wmf
file) in form design mode and set the stretch mode .. the background is
fine .. but if do this at runtime when the form loads:
this.BackgroundImage = Image.FromFile("City2.wmf");
this.BackgroundImageLayout = ImageLayout.Stretch;
the image doesn't fill the screen .. while another image doesn't show at all!
If I convert them to bitmaps before loading up .. its OK .. but I don't
like not understanding why I have to do this.
TIA
===
Brian
===

Chris Dunaway
2/4/2008 7:58:16 AM
On Feb 1, 4:10 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
[quoted text, click to view]

In support of Peter's post, please see these links:

http://www.bobpowell.net/creategraphics.htm
http://www.bobpowell.net/graphicsobject.htm
http://www.bobpowell.net/faqmain.htm

AddThis Social Bookmark Button