C# Console利用mspaint打开图像并保存的方法

 更新时间:2016年01月07日 09:33:59   作者:礼拜一  
这篇文章主要介绍了C# Console利用mspaint打开图像并保存的方法,涉及C#调用画图板操作图片的相关技巧,需要的朋友可以参考下

本文实例讲述了C# Console利用mspaint打开图像并保存的方法。分享给大家供大家参考,具体如下:

调用画图板压缩图片

System.Diagnostics.Process process = new System.Diagnostics.Process();
process = System.Diagnostics.Process.Start("mspaint.exe", path);
int processId = process.Id;
AutomationElement element = FindWindowByProcessId(processId);
System.Windows.Forms.SendKeys.SendWait("^s"); //发送 Ctrl + s 键
System.Windows.Forms.SendKeys.SendWait("%{F4}"); // 发送 Alt + F4 键

代码

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

模拟键盘输入

SendKeys.SendWait("{F5}");     //发送F5按键
SendKeys.SendWait("^s");    //发送 Ctrl + s 键
SendKeys.SendWait("%{F4}");   // 发送 Alt + F4 键
//按键 代码 
BACKSPACE {BACKSPACE}, {BS}, 或 {BKSP} 
BREAK {BREAK} 
CAPS LOCK {CAPSLOCK} 
DEL or DELETE {DELETE} 或 {DEL} 
DOWN ARROW {DOWN} 
END {END} 
ENTER {ENTER}或 ~ 
ESC {ESC} 
HELP {HELP} 
HOME {HOME} 
INS or INSERT {INSERT} 或 {INS} 
LEFT ARROW {LEFT} 
NUM LOCK {NUMLOCK} 
PAGE DOWN {PGDN} 
PAGE UP {PGUP} 
PRINT SCREEN {PRTSC} 
RIGHT ARROW {RIGHT} 
SendKeys.SendWait("+{TAB}");
SendKeys.SendWait("%f");//alt+f
SendKeys.SendWait("{Tab}");
SendKeys.SendWait("{Enter}")
//多次按键的代码
//为了指定重复键,使用 {key number} 的形式。必须在 key 与 number 之间放置一个空格。//例如,{LEFT 42} 意指 42 次按下 LEFT ARROW 键;{h 10} 则是指 10 次按下 H 键。

Where is the System.Windows.Automation

The UIAutomationClient.dll 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):

完整demo程序代码如下:

using System;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Windows.Automation;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
namespace UIATest
{
  class Program
  {
    static void Main(string[] args)
    {
      Process process = Process.Start(@"E:\WorkBook\ATP\WpfApp\bin\Debug\WpfApp.exe");
      int processId = process.Id;
      AutomationElement element = FindElementById(processId, "textBox1");
      SendKeys sendkeys = new SendKeys();
      sendkeys.Sendkeys(element, "Sending keys to input data");
      Console.WriteLine(sendkeys.ToString());
      sendkeys.Sendkeys(element, sendkeys.ContextMenu);
      Console.WriteLine(sendkeys.ToString());
      Console.WriteLine("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 = Process.GetProcessById(processId);
        targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
        return targetWindow;
      }
      catch (Exception ex)
      {
        count++;
        StringBuilder sb = new StringBuilder();
        string message = sb.AppendLine(string.Format("Target window is not existing.try #{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 = aeForm.FindFirst(TreeScope.Descendants,
      new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
      return tarFindElement;
    }
  }
}

希望本文所述对大家C#程序设计有所帮助。

相关文章

  • C#调用执行外部程序的实现方法

    C#调用执行外部程序的实现方法

    这篇文章主要介绍了C#调用执行外部程序的实现方法,涉及C#进程调用的相关使用技巧,非常简单实用,需要的朋友可以参考下
    2015-04-04
  • BarCode条形码基于C# GDI+ 的实现方法详解

    BarCode条形码基于C# GDI+ 的实现方法详解

    本篇文章介绍了,BarCode条形码基于C# GDI+ 的实现方法详解。需要的朋友参考下
    2013-05-05
  • C# SESSION丢失问题的解决办法

    C# SESSION丢失问题的解决办法

    这篇文章主要为大家详细介绍了C# SESSION丢失问题的解决办法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • C#中Socket与Unity相结合示例代码

    C#中Socket与Unity相结合示例代码

    这篇文章主要给大家介绍了关于C#中Socket与Unity相结合的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
    2017-10-10
  • C#中使用OpenCV的常用函数的常用示例

    C#中使用OpenCV的常用函数的常用示例

    这篇文章主要介绍了C#中使用OpenCV的常用函数的常用示例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-02-02
  • 使用C#发送Http请求实现模拟登陆实例

    使用C#发送Http请求实现模拟登陆实例

    本文主要介绍了使用C#发送Http请求实现模拟登陆实例,模拟登陆的原理简单,想要了解的朋友可以了解一下。
    2016-10-10
  • WPF实现数据绑定的几种方法

    WPF实现数据绑定的几种方法

    Windows Presentation Foundation (WPF) 是微软开发的一套用于构建用户界面的框架,在 WPF 中,数据绑定是一个非常重要的概念,它使得 UI 和数据源之间的同步变得简单和高效,本文将详细分析 WPF 中实现数据绑定的几种方法,需要的朋友可以参考下
    2024-12-12
  • C#通过JObject解析json对象

    C#通过JObject解析json对象

    这篇文章介绍了C#通过JObject解析json对象的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07
  • .NET实现父窗体关闭而不影响子窗体的方法

    .NET实现父窗体关闭而不影响子窗体的方法

    这篇文章主要介绍了.NET实现父窗体关闭而不影响子窗体的方法,很实用的功能,需要的朋友可以参考下
    2014-08-08
  • C# BackgroundWorker组件学习入门介绍

    C# BackgroundWorker组件学习入门介绍

    一个程序中需要进行大量的运算,并且需要在运算过程中支持用户一定的交互,为了获得更好的用户体验,使用BackgroundWorker来完成这一功能
    2013-10-10

最新评论