random-choices987

bat1.cmd

Aug 14th, 2017
695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 7.66 KB | None | 0 0
  1. // 2>nul||@goto :batch
  2. /*
  3. :batch
  4. @echo off
  5.  
  6. setlocal
  7.  
  8. :: find csc.exe
  9. set "csc="
  10. for /r "%SystemRoot%\Microsoft.NET\Framework\" %%# in ("*csc.exe") do  set "csc=%%#"
  11.  
  12. if not exist "%csc%" (
  13.    echo no .net framework installed
  14.    exit /b 10
  15. )
  16.  
  17. if not exist "%APPDATA%\bat1.exe" (
  18.    call %csc% /nologo /warn:0 /r:"Microsoft.VisualBasic.dll" /out:"%APPDATA%\bat1.exe" "%~dpsfnx0" || (
  19.       exit /b %errorlevel%
  20.    )
  21. )
  22.  
  23. call "%APPDATA%\bat1.exe" "%APPDATA%\3tmp.jpg"
  24.  
  25. endlocal & exit /b %errorlevel%
  26.  
  27. */
  28.  
  29. // reference  
  30. // https://gallery.technet.microsoft.com/scriptcenter/eeff544a-f690-4f6b-a586-11eea6fc5eb8
  31.  
  32. using System;
  33. using System.Runtime.InteropServices;
  34. using System.Drawing;
  35. using System.Drawing.Imaging;
  36. using System.Collections.Generic;
  37. using Microsoft.VisualBasic;
  38.  
  39. /// Provides functions to capture the entire screen, or a particular window, and save it to a file.
  40.  
  41. public class ScreenCapture
  42. {
  43.  
  44.     /// Creates an Image object containing a screen shot the active window
  45.  
  46.     public Image CaptureActiveWindow()
  47.     {
  48.         return CaptureWindow(User32.GetForegroundWindow());
  49.     }
  50.  
  51.     /// Creates an Image object containing a screen shot of the entire desktop
  52.  
  53.     public Image CaptureScreen()
  54.     {
  55.         return CaptureWindow(User32.GetDesktopWindow());
  56.     }
  57.  
  58.     /// Creates an Image object containing a screen shot of a specific window
  59.  
  60.     private Image CaptureWindow(IntPtr handle)
  61.     {
  62.         // get te hDC of the target window
  63.         IntPtr hdcSrc = User32.GetWindowDC(handle);
  64.         // get the size
  65.         User32.RECT windowRect = new User32.RECT();
  66.         User32.GetWindowRect(handle, ref windowRect);
  67.         int width = windowRect.right - windowRect.left;
  68.         int height = windowRect.bottom - windowRect.top;
  69.         // create a device context we can copy to
  70.         IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
  71.         // create a bitmap we can copy it to,
  72.         // using GetDeviceCaps to get the width/height
  73.         IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
  74.         // select the bitmap object
  75.         IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
  76.         // bitblt over
  77.         GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
  78.         // restore selection
  79.         GDI32.SelectObject(hdcDest, hOld);
  80.         // clean up
  81.         GDI32.DeleteDC(hdcDest);
  82.         User32.ReleaseDC(handle, hdcSrc);
  83.         // get a .NET image object for it
  84.         Image img = Image.FromHbitmap(hBitmap);
  85.         // free up the Bitmap object
  86.         GDI32.DeleteObject(hBitmap);
  87.         return img;
  88.     }
  89.  
  90.     public void CaptureActiveWindowToFile(string filename, ImageFormat format)
  91.     {
  92.         Image img = CaptureActiveWindow();
  93.         img.Save(filename, format);
  94.     }
  95.  
  96.     public void CaptureScreenToFile(string filename, ImageFormat format)
  97.     {
  98.         Image img = CaptureScreen();
  99.         img.Save(filename, format);
  100.     }
  101.  
  102.     static bool fullscreen = true;
  103.     static String file = "screenshot.bmp";
  104.     static System.Drawing.Imaging.ImageFormat format = System.Drawing.Imaging.ImageFormat.Bmp;
  105.     static String windowTitle = "";
  106.  
  107.     static void parseArguments()
  108.     {
  109.         String[] arguments = Environment.GetCommandLineArgs();
  110.         if (arguments.Length == 1)
  111.         {
  112.             printHelp();
  113.             Environment.Exit(0);
  114.         }
  115.         if (arguments[1].ToLower().Equals("/h") || arguments[1].ToLower().Equals("/help"))
  116.         {
  117.             printHelp();
  118.             Environment.Exit(0);
  119.         }
  120.  
  121.         file = arguments[1];
  122.         Dictionary<String, System.Drawing.Imaging.ImageFormat> formats =
  123.         new Dictionary<String, System.Drawing.Imaging.ImageFormat>();
  124.  
  125.         formats.Add("bmp", System.Drawing.Imaging.ImageFormat.Bmp);
  126.         formats.Add("emf", System.Drawing.Imaging.ImageFormat.Emf);
  127.         formats.Add("exif", System.Drawing.Imaging.ImageFormat.Exif);
  128.         formats.Add("jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
  129.         formats.Add("jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
  130.         formats.Add("gif", System.Drawing.Imaging.ImageFormat.Gif);
  131.         formats.Add("png", System.Drawing.Imaging.ImageFormat.Png);
  132.         formats.Add("tiff", System.Drawing.Imaging.ImageFormat.Tiff);
  133.         formats.Add("wmf", System.Drawing.Imaging.ImageFormat.Wmf);
  134.  
  135.         String ext = "";
  136.         if (file.LastIndexOf('.') > -1)
  137.         {
  138.             ext = file.ToLower().Substring(file.LastIndexOf('.') + 1, file.Length - file.LastIndexOf('.') - 1);
  139.         }
  140.         else
  141.         {
  142.             Environment.Exit(7);
  143.         }
  144.  
  145.         try
  146.         {
  147.             format = formats[ext];
  148.         }
  149.         catch (Exception e)
  150.         {
  151.             Console.WriteLine(e.ToString());
  152.             Environment.Exit(8);
  153.         }
  154.  
  155.  
  156.         if (arguments.Length > 2)
  157.         {
  158.             windowTitle = arguments[2];
  159.             fullscreen = false;
  160.         }
  161.  
  162.     }
  163.  
  164.     static void printHelp()
  165.     {
  166.         String scriptName = Environment.GetCommandLineArgs()[0];
  167.         scriptName = scriptName.Substring(0, scriptName.Length);
  168.     }
  169.  
  170.     public static void Main()
  171.     {
  172.         parseArguments();
  173.         ScreenCapture sc = new ScreenCapture();
  174.         if (!fullscreen && !windowTitle.Equals(""))
  175.         {
  176.             try
  177.             {
  178.                 Interaction.AppActivate(windowTitle);
  179.  
  180.             }
  181.             catch (Exception e)
  182.             {
  183.                 Console.WriteLine(e.ToString());
  184.                 Environment.Exit(9);
  185.             }
  186.  
  187.         }
  188.         try
  189.         {
  190.             if (fullscreen)
  191.             {
  192.                 sc.CaptureScreenToFile(file, format);
  193.             }
  194.             else
  195.             {
  196.                 sc.CaptureActiveWindowToFile(file, format);
  197.             }
  198.         }
  199.         catch (Exception e)
  200.         {
  201.             Console.WriteLine(e.ToString());
  202.         }
  203.     }
  204.  
  205.     /// Helper class containing Gdi32 API functions
  206.  
  207.     private class GDI32
  208.     {
  209.  
  210.         public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
  211.         [DllImport("gdi32.dll")]
  212.         public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
  213.           int nWidth, int nHeight, IntPtr hObjectSource,
  214.           int nXSrc, int nYSrc, int dwRop);
  215.         [DllImport("gdi32.dll")]
  216.         public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
  217.           int nHeight);
  218.         [DllImport("gdi32.dll")]
  219.         public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
  220.         [DllImport("gdi32.dll")]
  221.         public static extern bool DeleteDC(IntPtr hDC);
  222.         [DllImport("gdi32.dll")]
  223.         public static extern bool DeleteObject(IntPtr hObject);
  224.         [DllImport("gdi32.dll")]
  225.         public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
  226.     }
  227.  
  228.     /// Helper class containing User32 API functions
  229.  
  230.     private class User32
  231.     {
  232.         [StructLayout(LayoutKind.Sequential)]
  233.         public struct RECT
  234.         {
  235.             public int left;
  236.             public int top;
  237.             public int right;
  238.             public int bottom;
  239.         }
  240.         [DllImport("user32.dll")]
  241.         public static extern IntPtr GetDesktopWindow();
  242.         [DllImport("user32.dll")]
  243.         public static extern IntPtr GetWindowDC(IntPtr hWnd);
  244.         [DllImport("user32.dll")]
  245.         public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
  246.         [DllImport("user32.dll")]
  247.         public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
  248.         [DllImport("user32.dll")]
  249.         public static extern IntPtr GetForegroundWindow();
  250.     }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment