Create a desktop video recorder in C#

We have to record our desktop activity for make various reason, like make a demo for an application, or making video tutorial for an application etc.

the prototype application is like

This project can be done in 3 steps

  • taking screenshot of ur desktop continously
  • Make an video file using the images

So first step taking screenshot of ur desktop continously

so first we have to take the screen shot of the desktop, for that we have to write the code
int ScreenWidth = Screen.PrimaryScreen.Bounds.Width;
int ScreenHeight = Screen.PrimaryScreen.Bounds.Height;
Graphics g;
Bitmap b = new Bitmap(ScreenWidth, ScreenHeight);
g = Graphics.FromImage(b);
g.CopyFromScreen(Point.Empty, Point.Empty, Screen.PrimaryScreen.Bounds.Size);

Now we have to make avi file from these images, for that we aviwrapper u can download it from here.

now about making avi file from these images , the code i used is like
AviManager aviManager = new AviManager(@"..\..\Output\output.avi", false);
VideoStream aviStream = null;
aviStream = aviManager.AddVideoStream(true,4, bi);
aviStream.AddFrame(b);

the link for full source code is here

Advertisements

About kishor datta gupta

Graduate Research Assistant at University of Memphis Software Engineer at Silicon Orchard LTD. Former Research Assistant at Lamar University Former Software Engineer at Samsung R&D Institute Bangladesh Studies Ph.D. Computer Science at University of Memphis Studied Masters of Science in Computer Sciences at Lamar University Studied BSC in CSE at Khulna University of Engineering and Technology Studied HSC (completed) at Chittagang college 04-06 Studied High school at ST. Placid's High School'04 Studied Junior Secondary School at Saint Mary's School Lives in Memphis, Tennessee
This entry was posted in C#, Winform app and tagged , , , , . Bookmark the permalink.

