C#保存位图输出

时间:2013-05-26 18:41:41

标签: c# bitmap

我正在创建一个用位图捕获屏幕的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; 
    }

非常感谢!

2 个答案:

答案 0 :(得分:1)

Create a Video Stream (AVI) from a Series of Images

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

答案 1 :(得分:1)

Hello click this下载api包装器库。您应该编写的代码是:

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();