SoFunction
Updated on 2025-03-07

C# Console uses mspaint to open and save images

This article illustrates the method of C# Console using mspaint to open and save images. Share it for your reference, as follows:

Call the drawing board to compress pictures

 process = new ();
process = ("", path);
int processId = ;
AutomationElement element = FindWindowByProcessId(processId);
("^s"); //Send Ctrl + s key("%{F4}"); // Send Alt + F4 keys

Code

public static AutomationElement FindWindowByProcessId(int processId)
{
  AutomationElement targetWindow = null;
  int count = 0;
  try
  {
    Process p = (processId);
    targetWindow = ();
    return targetWindow;
  }
  catch (Exception ex)
  {
    count++;
    StringBuilder sb = new StringBuilder();
    string message = (("Target window is not  #{0}", count)).ToString();
    if (count > 5)
    {
      throw new InvalidProgramException(message, ex);
    }
    else
    {
      return FindWindowByProcessId(processId);
    }
  }
}

Analog keyboard input

("{F5}");     //Send F5 button("^s");    //Send Ctrl + s key("%{F4}");   // Send Alt + F4 keys//Key button codeBACKSPACE {BACKSPACE}, {BS}, or {BKSP} 
BREAK {BREAK} 
CAPS LOCK {CAPSLOCK} 
DEL or DELETE {DELETE} or {DEL} 
DOWN ARROW {DOWN} 
END {END} 
ENTER {ENTER}or ~ 
ESC {ESC} 
HELP {HELP} 
HOME {HOME} 
INS or INSERT {INSERT} or {INS} 
LEFT ARROW {LEFT} 
NUM LOCK {NUMLOCK} 
PAGE DOWN {PGDN} 
PAGE UP {PGUP} 
PRINT SCREEN {PRTSC} 
RIGHT ARROW {RIGHT} 
("+{TAB}");
("%f");//alt+f
("{Tab}");
("{Enter}")
//Code of multiple key presses//To specify a duplicate key, use the form {key number}.  A space must be placed between the key and number.  //For example, {LEFT 42} means pressing the LEFT ARROW key 42 times; {h 10} means pressing the H key 10 times.

Where is the

The is located in this folder:

C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0

If you can't find in your Add Reference->.Net tab, then you have to use the Browse tab to go to the given path, and add the assembly (Right Click on the References, choose add reference, click browse tab):

The complete demo program code is as follows:

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
namespace UIATest
{
  class Program
  {
    static void Main(string[] args)
    {
      Process process = (@"E:\WorkBook\ATP\WpfApp\bin\Debug\");
      int processId = ;
      AutomationElement element = FindElementById(processId, "textBox1");
      SendKeys sendkeys = new SendKeys();
      (element, "Sending keys to input data");
      (());
      (element, );
      (());
      ("Test finised."); 
    }
    /// <summary>
    /// Get the automation elemention of current form.
    /// </summary>
    /// <param name="processId">Process Id</param>
    /// <returns>Target element</returns>
    public static AutomationElement FindWindowByProcessId(int processId)
    {
      AutomationElement targetWindow = null;
      int count = 0;
      try
      {
        Process p = (processId);
        targetWindow = ();
        return targetWindow;
      }
      catch (Exception ex)
      {
        count++;
        StringBuilder sb = new StringBuilder();
        string message = (("Target window is not  #{0}", count)).ToString();
        if (count > 5)
        {
          throw new InvalidProgramException(message, ex);
        }
        else
        {
          return FindWindowByProcessId(processId);
        }
      }
    }
    /// <summary>
    /// Get the automation element by automation Id.
    /// </summary>
    /// <param name="windowName">Window name</param>
    /// <param name="automationId">Control automation Id</param>
    /// <returns>Automatin element searched by automation Id</returns>
    public static AutomationElement FindElementById(int processId, string automationId)
    {
      AutomationElement aeForm = FindWindowByProcessId(processId);
      AutomationElement tarFindElement = (,
      new PropertyCondition(, automationId));
      return tarFindElement;
    }
  }
}

I hope this article will be helpful to everyone's C# programming.