473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to create an animation in C# without WPF?

10
how can I create an animation in c# without WPF, I want to rotate an image in pictureBox. I did it using Graghics.DrawImage, in loop, where in each time I draw the image in other points. but cause the image has high resolution it's take a long time to rotate. so I want to do it easier by using animation. but I have a problem that I don't use WPF. so I want to know if there is a possibility to do it in C# without WPF.
thanks.
Feb 6 '11 #1
3 6448
GaryTexmo
1,501 Expert 1GB
You need two things here... an update loop, and a draw loop. The draw loop is pretty easy, just set up a timer to invalidate your control at an interval for the amount of FPS you want. If you're not doing any heavy animation you can just set this to something like 60 FPS and let it go.

I posted code that's similar to this in another thread, take a look:
http://bytes.com/topic/c-sharp/answe...es#post3645943

Now the update loop could be a little more complicated. If you can fit all your update logic into your 60 Hz cycle, you could probably call an update before you did your paint. If you can't, or you want it to update more frequently, you're free to create an update timer as well; however, you should always include some kind of DateTime variable so that you can use that in your calculations.

For example, lets say you want to move a ball across the screen and your update loop occurs at 30 Hz. In that update loop, if you increment the ball's position by a fixed number, the speed of your ball depends entirely on that update loop's run frequency. Now, if you use a time variable and use that to calculate the ball's position based on velocity and time, the update loop can run at any frequency you like and the ball will still update to the correct position every time.

I hope that makes sense and helps you, please let me know if you have any questions :)
Feb 6 '11 #2
st mnmn
10
thanks a lot.
maybe I should explain myself better, and show you my code...
I have a loop for delay,and for draw. but I think the problem is something else, (I haven't writen it before, sorry...):
each time in the draw loop I refresh the pictureBox so that it gets the new properties. and my pictureBox has as children-controls many user controls. and when I refresh it, it's refreshed with all its controls, so it takes a long time to rotate.
Is there any way to refresh/repaint only the image of the pictureBox?
or you have another solution for my problem...

here is my code:

Expand|Select|Wrap|Line Numbers
  1. int d = 10;
  2. Bitmap img = new Bitmap((Controls["Board" + boardId] as PictureBox).Image);
  3. graphic = Graphics.FromImage((Controls["Board" + boardId] as PictureBox).Image);
  4.  
  5. switch (dir)
  6. {
  7.     case "Right":
  8.     {
  9.         for (int i = 0; i < 28; i++)
  10.         {
  11.            graphic.FillRectangle(Brushes.Silver,0,0,280,280);
  12. graphic.DrawImage(new Bitmap(img), new Point[] { new Point(d, 0), new Point(280,d), new Point(0,280-d) });
  13. (Controls["Board" + boardId] as PictureBox).Refresh ();
  14. Delay(50);
  15. d += 10;
  16.       }
  17.    } break;
  18.  
  19. //the delay function:
  20. public  void Delay(int ms)
  21. {
  22.     DateTime sof=DateTime .Now .AddMilliseconds (ms);
  23.     do
  24.     { } while (DateTime.Now <= sof);
  25. }
I hope you understand me and can help me.
anyway, thanks a lot!
Feb 7 '11 #3
GaryTexmo
1,501 Expert 1GB
Yea, you can certainly draw directly to the picture box instead of having to draw to the image on it. You can just put your drawing code in the Paint event for the PictureBox and use the PaintEventArgs parameter to access the graphics object for it.

Here's a quick example...
Expand|Select|Wrap|Line Numbers
  1.     public partial class Form1 : Form
  2.     {
  3.         public Form1()
  4.         {
  5.             InitializeComponent();
  6.  
  7.             pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
  8.         }
  9.  
  10.         private void pictureBox1_Paint(object sender, PaintEventArgs e)
  11.         {
  12.             PictureBox thePB = sender as PictureBox;
  13.  
  14.             if (thePB != null)
  15.             {
  16.                 Graphics g = e.Graphics;
  17.                 Point c = new Point(thePB.Width / 2, thePB.Height / 2);
  18.  
  19.                 for (int r = 2; r < thePB.Width / 2; r += 10)
  20.                 {
  21.                     g.DrawArc(Pens.Red, c.X - r / 2f, c.Y - r / 2f, r, r, 0f, 360f);
  22.                 }
  23.  
  24.                 g.FillRectangle(Brushes.Black, c.X - 3, c.Y - 3, 6, 6);
  25.             }
  26.         }
  27.     }
IMPORTANT: You're still using a fixed delay in your draw loop, which is going to cause problems with your form's responsiveness. While you're animating, you'll be stuck in a loop. This is what I was talking about earlier, using a timer to trigger a draw at a set interval. Please have a look at the thread I posted earlier, there's an example of exactly what you need there. Don't force a delay on your draw, simply draw at certain intervals.
Feb 7 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Brian Angliss | last post by:
I'm relatively new to scripting in JavaScript, so I'm not too surprised I'm having difficulty scripting up an animation effect for my personal site. What I'm trying to do is the following: When...
5
by: wmschneider | last post by:
I am trying to make a progress animation so that the user knows that there files are correctly being checked in. Trying to animate papers moving from the computer to the server pics. I'm brand...
0
by: Max99 | last post by:
Hello I'm developing an MDI program with Microsoft visual C# .net In the program for opening the forms I use Form1 theForm = new Form1(); theForm.Show();
10
by: Robert Skidmore | last post by:
Take a look at this new JS function I made. It is really simple but very powerful. You can animate any stylesheet numeric value (top left width height have been tested), and works for both % and px...
4
by: petermichaux | last post by:
Hi, Is there any way to make DOM scripted animation smoother? Flash is far superior in this area. Any one here know what makes Flash so smooth by comparison? I don't like the fact that the...
2
by: rdemyan via AccessMonster.com | last post by:
My application has a lot of complicated SQL statements, calculations, processing that takes time. I've created a custom form to act like a messagebox. It has 10 small rectangles on it that change...
1
by: Martijn Mulder | last post by:
I am looking for a C# .NET 2.0 tutorial on animation techniques, especially the difficult subject of Invalidating() the smallest possible area on the screen and the proper way to set things up.
0
Frinavale
by: Frinavale | last post by:
I have a peculiar problem with an UpdatePanelAnimationExtender wherein the animation for the OnUpdating event only fires the first time the UpdatePanel it target's performs a PostBack. The...
6
by: dantz | last post by:
HI everyone, I hope someone can help me on this. I have form application that has 3 Timers that does an animation (changing an image for every interval) Each image are loaded at start of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.