Tuesday, March 31, 2009

MS Paint Animation

I gave MS Paint the ability to make animations and save them as .avi files.

I wanted to write a "flipbook" program, where you could draw a series of frames and create a simple animation. I think this type of program could be fun for kids.

I was about to write the program in Pygame, but found myself re-implementing many of the standard bitmap editing tools. It's easy to make a rectangle and oval tool, but I didn't really feel like making the fill tool or selection tool. So, instead, I used MSPaint as part of the interface to the program. (This is completely a hack, and the resulting program isn't robust, but it was kind of interesting to do). See a video of how to use it - you can move from frame to frame, duplicate the current frame, and play the animation.

I'm trying to make this look like just one program. A lot is going on behind the scenes. The program is a c# app that, first, launches Paint. It has a window style that causes it to be on top of other windows. The program uses many Windows API SendKey calls to send key events to Paint. When you move from one frame to the next, it does the following:
  • Tell paint to select all (Ctrl A), and cut (Ctrl X)
  • Take that image from the clipboard and save it to a .png file
  • Open the next .png file in memory and put it in clipboard
  • Tell paint to paste (Ctrl V) and deselect (Esc)
The other operations are done with a similar series of events. I had to tune the timing; the c# program sleeps while waiting for Paint. The play preview actually opens up a borderless c# window that is positioned so that it appears above the image, and cycles through the images.

It ended up working. The Win api gives you almost too much to play with. Now I know that c# apps can send simulated keystrokes to other processes, for semi-practical purposes.

1 comment:

Layl said...

Hey, this is awesome !
I'm trying to make my own program to improve mspaint too; I want to make it able to draw recognising the pressure of the pen. I'm writing my program in C++, and i'll probably have to use Windows API SendKey calls too.
But that's not the problem. Actually, I want to use your program, it looks really cool, you should let it downloadable somewhere :D

(sry I'm not english, there is probably some mistakes)