Click here to Skip to main content
15,880,796 members
Articles / Programming Languages / C#

Motion JPEG Streaming Server

Rate me:
Please Sign up or sign in to vote.
4.99/5 (69 votes)
23 Apr 2012CPOL3 min read 445.1K   44.5K   138   109
Implementing a MJPEG (or Motion JPEG) streaming server that can be used to steam screenshots or any other source of images over the HTTP protocol to any web browser client (MJPEG stream writer is included in the library)

For mobile clients:

Samsung Galaxy SII

Other clients (such as Firefox, Chrome and VLC player):

Streaming the desktop images to different remote clients

Introduction

Motion JPEG is a stream of JPG images over HTTP protocol and it is commonly used for many multimedia applications specially in digital cameras (IP cameras). Although there are many ways to send images as a video to any remote computer, MJPEG is the preferred one since normally it does not need any client software to be installed on the remote computer! That is because it is supported by most of the common web browsers like Firefox and Chrome but unfortunately, it is not yet supported by Internet Explorer directly (but there are some plug-ins that make it possible to work with Internet Explorer).

Motivation

In many, I needed to stream a video of my desktop, camera, or whatever to a remote computer which has no client software to see my stream... In such a case, using Motion-JPEG streams is preferred because it needs no more than an internet browser. In this article, I'm not going to describe the internal structure of an MJPEG stream (for more information, please visit Motion_JPEG). Instead of that, I'm going to describe how to use this library for MJPEG streaming.

How to Use This Library

The main class in our library is ImageSreamingSever() which contains the following methods and properties:

Methods

  • void Start(): Starts the server on a specified TCP port
  • void Stop(): Stops the server and closes any open connections

Properties

  • ImagesSource: The images collection (IEnumerable<Image>) to be streamed to any connected client
  • Interval: The delay time (in milliseconds) between each sending image from the ImagesSource collection
  • IsRunning: Is a read-only bool property indicating the status of the server
  • Clients: Returns a read-only collection of the currently connected socket of each client

Example

In order to implement a streaming server that can send a video of your desktop screen to any connected client, you'll need only two lines of code using our ImageStreamingServer, as follows:

C#
ImageStreamingServer server = new ImageStreamingServer(); 
server.Start(8080);

That's it !!!

Now if you open (for example) the Chrome Internet Explorer and write the IP of the server followed by colon and then the port number, in our example, it is something like this...

http://127.0.0.1:8080

...you will properly see a video of your computer screen in the explorer... and same if you do this from another computer (with the correct IP of your server), you should see something like this:

Google Chrome

Implementing Your Own ImagesSource

Actually, I've chosen IEnumerable<Image> as a source of images because of the flexibility that this interface is providing, for example, the default ImagesSource property of the class ImageStreamingServer is an IEnumerable<Image> of the desktop screen, but at the same time, you are free to implement any other type of image source you like by changing the value of that property.

Suppose you want to make the images source from the camera or from the images file of any folder in your computer, then your code should be something like this:

C#
IEnumerable<Image> CameraImagesSource() 
{ 
    // Your code must be here 
} 

IEnumerable<Image> FolderImagesSource(string path) 
{ 
    // Your code must be here 
}

As a real example, let's implement a source of images that streams all "*.jpg" photos from a specified folder path as follows:

C#
private IEnumerable<Image> FolderImagesSource(string path)
{
    var files = System.IO.Directory.GetFiles(path, "*.jpg");

    foreach (var file in files)
        yield return Image.FromFile(file);

    yield break;
}

