如题。

解决方案 »

  1.   

    Contents
    Introduction 
    Creating Avi Movie 
    Creating WMV Movie 
    Creating QuickTime Movie 
    Introduction
    As every one knows a movie is a sequence of images or bitmaps. And also it is known that HBitmap is the basic ingredient of Bitmap. And we have lots of HBitmaps with us in all our windows applications whether they are animations or just static interfaces. And it is high time for all of us to save all those beautiful sequence of HBitmaps into a file and call it as movie or animation or demo, you name it.The following presents a way of creating a Movie (avi / wmv / mov) from a sequence of HBitmaps. The required functionality has been wrapped in appropriate classes like CAviFile, CwmvFile and CQTMovieFile. Using these classes is fairly simple and involves a single function call AppendNewFrame(); All the necessary initialization (like frame rate settings etc..) would be taken care by the class itself when the AppendNewFrame() is called for the first time. (Except for the QuickTime class. It has its own Graphics world that need to be initialized explicitly through a call to InitGraphicsWorld()).As one can easily expect - this approach is two fold - For those who want to create movie from a set of image files, say *.jpg or *.bmp, all that is needed is - load all those images into an array of HBitmaps and call AppendNewFrame() with each of them in the order of presentation. For those who want to create movie from the program generated animation sequence - just render the drawing to an HBitmap and call AppendNewFrame() on it for each update (perhaps in WM_PAINT or OnPaint() handler).Before we move on further, I would like to mention one point worth noting. These classes that we are about to discuss are primarily aimed at providing an add-on support for otherwise complete applications (though they could be used perfectly well in your not yet designed application also). To be exact, these classes have been designed with especial care so as to not to interfere with the design or functionality of the application that is using them. If any error occurs within these classes, they would rather turn off themselves than causing the entire application to halt. Hence the user is freed from any initializations and error checking's. If every thing goes well, you would have a fine movie at the end, but if any thing goes wrong (with in these modules), still you could have your application running perfectly. So with all this modularity you should not wonder if you do not see much control functions like SetFrameRate() or SetOutputfilename() etc. This is for two reasons You can add up all those functions yourselves if you want.Their presence doesn't really give you any control - because they would be one time functions and you cannot alter the name or frame rate of the movie in between. Hence you would not need them. Instead you can supply the name as part of the constructor and adjust the frame rate at compile time. Of course, if you are writing an application solely for creating a movie and does nothing else then you might find some need to change those settings in between too. But then as told above its up to you how you extend these classes. And for the rest of us who use these as just add-ons settle for some feasible frame rate at compile time and get the product completed and shipped.In the following sections details of each of these classes has been explained separately.Creating Avi Movie
    Include files : avifile.h 
    Implementation files : avifile.cpp 
    Additional Libraries : vfw32.lib 
    Our class CAviFile creates an avi movie from HBitmaps. Using it is a two step process. The First step involves creating a CAviFile object. The constructor of CAviFile has been declared in AviFile.h as:class CAviFile{
    public:
        CAviFile(LPCSTR lpszFileName=_T("Output.avi"));
        ~CAviFile(void);
        HRESULT AppendNewFrame(HBITMAP hBitmap);
        HRESULT AppendNewFrame(int nWidth, int nHeight, LPVOID pBits,
            int nBitsPerPixel=32);
      

  2.   

    The constructor accepts one argument - the output file name - which by default is set to Output.avi. The second step involved is the actual call to AppendNewFrame(). From the above it is clear that it has two overloaded alternatives. One comes with HBitmap style - where all our drawing has been done on to a HBitmap and is ready to stuffed to the end of the current movie. The other form accepts raw bitmap bits instead of HBitmap. If we have our rendered drawing in the form of bits than HBitmap - we might as well use this option. However, it should be noted that once we start with one of them we can not switch to other form in between during the movie creation. The following illustrates the code sequence:#include "avifile.h" 
    CAviFile aviFile;
    OnPaint(){
        hdc = BeginPaint(hWnd, &ps);
        //...Drawing Code onto some hBitmap
        EndPaint(hWnd, &ps);
        aviFile.AppendNewFrame(hBackBitmap);
    }
    The include file avifile.h in turn includes a header file vfw.h which requires vfw32.lib as additional library.Implementation
    This section briefly covers the behind scene mechanisms involved in the operation of CAviFile class. One can find all these from the implementation file avifile.cpp itself. Avi File creation has been one of the most oldest forms of movie creation and has lot of support through AVIFile set of functions. Before calling any AVIFile function we should call AVIFileInit() and before exiting the application we should call AVIFileExit(). The constructor and destructor are the best places for both of them. Hence you can find them in the constructor and destructor of CAviFile class respectively.Among all the set of AVIFile functions, the ones that we are interested in are : AVIFileOpen(), AVIFileRelease(), AVIFileCreateStream(), AVIMakeCompressedStream(), AVIStreamSetFormat(), AVIStreamWrite().Among the above, AVIStreamWrite() is the main operation that actually writes the compressed image bits to the movie file. All the others are used for setting the file, stream and compression options. After creating/opening the avi file withAVIFileOpen(), the compression options and video codec options can be chosen by settings appropriate values for the AVISTREAMINFO structure members and passing it to the function AVICreateStream(). For example, The fccHandler member of AVISTREAMINFO represents the four character code for the video codec. Typically the four character code for the video codec is a string such as "divx" "mpg4" etc.. that would be unique for each video codec installed in the system. The function mmioFOURCC() can be used to convert these characters into the dword format acceptable by the fccHandler member. By default, in the CAviFile implementation we use "mpg4" codec. m_AviStreamInfo.fccHandler=mmioFOURCC('M','P','G','4');
    A list of Fourcc codes and other related information can be found at:http://www.microsoft.com/whdc/hwdev/archive/devdes/fourcc.mspx 
    The member dwRate of AVISTREAMINFO controls the Frame rate of the movie. Values between 5 to 15 are common and should be good for most animations. (dwRate = 15 typically means 15 frames per second). Once we set all these options and get the new stream from AVICreateStream() function, we can call AVIMakeCompressedStream() to setup the compression filter for that stream. All our later HBitmap data would be processed with these compression filter settings. Finally after setting the compression settings, and before starting to write our actual image sequence, we need to set the format of our stream - this is done by using AVIStreamSetFormat. Once we set the format we can start writing our actual HBitmap data using the function AVIStreamWrite(). This function automatically compresses the data and writes the data to the avi stream which would be saved to file once it is closed using the function AVIFileRelease().
      

  3.   

    摘自:http://www.codeproject.com/bitmap/createmovie.asp?target=AVI,上面有源代码,祝你好运