Home > Software design >  Capture desktop with Aforge
Capture desktop with Aforge

Time:01-04

I have seen various tutorial like enter image description here

Thanks

but when I look at the png is all black. The saving procedure is correct for when I save the webcam frame it is ok. Could you please tell me what is wrong in my adaptation from your code?

Thanks

CodePudding user response:

It may not be a professionally solution and not realtime but it does the work for simple scenarios like yours. It is a console application built with Net Framework 4.7.2. And I rebuilt the assembly Aforge.Video.FFMPEG by using its source code so that it can be used with Net Framework 4.0 .

using AForge.Video.FFMPEG;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

class Program
    {
        static VideoFileWriter VideoFileWriter;
        static Stopwatch stopwatch = new Stopwatch();
        static void Main(string[] args)
        {
            VideoFileWriter = new VideoFileWriter();

            Size screenSize = Screen.PrimaryScreen.Bounds.Size;
            VideoFileWriter.Open("test.mp4", screenSize.Width, screenSize.Height, 30, VideoCodec.MPEG4, 19200000);
            Bitmap bitmap = new Bitmap(screenSize.Width, screenSize.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            Graphics g = Graphics.FromImage(bitmap);
            stopwatch.Start();
            while (stopwatch.Elapsed < TimeSpan.FromSeconds(10)) // Record for ten seconds.
            {
                g.CopyFromScreen(0, 0, 0, 0, screenSize, CopyPixelOperation.SourceCopy);
                VideoFileWriter.WriteVideoFrame(bitmap, stopwatch.Elapsed);
                Thread.Sleep(29);
            }
            VideoFileWriter.Close();
            g.Dispose();
        }
    }
  •  Tags:  
  • Related