Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I think it is:
1. Store in bytes.
2. Create timer.
3. Use Marshal.Copy to copy bytes by timer interval to make animation.

I just guessed it, may be there is a better way?
I am talking about GDI+ in Windows Forms, not WPF, 3d party libraries or Platform Invoke.
Posted
Comments
Sergey Alexandrovich Kryukov 19-Jan-15 15:25pm    
It depends on what do you want to animate. The way you described is pretty bad, because it assumes you already have frames. But then it would be better to pre-create a video file and show the video. And do animation with vector graphics if this is required.
Another bad thing is using the timer. Using a thread is easier and safer.
Anyway, never use System.Windows.Forms.Timer; it will literally kill your animation, due to bad... timing (accuracy). Other timers (as well as thread) will require UI thread invocation anyway, so why bothering?
—SA
Sergey Alexandrovich Kryukov 19-Jan-15 15:28pm    
If you need more detail, please tell my what should be on input of your animation, what should be shown, the goals of it, etc.
—SA
Ziya1995 20-Jan-15 2:33am    
1. I can't create a video file because it is not supported in .Net 4.0, i don't want to use Pinvoke or 3d party libraries.
2. I remember using Threads, thanks.
3. Vector Graphics? What is this?
4. I use for loop to store bytes, bytes[0][x], bytes[1][x]...and so on.
For example, i increase alpha to make light effect animation.

So, can you give me a link to read about it?
Sergey Alexandrovich Kryukov 20-Jan-15 2:57am    
1. What is not supported, for goodness sake?
2. Okay.
3. Maybe we should not even waste time discussing your questions. It might take forever...
—SA
Ziya1995 20-Jan-15 3:09am    
1. I already talked about it on this forum, making video files are not supported in .Net.
3. Ok, sorry, i just need a link to a page where i can learn how to make animation better than what i am doing now. And i know what is vector graphics and already have animations using vector graphics, but i thought you mean something else.

I already made animations and use them in my game, i am just curious if there is a better way. You said my way is bad, so you can send me a link where is a better way or just teach me.

I have no idea how to create animation without frames or recreating vector images, but i guess low level code can create a video file which is a better way, but as i said it is not supported in .Net, i need to use 3d party ways, but i don't want.

Code:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;

namespace _123
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            bitmap = new Bitmap(500, 500);
            g = Graphics.FromImage(bitmap);
            stopwatch = new Stopwatch();

            this.DoubleBuffered = true;
            this.Paint += Draw;

            stopwatch.Start();
            (new Thread(new ThreadStart(Drawing_Thread))).Start();
        }

        Stopwatch stopwatch;

        Bitmap bitmap;
        Graphics g;

        int x = 20;
        int add = 1;

        private void Drawing_Thread()
        {
            while (true)
                if (stopwatch.Elapsed.Milliseconds >= 1)
                {
                    stopwatch.Stop();
                    if (x == 300) add *= -1;
                    if (x == 0) add *= -1;
                    g.Clear(Color.Transparent);
                    g.FillRectangle(Brushes.Red, x += add, 50, 100, 100);

                    this.Invoke(new Action(this.Invalidate));
                    stopwatch.Restart();
                }
        }

        private void Draw(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(bitmap, 0, 0);
        }
    }
}
 
Share this answer
 
v7
Comments
Sergey Alexandrovich Kryukov 20-Jan-15 10:36am    
It's hard to invent something even more inefficient that that.
The use of System.Windows.Forms.Timer doesn't leave much hope for smoothness.
The solution is: render directly on some control, without any images, by drawing on the instance of Graphics passed to overridden OnPaint of this control, use Invalidate on each frame, the amount move calculate according to real time taken using System.Diagnostics.Stopwatch (not frame number), instead of timer use thread with cycle, in this thread, all UI operation call through Control.Invoke. That's all.
—SA
Ziya1995 22-Jan-15 2:27am    
1. I have a vector based animation and a bitmap based animation.
Bitmap based draws smoother, because i don't need to recalculate lots of numbers and that increases performance.
2. Using Threads. Your quote:

"organize infinite loop, use System.Threading.Thread.Sleep for certain period of time inside the loop. Then calculate current time using System.DateTime.Now; depending on current time, update BallPosition and call Form.Invalidate."

And now my question - how can you get precise DateTime using Thread Sleep which is not precise?
Here is a quote:
"Timers are not guaranteed to execute exactly when the time interval occurs, but they are guaranteed to not execute before the time interval occurs."
It is impossible to capture a right moment using Thread.Sleep, but you can capture when it is lower or upper than the right moment.

For example, we wanna capture 1000 milliseconds,
Thread Sleep counts 1000 milliseconds, but it mistakes by 50 milliseconds and we get 1050 milliseconds.
Inaccuracy: 50 milliseconds.
And now we can use your way to reduce inaccuracy down to nearly 1000 milliseconds to get 1000 -+ 5 milliseconds if to consider Thread.Sleep inaccuracy as 5 milliseconds by 1 interval.

> You talked here enough not to help you.
I am friend, not enemy)
Sergey Alexandrovich Kryukov 22-Jan-15 2:41am    
Precise time:

Say, you have some moving body, let's say, y = at^2 + vx * t + x0, y = vx * t; or whatever.
Don't try to get to the point where you draw a frame in exact equal period of time. Get there when the thread put you in this point. Just measure t. As I advised, use System.Diagnostics.Stopwatch.

Your approach is wrong, upside down. Animation smoothness is being at right point at given time, not making certain time for a point. Implement it; and you will see perfect smoothness.

—SA
Ziya1995 12-Feb-15 12:17pm    
Look, i updated solution and that works greatly!
Is it what you meant?
is it the best way?
Mistake me or approve it as the fully usable principle for my next projects.
Sergey Alexandrovich Kryukov 12-Feb-15 13:08pm    
I don't know the rendering and Invalidate can work. I think you have to invoke it to the UI thread using this.Invoke(new Action(()=>{/* all UI-related part here */}))...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900