private void Form1_Load(object sender, EventArgs e)
{
    var source = FolderImagesSource(@"C:\Pictures\");

    _Server = new ImageStreamingServer();
    _Server.ImagesSource = source;
    _Server.Start(8080);
}

Note: The above code can be shortened as follows (thanks to IEnumerable extensions and LINQ):

C#
var source = System.IO.Directory.GetFiles(@"C:\Pictures\", "*.jpg").Select(s => Image.FromFile(s));

_Server = new ImageStreamingServer(source);
_Server.Start(8080);

Supported Client Software

To see the streaming video of our server, you will need to use any software that supports Motion-JPEG streaming such as a Mozilla Firefox, Google's Chrome or VideoLAN Player (VLC):

Free client software for Windows PC

And finally, for your mobile (Android), use FireFox or any other free app that can be found on Google's market such as tinyCamMonitor (that was used in the picture below):

Samsung Galaxy SII

Future Versions

In the future versions of the library, I'm going to add a variety of image sources such as:

  • Images from a specific region of the screen
  • Images from a specific window in the system
  • Streaming images from a camera
  • All photos from any folder in a disk
  • And any other suggested sources of images you may want !? ...

And till that time, feel free to enjoy this version. ;-)

History

  • 23rd April, 2012: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Hawk International for Finance & Construction Ltd.
Yemen Yemen
Bachelor degree of Computer Sciences, Software developer using .NET (Visual Basic & C#).
I like the development of an Artificial Intelligence Systems, GPS Applications, GDI+, APIs, N-Tier Applications, Distributed Systems and Network Monitoring Systems.
Mobile Development (Windows Mobile & Android).

Comments and Discussions

 
QuestionHow can i do same scenario for audio i have tried multiple solution using your api . could you please help me how can i do the same audio . Pin
Member 1360243528-Oct-19 7:16
Member 1360243528-Oct-19 7:16 
Questioncan we Convert it to web form Pin
mariamg10-Sep-19 1:26
mariamg10-Sep-19 1:26 
QuestionHow to restrict the number of client Pin
Member 1453849324-Jul-19 8:00
Member 1453849324-Jul-19 8:00 
AnswerRe: How to restrict the number of client Pin
Member 125495874-Apr-24 1:01
Member 125495874-Apr-24 1:01 
Question5 star Pin
Predator7528-Feb-19 23:15
professionalPredator7528-Feb-19 23:15 
QuestionExcellent project, thank you for sharing Pin
Member 1412120917-Jan-19 23:08
Member 1412120917-Jan-19 23:08 
QuestionIn your example is worked..but output is loop format (i.e) inside of window another window shows. How to solve this problem? Pin
Member 140717732-Dec-18 21:16
Member 140717732-Dec-18 21:16 
GeneralThank U very much! Pin
Member 1396686529-Aug-18 22:34
Member 1396686529-Aug-18 22:34 
QuestionOnly works on LAN, not working over internet ? Pin
Member 114163041-Jun-18 20:35
professionalMember 114163041-Jun-18 20:35 
QuestionInfinite loop of same image Pin
Member 1161590225-Apr-18 4:02
Member 1161590225-Apr-18 4:02 
QuestionHow to connect client and server in Remote desktop over the internet ? Pin
Member 1359412324-Mar-18 19:28
Member 1359412324-Mar-18 19:28 
How to connect client and server  in  Remote desktop over the  internet ?

PraiseGOOD JOB Pin
Vladimir J Arias L12-Oct-17 18:14
Vladimir J Arias L12-Oct-17 18:14 
QuestionFantastic work! Pin
Fumetsuj04-Jun-17 3:08
professionalFumetsuj04-Jun-17 3:08 
QuestionUnable to Stop the the process, how to stop it ? Pin
Shemeemsha (ഷെമീംഷ)18-Mar-17 0:53
Shemeemsha (ഷെമീംഷ)18-Mar-17 0:53 
QuestionDelay Pin
Member 111897539-Mar-17 2:07
Member 111897539-Mar-17 2:07 
Questionrtsp Pin
Member 130416426-Mar-17 7:11
Member 130416426-Mar-17 7:11 
AnswerRe: rtsp Pin
p3im4n13-Nov-20 5:02
p3im4n13-Nov-20 5:02 
QuestionStop ImageStreamingServer Pin
ibrar aslam11-Jan-17 22:44
ibrar aslam11-Jan-17 22:44 
QuestionWicked smaht! Pin
Guad17-Dec-16 4:29
Guad17-Dec-16 4:29 
QuestionAudio streaming? Pin
Member 925474831-May-16 11:11
Member 925474831-May-16 11:11 
QuestionCan we implement this in c# web application? Pin
Member 108768699-Mar-16 20:13
Member 108768699-Mar-16 20:13 
QuestionCan we implement this in c# web application? Pin
Member 108768699-Mar-16 18:32
Member 108768699-Mar-16 18:32 
PraiseNot bad at all Pin
ControlBreak15-Feb-16 11:04
ControlBreak15-Feb-16 11:04 
Questionstreaming video received from other device Pin
sujataa17-Jan-16 9:19
sujataa17-Jan-16 9:19 
QuestionI can't use server.stop Pin
Member 1051975611-Nov-15 21:56
professionalMember 1051975611-Nov-15 21:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.