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

Animation and windows progs

P: n/a
Hello,

I'm new to c# and have created an animation which draws directly
to the window, it uses the code below which all works. I would
like to know if this is the 'correct' way to do it - or would an
experienced c# programmer do it another way?

This part defines the screen and the graphics pad I'm drawing to:
Graphics screen = CreateGraphics();
Graphics drawpadg = Graphics.FromImage(drawpad);

Next, the background and the frames of animation
Bitmap back =
(Bitmap)Bitmap.FromStream(this.GetType().Assembly. GetManifestResourceStream("WindowsApplication1.sky ground.gif"));
Bitmap lemframes =
(Bitmap)Bitmap.FromStream(this.GetType().Assembly. GetManifestResourceStream("WindowsApplication1.lem walk.gif"));

Clear the pad I draw to
drawpadg.Clear(this.BackColor);

Copy the background and then the frame of animation to the draw pad.
drawpadg.DrawImage(back, new Rectangle(0, 0, 400, 120), 0,
0, 400, 120, GraphicsUnit.Pixel);
drawpadg.DrawImage(lemframes, new Rectangle(x+lo[f], y,
lw[f], 46), lx[f], 0, lw[f], 46, GraphicsUnit.Pixel);

Finally copy the draw pad to the screen
screen.DrawImage(drawpad, 0, 0);

Then I dispose of everything afterwards (like a good little c# coder
:o)

I also want to start developing an application which allows you to
scroll around an image, and which lets you resize the window (and have
the scroll bars update). I cannot get the scroll bars to appear by
playing with the properties - how should I be doing this please?

Ta, James

Dec 28 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
James,

It's hard to say if this is "right" or not.

I don't see you disposing of anything anywhere (the graphics instance,
the bitmaps, the streams, anything), so I can't say if that's correct.

On top of that, where is this code? Are you overriding your OnPaint
method, or in the Paint event handler? If it is not in either of these
places, then you aren't going to have a very smooth animation.

Finally, you can probably make this better. Since you are painting
every frame, you probably could pre-load the frames of the animation, so
that you don't have to incur the cost of loading while drawing.

Hope this helps.
"James" <c_********@yahoo.co.uk> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hello,

I'm new to c# and have created an animation which draws directly
to the window, it uses the code below which all works. I would
like to know if this is the 'correct' way to do it - or would an
experienced c# programmer do it another way?

This part defines the screen and the graphics pad I'm drawing to:
Graphics screen = CreateGraphics();
Graphics drawpadg = Graphics.FromImage(drawpad);

Next, the background and the frames of animation
Bitmap back =
(Bitmap)Bitmap.FromStream(this.GetType().Assembly. GetManifestResourceStream("WindowsApplication1.sky ground.gif"));
Bitmap lemframes =
(Bitmap)Bitmap.FromStream(this.GetType().Assembly. GetManifestResourceStream("WindowsApplication1.lem walk.gif"));

Clear the pad I draw to
drawpadg.Clear(this.BackColor);

Copy the background and then the frame of animation to the draw pad.
drawpadg.DrawImage(back, new Rectangle(0, 0, 400, 120), 0,
0, 400, 120, GraphicsUnit.Pixel);
drawpadg.DrawImage(lemframes, new Rectangle(x+lo[f], y,
lw[f], 46), lx[f], 0, lw[f], 46, GraphicsUnit.Pixel);

Finally copy the draw pad to the screen
screen.DrawImage(drawpad, 0, 0);

Then I dispose of everything afterwards (like a good little c# coder
:o)

I also want to start developing an application which allows you to
scroll around an image, and which lets you resize the window (and have
the scroll bars update). I cannot get the scroll bars to appear by
playing with the properties - how should I be doing this please?

Ta, James

Dec 28 '05 #2

P: n/a
Hi Nicholas.

I am disposing of everything afterwards:

back.Dispose();
screen.Dispose();
drawpadg.Dispose();
lemframes.Dispose();

The code is in the timer tick, and you're totally right in that I do
load the animation every frame which I know is wasteful and I intend to
correct that soon (ie to load the images in the public Form1(), but one
problem at a time :o)

Thanks

James

Dec 30 '05 #3

P: n/a
James wrote:
Hi Nicholas.

I am disposing of everything afterwards:

back.Dispose();
screen.Dispose();
drawpadg.Dispose();
lemframes.Dispose();

The code is in the timer tick, and you're totally right in that I do
load the animation every frame which I know is wasteful and I intend to
correct that soon (ie to load the images in the public Form1(), but one
problem at a time :o)

Thanks

James


A (IMHO) better approach would be to just Invalidate your control in the
timer tick (and possibly increment a counter value/update the animation
variables) and paint everything in the OnPaint handler. This way, your
control will repaint properly when partially invalidated etc. And you
should not implement your own doublebuffering unless really needed. Look
at the Control.SetStyle() and ControlStyles.DoubleBuffer instead.

HTH

Stefan
Dec 30 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.