简体   繁体   中英

C# save bitmap output

I'm creating a C# program that's capturing the screen with bitmap. And than I want to save it to an .Avi/ .mpeg file. 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

I think this might be your best solution. Store all the .jpg's and create an avi from the command line at intervals. I don't see how creating video on the fly would produce a "lightweight" solution.

Hello click this to download the aviwrapper liblary. 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();

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