简体   繁体   中英

How can I create an avi video file in real time while my application is processing?

Edit:

This is what I did: In Form1:

ScreenCapture sc;
AviFile af;

In Form1 constructor:

sc = new ScreenCapture();
af = new AviFile();

In Form1 timer1 tick event:

private void timer1_Tick(object sender, EventArgs e)
        {
            if (playImages == true)
            {
                timer1.Enabled = false;
                play();
                timer1.Enabled = true;
            }
            else
            {
                af.CreateAvi(this.sc);
            }
        }

The AviFile class after changed it:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using AviFile;
using ScreenShotDemo;

namespace ScreenVideoRecorder
{
    class AviFile
    {
        AviManager aviManager;

        Bitmap bmp;

        public AviFile()
        {
            aviManager = new AviManager(@"d:\testdata\new.avi", false);
        }

        public void CreateAvi(ScreenCapture sc)
        {
            bmp = new Bitmap(sc.CaptureScreen());
            VideoStream aviStream = aviManager.AddVideoStream(false, 25, bmp);//dlg.Rate, bmp);
            aviStream.AddFrame(bmp);
            bmp.Dispose();
        }

        public AviManager avim
        {
            get
            {
                return aviManager;
            }
            set
            {
                aviManager = value;
            }
        }
    }
}

In Form1 button click event to stop:

private void button3_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            if (af != null)
            {
                af.avim.Close();
            }
        }

But before I stop it after running it after few seconds of taking screenshots: This is button1 which starts the timer and take screenshots:

private void button1_Click(object sender, EventArgs e)
        {
            playImages = false;
            timer1.Enabled = true;
        }

After few seconds I'm getting an exception:

Exception in AVIFileCreateStream: -2147205019


System.Exception was unhandled
  HResult=-2146233088
  Message=Exception in AVIFileCreateStream: -2147205019
  Source=AviFile
  StackTrace:
       at AviFile.VideoStream.CreateStreamWithoutFormat()
       at AviFile.VideoStream.CreateStream()
       at AviFile.VideoStream..ctor(Int32 aviFile, Boolean writeCompressed, Double frameRate, Bitmap firstFrame)
       at AviFile.AviManager.AddVideoStream(Boolean isCompressed, Double frameRate, Bitmap firstFrame)
       at ScreenVideoRecorder.AviFile.CreateAvi(ScreenCapture sc) in d:\C-Sharp\ScreenVideoRecorder\ScreenVideoRecorder\ScreenVideoRecorder\AviFile.cs:line 27
       at ScreenVideoRecorder.Form1.timer1_Tick(Object sender, EventArgs e) in d:\C-Sharp\ScreenVideoRecorder\ScreenVideoRecorder\ScreenVideoRecorder\Form1.cs:line 61
       at System.Windows.Forms.Timer.OnTick(EventArgs e)
       at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at ScreenVideoRecorder.Program.Main() in d:\C-Sharp\ScreenVideoRecorder\ScreenVideoRecorder\ScreenVideoRecorder\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

The exception is in the class: VideoStream which is not my class it's from the site I downloaded the source files: http://www.codeproject.com/Articles/7388/A-Simple-C-Wrapper-for-the-AviFile-Library

How can I fix it?

It's not entirely clear from your question what you're asking.

But it appears to be the case that you want to just put the screenshots into the AviFile directly instead of saving them to the disk and then loading them in.

The key piece of code is this:

bitmap = (Bitmap)Bitmap.FromFile(Form1.imagesFiles[n]);
aviStream.AddFrame(bitmap);
bitmap.Dispose();

Instead of loading them into memory from a file you can just do:

bitmap = new Bitmap(sc.CaptureScreen());
aviStream.AddFrame(bitmap);
bitmap.Dispose();

That way you're not saving to disk, you're just inserting directly into the AVI stream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM