简体   繁体   English

C#保存位图输出

[英]C# save bitmap output

I'm creating a C# program that's capturing the screen with bitmap. 我正在创建一个使用位图捕获屏幕的C#程序。 And than I want to save it to an .Avi/ .mpeg file. 而且比我想将其保存到.Avi / .mpeg文件。 But I don't know how to save it to a video. 但是我不知道如何将其保存到视频中。

Here is the code I already have. 这是我已经拥有的代码。

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; 
    }

Thank you very much! 非常感谢你!

Create a Video Stream (AVI) from a Series of Images 从一系列图像创建视频流(AVI)

I think this might be your best solution. 我认为这可能是您最好的解决方案。 Store all the .jpg's and create an avi from the command line at intervals. 存储所有.jpg文件,并从命令行间隔创建一个avi。 I don't see how creating video on the fly would produce a "lightweight" solution. 我看不到即时创建视频如何产生“轻量级”解决方案。

Hello click this to download the aviwrapper liblary. 您好, 点击这里下载aviwrapper库。 And the code that you should write is this: 您应该编写的代码是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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