using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
public static class VoiceBotScript
{
public static void Run(IntPtr windowHandle)
{
// Here you set the path for saving screenshots
string path = (@"C:\ScreenShots\");
// Here you may change image format (Png, Jpeg, Bmp, Tiff, Gif, Icon, Wmf, Emf, Exif)
ImageFormat ext = ImageFormat.Png;
// If directory not exists call a method CreateDir
if (!Directory.Exists(path))
{
Dir.CreateDir(path);
}
// Call a method Capture
Capture.GetCapture(path,ext);
}
}
static class Dir
{
public static void CreateDir(string path)
{
// Create directory if sufficient permissons.
try
{
Directory.CreateDirectory(path);
// Get only name of directory
string foldername = Path.GetFileName(Path.GetDirectoryName(path));
BFS.Speech.TextToSpeech("Create directory" + foldername);
}
// Permission exception
catch (UnauthorizedAccessException)
{
BFS.Speech.TextToSpeech("Insufficient permissions. Can't create the directory");
}
}
}
static class Capture
{
private static string name, time, extension, filename;
public static void GetCapture(string path, ImageFormat ext)
{
// Get and save fullscreen image
Image imgFullScr = ScreenCapture.CaptureDesktop();
GetFileName(path, ext);
imgFullScr.Save(@filename, ext);
}
private static void GetFileName(string path, ImageFormat ext)
{
// Get handle focused window
IntPtr hWnd = BFS.Window.GetFocusedWindow();
// Get window name from hanle
name = BFS.Window.GetText(hWnd);
// Convert current time and date to string
time = DateTime.Now.ToString("(yyyy-MM-dd@HH.mm.ss)");
// Convert imageformat to string and to lowercase
extension = ext.ToString().ToLower();
// Set formatting for saving file
filename = (path + name + " " + time + "." + extension);
}
}
static class ScreenCapture
{
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport(@"dwmapi.dll")]
private static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, out Rect pvAttribute, int cbAttribute);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetDesktopWindow();
[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;
}
public static Image CaptureDesktop()
{
return CaptureWindow(GetDesktopWindow());
}
public static Bitmap CaptureActiveWindow()
{
return CaptureWindow(GetForegroundWindow());
}
// Capture desktop or active window
// and return it as a bitmap
public static Bitmap CaptureWindow(IntPtr hWnd)
{
var rect = new Rect();
// If Win XP and earlier use the old way
if (Environment.OSVersion.Version.Major < 6)
GetWindowRect(hWnd, ref rect);
// using DwmApi for border rectangle
else
{
var res = -1;
try
{
res = DwmGetWindowAttribute(hWnd, 9, out rect, Marshal.SizeOf(typeof(Rect)));
}
catch {}
if (res < 0)
GetWindowRect(hWnd, ref rect);
}
var bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
var result = new Bitmap(bounds.Width, bounds.Height);
using (Graphics graphics = Graphics.FromImage(result))
{
graphics.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
}
return result;
}
}