Click here to Skip to main content
15,888,454 members
Articles / Programming Languages / C#
Tip/Trick

Image Capture Desktop and Track User

Rate me:
Please Sign up or sign in to vote.
4.56/5 (7 votes)
21 Oct 2014MIT 19.4K   595   24   1
Snapshot desktop application and tracking

*Attention: When you run the program, save snapshot in the path program "...\snapshot_and_track_user\bin\Debug\image" and log save in "...\snapshot_and_track_user\bin\Debug\loge".

Image 1

Image 2

Introduction

This program may be used for hacking or something else. But I wrote it for tracking users who use my computer. Its snapshot image desktop application (not run time it takes events) saves title program running a compact image in output for sending an email and saves tracking user with time and name applications.

Background

It takes pictures when user runs the applications and saves it with a compact size with the user program.

Using the Code

Image 3

Start Up App and Run in Startup Windows

C#
private void Form1_Load(object sender, EventArgs e)
      {
          creat_folder_log("//log"); //check folder log
          creat_folder("//image");   //check folder image
          RegistryKey add = Registry.CurrentUser.OpenSubKey("SOFTWARE\\
          Microsoft\\Windows\\CurrentVersion\\Run", true); //add to Registry windows
          add.SetValue("vchimg", "\"" +
              Application.ExecutablePath.ToString() + "\"");
        // this.Opacity = 0;   // if you want hide form
         path_log = path_program_log + "\\logtex.txt"; // static way to get path app
      }

Header You Need For GetActiveWindowTitle

C#
WinEventDelegate dele = new WinEventDelegate(WinEventProc);
IntPtr m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND,
IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT);

//  WinEventDelegate dele = null;

delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd,
int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);

[DllImport("user32.dll")]
static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax,
IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess,
uint idThread, uint dwFlags);

private const uint WINEVENT_OUTOFCONTEXT = 0;
private const uint EVENT_SYSTEM_FOREGROUND = 3;

[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

[DllImport("user32.dll")]
private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);

[StructLayout(LayoutKind.Sequential)]
private struct Rect
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}

Void GetActiveWindowTitle With user32.dll

C#
public string GetActiveWindowTitle()
{
    const int nChars = 256;
    IntPtr handle = IntPtr.Zero;
    StringBuilder Buff = new StringBuilder(nChars);
    handle = GetForegroundWindow();

    if (GetWindowText(handle, Buff, nChars) > 0)
    {
        return Buff.ToString();
    }
    return null;
}

Void snapshot Imge and compress Jpg

C#
void  captcure_imge()
    {
       // set size bitmap
        Rectangle bounds;
        var foregroundWindowsHandle = GetForegroundWindow();
        var rect = new Rect();
        GetWindowRect(foregroundWindowsHandle, ref rect);
        bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left,
         rect.Bottom - rect.Top);
      // control error (not need tray and catch)
        if (bounds.Width != 0 || bounds.Height != 0)
        {

            var result = new Bitmap(bounds.Width, bounds.Height);
            //take screen form
            using (var g = Graphics.FromImage(result))
            {
                g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty
                , bounds.Size);
                //g.CopyFromScreen(new Point(w, h), Point.Empty, bounds.Size);
            }
            
            // creat_folder("//image");
            
            string name_file = path_program + "\\image" + m.ToString() + 
            "_" + DateTime.Now.Hour.ToString() + "_" + DateTime.Now.Minute.ToString()+
            "_"+DateTime.Now.Second.ToString() + "_day" + DateTime.Now.Day.ToString() +
            ".jpg";
            
            //result.Save(name_file, ImageFormat.Jpeg);
            
            //-----compress jpg
            // Bitmap bmp1 = new Bitmap(name_file);
            Bitmap bmp1 = new Bitmap(result);
            ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
            
            // Create an Encoder object based on the GUID 
            // for the Quality parameter category.
            System.Drawing.Imaging.Encoder myEncoder =System.Drawing.Imaging.Encoder.Quality;
            
            // Create an EncoderParameters object. 
            // An EncoderParameters object has an array of EncoderParameter 
            // objects. In this case, there is only one 
            // EncoderParameter object in the array.
            EncoderParameters myEncoderParameters = new EncoderParameters(1);
            EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 10L);
            myEncoderParameters.Param[0] = myEncoderParameter;
            
            bmp1.Save(name_file, jgpEncoder, myEncoderParameters);
           
            //--------one file
            // clear memory 24kb mem
            result.Dispose();
            bmp1.Dispose();
        }
    }
    
private ImageCodecInfo GetEncoder(ImageFormat format)
    {        
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
        
        foreach (ImageCodecInfo codec in codecs)
        {
            if (codec.FormatID == format.Guid)
            {
                return codec;
            }
        }
        return null;
    }

Create Folder Image And Log

C#
void creat_folder(string name_folder)
{
    string n = System.IO.Path.GetDirectoryName(Application.ExecutablePath.ToString()).
    ToString() + name_folder;
    if (Directory.Exists(n) == false)
    {
        System.IO.Directory.CreateDirectory(n);
    }
    path_program = n;
}

Main Void Run With EventHook. All void we need to run it in this function.

C#
public void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd,
int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
    // write log with time now
    StreamWriter log = new StreamWriter(path_log, true);
    log.WriteLine(GetActiveWindowTitle() + "=>Time:" + 
    DateTime.Now.ToLongTimeString());
    log.Close();
    // add ID for Image
    m++;
    // snapshot desktop
    captcure_imge();
}

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Program Manager none
Iran (Islamic Republic of) Iran (Islamic Republic of)
Programming is my Job!

unity
C#
php
Sql
Joomla
Jave
VB
Maxscript
CSS

Comments and Discussions

 
GeneralMy vote of 5 Pin
phpmajid majid24-Oct-14 1:14
phpmajid majid24-Oct-14 1:14 

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.