5 Comments
  •   Posted in: 
  • C#

At the office we have a Conceptronic CnetCam2. I have configured this IP Webcam to log every x seconds to an FTP Server. But now I would like to view the taken snapshot images in my web-application. The webcam makes this directory structure:

filezilla

So every day is a new folder and has the format of ‘hhMMss_x.jpg’ (h = hour, M = minutes, s = seconds, x= random?) So my Asp.Net web application has to connect through FTP to extract the latest jpg and display it. I have found this class on CodeProject for setting up an FTP connection. I have uploaded it to this blog as a mirror FtpClientCS.zip (6.62 kb) All credits for that class go to Howard Richards. So after I have added the C# class to the project I have made the following code. [more] Which is awesome because of the IEnumerable interface.

private void GetScreenCap(string hostname, string username, string password)
{
    try
    {
        FTPclient fc        = new FTPclient(hostname, username, password);
        FTPdirectory dir    = fc.ListDirectoryDetail("/cam/");
        FTPfileInfo dirLast = dir.GetDirectories().Last();
        FTPfileInfo fi      = fc.ListDirectoryDetail(dirLast.FullName + "/").GetFiles("jpg").Last();

        fc.Download(fi.FullName, Server.MapPath("~/tmpwrite/" + fi.Filename), true);

        Image img = new Image();
        string alt= "Webcam image: " + fi.Filename.Replace(".jpg", "");
        img.AlternateText = alt;
        img.ImageUrl = "~/tmpwrite/" + fi.Filename;
        ph.Controls.Add(img);
    }
    catch (Exception ex)
    {
        Literal li = new Literal();
        li.Text = ex.Message + "<br/>" + ex.StackTrace;
        ph.Controls.Add(li);
    }
}
 

As you can see, is the .Last method very handy to get the latest file from the latest directory. The rest is just boring stuff to save the file to the disk and make an Image object and point to the download location.

So use & share etc. Good luck! Open-mouthed smile

kick it on DotNetKicks.com Shout it

Post on LinkedIn