31 Responses to Create a desktop video recorder in C#

  1. akhtar ali says:

    Kindly could you give us code for how we could make a video from bitmaps. Similar concept what you have done in Create a desktop video recorder in C# but not from screen instead use bitmaps

    please must reply.

    thanks

  2. akhtar ali says:

    Thanks for replying, I am really thrilled with so quick reply but my problem not solved from the links you suggested.
    I have created a bitmap from pixels in this way.
    try
    {
    // Retrieve the image.
    //Bitmap image1 = new Bitmap(@”C:\Users\Bilal\Documents\Visual Studio 2010\Projects\WindowsFormsApplication5\bitmap\bitmap\bin\a”, true);

    // Color c=Color.FromArgb(k);
    int x, y;

    // Loop through the images pixels to reset color.
    for (x = 0; x < image1.Width; x++)
    {
    for (y = 0; y < image1.Height; y++)
    {
    Color pixelColor = image1.GetPixel(x, y);
    //Color newColor = Color.FromArgb(pixelColor.R, pixelColor.B, pixelColor.G);

    }
    }

    for (int i = 0; i < image1.Width; i++)
    {

    for (int j = 0; j < image1.Height; j++)
    {
    Color c = new Color();

    int r = 0;

    r = Convert.ToInt16( c.R);

    int g = 0;

    g = Convert.ToInt16(c.G);

    int b = 0;

    b = Convert.ToInt16(c.B);

    int max1 = System.Math.Max(r, g);

    int final = System.Math.Max(max1, b);

    r = final;

    g = final;

    b = final;

    c = Color.FromArgb(r, g, b);

    // image1.SetPixel(r, g, b);

    image1.SetPixel(i, j, c);
    }
    }

    // Set the PictureBox to display the image.
    pictureBox1.Image = image1;

    // Display the pixel format in Label1.
    label1.Text = "Pixel format: " + image1.PixelFormat.ToString();

    }
    catch (ArgumentException)
    {
    MessageBox.Show("There was an error." +
    "Check the path to the image file.");
    }
    but now i want to convert these bitmaps to video and i have no clue how to proceed,
    kindly if you could guide me.
    what would be possible alternative for this method
    g.CopyFromScreen(Point.Empty, Point.Empty, Screen.PrimaryScreen.Bounds.Size);
    to create video from bitmap made above not from CopyFromScreen methods.
    thanks

    • kishordgupta says:

      try
      {AviManager aviManager = new AviManager(@”..\..\Output\output.avi”, false);
      VideoStream aviStream = null;
      aviStream = aviManager.AddVideoStream(true,4, bi);

      // Retrieve the image.
      //Bitmap image1 = new Bitmap(@”C:\Users\Bilal\Documents\Visual Studio 2010\Projects\WindowsFormsApplication5\bitmap\bitmap\bin\a”, true);

      // Color c=Color.FromArgb(k);
      int x, y;

      // Loop through the images pixels to reset color.
      for (x = 0; x < image1.Width; x++)
      {
      for (y = 0; y < image1.Height; y++)
      {
      Color pixelColor = image1.GetPixel(x, y);
      //Color newColor = Color.FromArgb(pixelColor.R, pixelColor.B, pixelColor.G);

      }
      }

      for (int i = 0; i < image1.Width; i++)
      {

      for (int j = 0; j < image1.Height; j++)
      {
      Color c = new Color();

      int r = 0;

      r = Convert.ToInt16( c.R);

      int g = 0;

      g = Convert.ToInt16(c.G);

      int b = 0;

      b = Convert.ToInt16(c.B);

      int max1 = System.Math.Max(r, g);

      int final = System.Math.Max(max1, b);

      r = final;

      g = final;

      b = final;

      c = Color.FromArgb(r, g, b);

      // image1.SetPixel(r, g, b);

      image1.SetPixel(i, j, c);
      }
      }

      // Set the PictureBox to display the image.
      pictureBox1.Image = image1;
      aviStream.AddFrame( image1);
      // Display the pixel format in Label1.
      label1.Text = "Pixel format: " + image1.PixelFormat.ToString();

      }
      catch (ArgumentException)
      {
      MessageBox.Show("There was an error." +
      "Check the path to the image file.");
      }

      just add avi dll in reference and write the code use
      aviStream.AddFrame( image1); //image1 as bitmap
      hope it would help

  3. akhtar ali says:

    Thanks alot for replying and your interest in our problem.
    Yes this code helped me very much and i have created video from bitmap.

  4. akhtar ali says:

    code for Making a video from bitmaps………
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Drawing.Printing;
    using System.Drawing.Drawing2D;
    using System.Data;
    using AviFile;
    using System.Runtime.InteropServices;
    using System.IO;

    namespace bitmap
    {
    public partial class Form1 : Form
    {

    //AviManager aviManager = new AviManager(“C:\\Users\\Bilal\\Documents\\Visual Studio 2010\\Projects\\WindowsFormsApplication5\\bitmap\\Output\\output.avi”, false);
    int m = 0;
    //VideoStream aviStream = null;
    Bitmap[] image1 = new Bitmap[50];

    //private ArrayList streams = new ArrayList();
    public Form1()
    {
    //image1 = new Bitmap(500, 500);
    image1[0] = new Bitmap(500, 500);
    InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {
    try
    {
    // int k = 0;
    AviManager aviManager = new AviManager(“C:\\Users\\Bilal\\Documents\\Visual Studio 2010\\Projects\\WindowsFormsApplication5\\bitmap\\Output\\output.avi”, false);
    VideoStream aviStream = null;

    aviStream = aviManager.AddVideoStream(true,2, image1[0]);
    // Retrieve the image.
    //Bitmap image1 = new Bitmap(@”C:\Users\Bilal\Documents\Visual Studio 2010\Projects\WindowsFormsApplication5\bitmap\bitmap\bin\a”, true);

    // Color c=Color.FromArgb(k);
    int x, y;

    for (int count = 0; count < 10; count++)
    {

    image1[count] = new Bitmap(500, 500);
    //aviStream = aviManager.AddVideoStream(true, 4, image1[count]);
    // Loop through the images pixels to reset color.
    for (x = 0; x < image1[count].Width; x++)
    {
    for (y = 0; y < image1[count].Height; y++)
    {
    m = m + 22;
    Color c1 = Color.FromArgb(m);
    image1[count].SetPixel(x, y, c1);
    //Color newColor = Color.FromArgb(pixelColor.R, pixelColor.B, pixelColor.G);

    }
    }

    //m = m + 40;

    for (int i = 0; i < image1[count].Width; i++)
    {

    for (int j = 0; j < image1[count].Height; j++)
    {
    Color c = new Color();
    c = image1[count].GetPixel(i, j);

    int r = 0;

    r = Convert.ToInt16(c.R);

    int g = 0;

    g = Convert.ToInt16(c.G);

    int b = 0;

    b = Convert.ToInt16(c.B);

    int max1 = System.Math.Max(r, g);

    int final = System.Math.Max(max1, b);

    r = final;

    g = final;

    b = final;

    c = Color.FromArgb(r, g, b);
    image1[count].SetPixel(i, j, c);
    // image1.SetPixel(r, g, b);

    /*image1[count].SetPixel(i, j, c);
    pictureBox1.Image = image1[count];
    aviStream.AddFrame(image1[count]);
    image1[count].Dispose();
    label1.Text = "Pixel format: " + image1[count].PixelFormat.ToString();
    aviManager.Close();*/
    }

    }
    m = m + 200;

    // aviStream = aviManager.AddVideoStream(true, 1, image1[count]);
    aviStream.AddFrame(image1[count]);
    pictureBox1.Image = image1[count];
    //aviStream.AddFrame(image1[count]);

    image1[count].Dispose();
    label1.Text = "Pixel format: " + image1[count].PixelFormat.ToString();

    // aviManager.Close();
    }
    // Dispose();
    aviManager.Close();

    //aviManager.Close();
    // Set the PictureBox to display the image.
    // pictureBox1.Image = image1[i];
    //aviStream.AddFrame( image1);
    //image1.Dispose();
    //aviManager.Close();
    // Display the pixel format in Label1.
    //label1.Text = "Pixel format: " + image1[i].PixelFormat.ToString();
    }
    catch (ArgumentException)
    {
    MessageBox.Show("There was an error." +
    "Check the path to the image file.");
    }

    }

    }
    }
    the code works correctly but it throws an exception in program.cs file.
    Application.Run(new Form1()); exception was unhandled .
    can you help me out of this error.
    thanks in advance

  5. chakravarthy says:

    Avimanager shows an error (Assemblyrefrence is missing)

  6. chakravarthy says:

    aviStream = aviManager.AddVideoStream(true, 4, bi);
    from here form displays then not goes to next line
    if i put false instead of true its working but video is not running

  7. chakravarthy says:

    how to decrease the memory i mean for 20 sec memory 400 mb how to decrease this memory for 1 min 1mb

  8. chakravarthy says:

    Windows Media Player cannot play the file. The Player might not support the file type or might not support the codec that was used to compress the file.(some times working)
    what to do
    i need to change the player

  9. chakravarthy says:

    Exception in VideoStreamWrite: -2147205019

  10. chakravarthy says:

    why this code is not working in timer Elapsed

  11. Thanx nice app……….

  12. pStudio says:

    Thanks for the sample project. It is a good one to help me a lot 🙂

  13. Geoff says:

    Hi, Your link to your source code is down, can you please re-upload it?

  14. chandrasekhar says:

    Please upload your code once again links are expired

  15. nagendrareddy says:

    Hi Kishore Iam Very New To these C#.net I have To generate screen recorder like a video i have a start ,stop ,play. buttons if i presssed statrt start recording of screen when i pres stop stop the recording when i press play it just play the video of recording screen. i hope you undersatnd my requirement .could you please send me the source code my mail id nagendra.re30@gmail.com thanq in advance iam waiting for rply

  16. stealthyloon says:

    The full source code link goes to a blank page and never loads?

  17. Ratatulli says:

    Where do i find this Avi.dll?
    The given website only contains a download link with many .cs files.
    I pasted them all into my project, but when i try to run it, and i press button1 it says in AviManager.cs:

    Exception was not handled
    Exception in AVIFileOpen: -2147205009

  18. Pingback: Record video from opengl | PHP Developer Resource

  19. Anonymous says:

    This file does not exist, the access to the following file is limited or it has been removed due to infringement of copyright. -> how could i see the full source code?? please help me…

  20. Anonymous says:

    avimanager is a joke, it is a memory pig, just use ffmeg it cuts the dozens of lines of code down to a single one.

  21. Anonymous says:

    hello.. I want to record video with using webcam. i should use c# . how i am do that?

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s