Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Image Capture Desktop and Track User

0.00/5 (No votes)
21 Oct 2014 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".

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

Start Up App and Run in Startup Windows

  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

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

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

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

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.

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here