1

我正在创建一个使用位图捕获屏幕的 C# 程序。而且我想将其保存到 .Avi/ .mpeg 文件中。但我不知道如何将其保存到视频中。

这是我已经拥有的代码。

public Form1()
    {
        InitializeComponent();
    }
    static Bitmap bm;
    private void btnFolder_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog folderDlg = new FolderBrowserDialog();
        folderDlg.ShowNewFolderButton = true;
        DialogResult result = folderDlg.ShowDialog();
        if (result == DialogResult.OK)
        {
            textBox1.Text = folderDlg.SelectedPath;
            Environment.SpecialFolder root = folderDlg.RootFolder;
        }
    }

    private void btnStart_Click(object sender, EventArgs e)
    {
        timer1.Start();
    }

    private void btnStop_Click(object sender, EventArgs e)
    {
        timer1.Stop();
        SaveCapture(textBox1.Text);
    }
    private void SaveCapture(string path)
    { 
        // Here should be the code to save it to mpeg/avi
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        // Take screenshot
        bm = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
        Graphics graphics = Graphics.FromImage(bm as Image);
        graphics.CopyFromScreen(0, 0, 0, 0, bm.Size);
        pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

        // Show it in picturebox
        pictureBox1.Image = bm; 
    }

非常感谢!

4

2 回答 2

1

你好,点击这里 下载 aviwrapper 库。你应该写的代码是这样的:

var pngFileList = Directory.EnumerateFiles(folderImages, "*.png");
//load the first image
Bitmap bitmap = (Bitmap)Image.FromFile(pngFileList.First());
//create a new AVI file
AviManager aviManager = new AviManager(fileName, false);  // location and the name of video file

//add a new video stream and one frame to the new file
//set IsCompressed = false
VideoStream aviStream = aviManager.AddVideoStream(false, 3, bitmap);

pngFileList.Skip(1).ToList().ForEach(file =>
{
  bitmap = (Bitmap)Bitmap.FromFile(file);
  aviStream.AddFrame(bitmap);
  bitmap.Dispose();
 });

 aviManager.Close();
于 2013-06-20T14:27:46.160 回答
1

从一系列图像创建视频流 (AVI)

我认为这可能是您最好的解决方案。存储所有 .jpg 文件并每隔一段时间从命令行创建一个 avi。我看不出动态创建视频会如何产生“轻量级”解决方案。

于 2013-05-26T19:10:42.687 回答