screenshot of CMD application

Rav

New member
Joined
Jun 19, 2024
Messages
4
Programming Experience
1-3
Hi,

I am struggling with a thing in c#. I would like to open cmd application, perform two actions:
.StartInfo.Arguments = @"/C echo %date%-%time%";
.Arguments = @"/c netsh wlan show interfaces";
and than create a screenshot of cmd application. But only cmd application.
It is not a problem to perform mentioned actions but unfortunately I cannot force c# to choose correct screen, position and size to perform a screenshot.
I tried to find position and size of cmd (didn't work, only get some random numbers) or resize and change position of cmd (...also didn't work). Can you please help me with that? Perhaps provide me with simple, working example
 
Show us your code for determine the position, size, and shape of the CMD window.

As aside, why do you need a screenshot? At best, you can just call the underlying APIs that those commands run and get the data directly. At next best is you can redirect the output of the commands into a text file. A screenshot seems to be the most memory intensive solution, which will also result in needing to come up with a way to eventually pull the data out of the screenshot.
 
Moved thread to "Graphics/GDI+" subforum...
 
Skydiver - normally I do save text file. Screenshot have to be done only in some of cases as a proof.

my function:

CaptureCMD function:
 static void CaptureCMD(Process proc)
    {
        string path = AppDomain.CurrentDomain.BaseDirectory;
        var rect = new User32.Rect();
        User32.GetWindowRect(proc.MainWindowHandle, ref rect);

        int width = Console.WindowWidth; //rect.right - rect.left;
        int height = Console.WindowHeight; //rect.bottom - rect.top;
        width = Console.LargestWindowWidth;
        height = Console.LargestWindowHeight;
        //Console.SetWindowSize(200, 40);     //...not working



        Console.WriteLine(width);
        Console.WriteLine(height);
        var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
        using (Graphics graphics = Graphics.FromImage(bmp))
        {
            graphics.CopyFromScreen(rect.left, rect.top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);
        }

        bmp.Save(path + "\\test.png", ImageFormat.Png);
    }
 
Try passing the "WindowsTerminal" process, and use width/height from GetWindowRect rect.
 
I used cmd.exe, not WindowsTerminal and I passed the process.
Btw, I cannot find width/height in rect so I tried right - left and bottom - top
As I can see all of the values are 0



Main::
static void Main(string[] args)
{
    string path = AppDomain.CurrentDomain.BaseDirectory;
    
    Process p = new Process();

    p.StartInfo.FileName = "cmd.exe";
    p.StartInfo.Arguments = @"/C echo  %date%-%time%";

    p.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
    p.StartInfo.CreateNoWindow = false;
    p.StartInfo.UseShellExecute = false;


    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.Start();

    string branchName = p.StandardOutput.ReadToEnd().TrimEnd();
    string errorInfoIfAny = p.StandardError.ReadToEnd().TrimEnd();
    Console.WriteLine(@"COMMAND: echo  %date%-%time%");
    Console.WriteLine(branchName);

    p.StartInfo.Arguments = @"/c netsh wlan show interfaces";
    p.Start();

     branchName = p.StandardOutput.ReadToEnd().TrimEnd();
     errorInfoIfAny = p.StandardError.ReadToEnd().TrimEnd();
    Console.WriteLine();
    Console.WriteLine(@"COMMAND: netsh wlan show interfaces");
    Console.WriteLine(branchName);

    CaptureCMD(p);
}


Modified CaptureCMD function:
static void CaptureCMD(Process proc)
   {
       string path = AppDomain.CurrentDomain.BaseDirectory;
       var rect = new User32.Rect();
       User32.GetWindowRect(proc.MainWindowHandle, ref rect);
       //User32.GetWindowRect(proc.MainWindowHandle, ref rect);

       int width = Console.WindowWidth; //rect.right - rect.left;
       int height = Console.WindowHeight; //rect.bottom - rect.top;
       width = Console.LargestWindowWidth;
       height = Console.LargestWindowHeight;
       Console.SetWindowSize(200, 40);

           //GetWindowRect solution:
           int width2 = rect.right - rect.left;
           int height2 = rect.bottom - rect.top;


        Console.WriteLine(width);
        Console.WriteLine(height);
        var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
        using (Graphics graphics = Graphics.FromImage(bmp))
        {
            graphics.CopyFromScreen(rect.left, rect.top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);
        }

        bmp.Save(path + "\\test.png", ImageFormat.Png);

}
 
There is no output in that process when you redirect its output, so there's nothing to capture. Maybe it is your own process that you want to capture? (where you console.write)
Also, "cmd /c" carries out the command and exits, so that window should be closed before you try to capture it - surprised that you don't get exception on accessing MainWindowHandle.
 
There is no output in that process when you redirect its output, so there's nothing to capture. Maybe it is your own process that you want to capture? (where you console.write)

Aaaaahmm, very much yes. My own console process is the one to capture



Also, "cmd /c" carries out the command and exits, so that window should be closed before you try to capture it - surprised that you don't get exception on accessing MainWindowHandle.

I am as surprised as You are. The console does not close itself but I can still simply add Console.ReadKey()
(just for safety reasons)
 
Then you must pass Process.GetCurrentProcess() to CaptureCMD and not the 'external' p process.
 
Back
Top Bottom