{"id":339,"date":"2022-02-26T12:09:06","date_gmt":"2022-02-26T12:09:06","guid":{"rendered":"https:\/\/xuan.guru\/?p=339"},"modified":"2022-02-26T12:09:08","modified_gmt":"2022-02-26T12:09:08","slug":"take-a-screenshot-via-command-line","status":"publish","type":"post","link":"https:\/\/xuan.co.uk\/?p=339","title":{"rendered":"Take a screenshot via command line"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/npocmaka\/batch.scripts\/blob\/master\/hybrids\/.net\/c\/screenCapture.bat\">https:\/\/github.com\/npocmaka\/batch.scripts\/blob\/master\/hybrids\/.net\/c\/screenCapture.bat<\/a><\/p>\n\n\n\n<p>This script will make an exe that can take a screenshot of the screen using command line<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\/\/ 2>nul||@goto :batch<br>\/*<br>:batch<br>@echo off<br>setlocal<\/p>\n\n\n\n<p>:: find csc.exe<br>set &#8220;csc=&#8221;<br>for \/r &#8220;%SystemRoot%\\Microsoft.NET\\Framework\\&#8221; %%# in (&#8220;*csc.exe&#8221;) do set &#8220;csc=%%#&#8221;<\/p>\n\n\n\n<p>if not exist &#8220;%csc%&#8221; (<br>echo no .net framework installed<br>exit \/b 10<br>)<\/p>\n\n\n\n<p>if not exist &#8220;%~n0.exe&#8221; (<br>call %csc% \/nologo \/r:&#8221;Microsoft.VisualBasic.dll&#8221; \/out:&#8221;%~n0.exe&#8221; &#8220;%~dpsfnx0&#8221; || (<br>exit \/b %errorlevel%<br>)<br>)<br>%~n0.exe %*<br>endlocal &amp; exit \/b %errorlevel%<\/p>\n\n\n\n<p>*\/<\/p>\n\n\n\n<p>\/\/ reference<br>\/\/ https:\/\/gallery.technet.microsoft.com\/scriptcenter\/eeff544a-f690-4f6b-a586-11eea6fc5eb8<\/p>\n\n\n\n<p>using System;<br>using System.Runtime.InteropServices;<br>using System.Drawing;<br>using System.Drawing.Imaging;<br>using System.Collections.Generic;<br>using Microsoft.VisualBasic;<\/p>\n\n\n\n<p>\/\/\/ Provides functions to capture the entire screen, or a particular window, and save it to a file.<\/p>\n\n\n\n<p>public class ScreenCapture<br>{<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\/ Creates an Image object containing a screen shot the active window \n\npublic Image CaptureActiveWindow()\n{\n    return CaptureWindow(User32.GetForegroundWindow());\n}\n\n\/\/\/ Creates an Image object containing a screen shot of the entire desktop \n\npublic Image CaptureScreen()\n{\n    return CaptureWindow(User32.GetDesktopWindow());\n}\n\n\/\/\/ Creates an Image object containing a screen shot of a specific window \n\nprivate Image CaptureWindow(IntPtr handle)\n{\n    \/\/ get te hDC of the target window \n    IntPtr hdcSrc = User32.GetWindowDC(handle);\n    \/\/ get the size \n    User32.RECT windowRect = new User32.RECT();\n    User32.GetWindowRect(handle, ref windowRect);\n    int width = windowRect.right - windowRect.left;\n    int height = windowRect.bottom - windowRect.top;\n    \/\/ create a device context we can copy to \n    IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);\n    \/\/ create a bitmap we can copy it to, \n    \/\/ using GetDeviceCaps to get the width\/height \n    IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);\n    \/\/ select the bitmap object \n    IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);\n    \/\/ bitblt over \n    GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);\n    \/\/ restore selection \n    GDI32.SelectObject(hdcDest, hOld);\n    \/\/ clean up \n    GDI32.DeleteDC(hdcDest);\n    User32.ReleaseDC(handle, hdcSrc);\n    \/\/ get a .NET image object for it \n    Image img = Image.FromHbitmap(hBitmap);\n    \/\/ free up the Bitmap object \n    GDI32.DeleteObject(hBitmap);\n    return img;\n}\n\npublic void CaptureActiveWindowToFile(string filename, ImageFormat format)\n{\n    Image img = CaptureActiveWindow();\n    img.Save(filename, format);\n}\n\npublic void CaptureScreenToFile(string filename, ImageFormat format)\n{\n    Image img = CaptureScreen();\n    img.Save(filename, format);\n}\n\nstatic bool fullscreen = true;\nstatic String file = \"screenshot.bmp\";\nstatic System.Drawing.Imaging.ImageFormat format = System.Drawing.Imaging.ImageFormat.Bmp;\nstatic String windowTitle = \"\";\n\nstatic void parseArguments()\n{\n    String&#91;] arguments = Environment.GetCommandLineArgs();\n    if (arguments.Length == 1)\n    {\n        printHelp();\n        Environment.Exit(0);\n    }\n    if (arguments&#91;1].ToLower().Equals(\"\/h\") || arguments&#91;1].ToLower().Equals(\"\/help\"))\n    {\n        printHelp();\n        Environment.Exit(0);\n    }\n\n    file = arguments&#91;1];\n    Dictionary&lt;String, System.Drawing.Imaging.ImageFormat&gt; formats =\n    new Dictionary&lt;String, System.Drawing.Imaging.ImageFormat&gt;();\n\n    formats.Add(\"bmp\", System.Drawing.Imaging.ImageFormat.Bmp);\n    formats.Add(\"emf\", System.Drawing.Imaging.ImageFormat.Emf);\n    formats.Add(\"exif\", System.Drawing.Imaging.ImageFormat.Exif);\n    formats.Add(\"jpg\", System.Drawing.Imaging.ImageFormat.Jpeg);\n    formats.Add(\"jpeg\", System.Drawing.Imaging.ImageFormat.Jpeg);\n    formats.Add(\"gif\", System.Drawing.Imaging.ImageFormat.Gif);\n    formats.Add(\"png\", System.Drawing.Imaging.ImageFormat.Png);\n    formats.Add(\"tiff\", System.Drawing.Imaging.ImageFormat.Tiff);\n    formats.Add(\"wmf\", System.Drawing.Imaging.ImageFormat.Wmf);\n\n\n    String ext = \"\";\n    if (file.LastIndexOf('.') &gt; -1)\n    {\n        ext = file.ToLower().Substring(file.LastIndexOf('.') + 1, file.Length - file.LastIndexOf('.') - 1);\n    }\n    else\n    {\n        Console.WriteLine(\"Invalid file name - no extension\");\n        Environment.Exit(7);\n    }\n\n    try\n    {\n        format = formats&#91;ext];\n    }\n    catch (Exception e)\n    {\n        Console.WriteLine(\"Probably wrong file format:\" + ext);\n        Console.WriteLine(e.ToString());\n        Environment.Exit(8);\n    }\n\n\n    if (arguments.Length &gt; 2)\n    {\n        windowTitle = arguments&#91;2];\n        fullscreen = false;\n    }\n\n}\n\nstatic void printHelp()\n{\n    \/\/clears the extension from the script name\n    String scriptName = Environment.GetCommandLineArgs()&#91;0];\n    scriptName = scriptName.Substring(0, scriptName.Length);\n    Console.WriteLine(scriptName + \" captures the screen or the active window and saves it to a file.\");\n    Console.WriteLine(\"\");\n    Console.WriteLine(\"Usage:\");\n    Console.WriteLine(\" \" + scriptName + \" filename  &#91;WindowTitle]\");\n    Console.WriteLine(\"\");\n    Console.WriteLine(\"filename - the file where the screen capture will be saved\");\n    Console.WriteLine(\"     allowed file extensions are - Bmp,Emf,Exif,Gif,Icon,Jpeg,Png,Tiff,Wmf.\");\n    Console.WriteLine(\"WindowTitle - instead of capture whole screen you can point to a window \");\n    Console.WriteLine(\"     with a title which will put on focus and captuted.\");\n    Console.WriteLine(\"     For WindowTitle you can pass only the first few characters.\");\n    Console.WriteLine(\"     If don't want to change the current active window pass only \\\"\\\"\");\n}\n\npublic static void Main()\n{\n    User32.SetProcessDPIAware();\n\n    parseArguments();\n    ScreenCapture sc = new ScreenCapture();\n    if (!fullscreen &amp;&amp; !windowTitle.Equals(\"\"))\n    {\n        try\n        {\n\n            Interaction.AppActivate(windowTitle);\n            Console.WriteLine(\"setting \" + windowTitle + \" on focus\");\n        }\n        catch (Exception e)\n        {\n            Console.WriteLine(\"Probably there's no window like \" + windowTitle);\n            Console.WriteLine(e.ToString());\n            Environment.Exit(9);\n        }\n\n\n    }\n    try\n    {\n        if (fullscreen)\n        {\n            Console.WriteLine(\"Taking a capture of the whole screen to \" + file);\n            sc.CaptureScreenToFile(file, format);\n        }\n        else\n        {\n            Console.WriteLine(\"Taking a capture of the active window to \" + file);\n            sc.CaptureActiveWindowToFile(file, format);\n        }\n    }\n    catch (Exception e)\n    {\n        Console.WriteLine(\"Check if file path is valid \" + file);\n        Console.WriteLine(e.ToString());\n    }\n}\n\n\/\/\/ Helper class containing Gdi32 API functions \n\nprivate class GDI32\n{\n\n    public const int SRCCOPY = 0x00CC0020; \/\/ BitBlt dwRop parameter \n    &#91;DllImport(\"gdi32.dll\")]\n    public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,\n      int nWidth, int nHeight, IntPtr hObjectSource,\n      int nXSrc, int nYSrc, int dwRop);\n    &#91;DllImport(\"gdi32.dll\")]\n    public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,\n      int nHeight);\n    &#91;DllImport(\"gdi32.dll\")]\n    public static extern IntPtr CreateCompatibleDC(IntPtr hDC);\n    &#91;DllImport(\"gdi32.dll\")]\n    public static extern bool DeleteDC(IntPtr hDC);\n    &#91;DllImport(\"gdi32.dll\")]\n    public static extern bool DeleteObject(IntPtr hObject);\n    &#91;DllImport(\"gdi32.dll\")]\n    public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);\n}\n\n\n\/\/\/ Helper class containing User32 API functions \n\nprivate class User32\n{\n    &#91;StructLayout(LayoutKind.Sequential)]\n    public struct RECT\n    {\n        public int left;\n        public int top;\n        public int right;\n        public int bottom;\n    }\n    &#91;DllImport(\"user32.dll\")]\n    public static extern IntPtr GetDesktopWindow();\n    &#91;DllImport(\"user32.dll\")]\n    public static extern IntPtr GetWindowDC(IntPtr hWnd);\n    &#91;DllImport(\"user32.dll\")]\n    public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);\n    &#91;DllImport(\"user32.dll\")]\n    public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);\n    &#91;DllImport(\"user32.dll\")]\n    public static extern IntPtr GetForegroundWindow();\n    &#91;DllImport(\"user32.dll\")]\n    public static extern int SetProcessDPIAware();\n}<\/code><\/pre>\n\n\n\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/github.com\/npocmaka\/batch.scripts\/blob\/master\/hybrids\/.net\/c\/screenCapture.bat This script will make an exe that can take a screenshot of the screen using command line \/\/ 2>nul||@goto :batch\/*:batch@echo offsetlocal :: find csc.exeset &#8220;csc=&#8221;for \/r &#8220;%SystemRoot%\\Microsoft.NET\\Framework\\&#8221; %%# in (&#8220;*csc.exe&#8221;) do set &#8220;csc=%%#&#8221; if not exist &#8220;%csc%&#8221; (echo no .net framework installedexit \/b 10) if not exist &#8220;%~n0.exe&#8221; (call <a class=\"more-link\" href=\"https:\/\/xuan.co.uk\/?p=339\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[73,74,71,72],"class_list":["post-339","post","type-post","status-publish","format-standard","hentry","category-tech-tips","tag-command-line","tag-dos","tag-screen-capture","tag-screenshot"],"_links":{"self":[{"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=339"}],"version-history":[{"count":1,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/339\/revisions"}],"predecessor-version":[{"id":340,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/339\/revisions\/340"}],"wp:attachment":[{"href":"https:\/\/xuan.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}