C# Console利用mspaint打开图像并保存的方法
作者:礼拜一 发布时间:2023-07-05 16:15:43
标签:C#,Console,图像
本文实例讲述了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("Targetwindowisnotexisting.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#程序设计有所帮助。


猜你喜欢
- Java 中java.io.IOException: Broken pipe认识broken pipepipe是管道的意思,管道里面是数据流
- 前言学习自定义view,想找点东西耍一下,刚好看到抖音的点赞效果不错,尝试一下。抖音效果: 话不多说,先上代码:public class L
- Java多线程下载网图案例此案例依赖——文件操作工具类(FileUtils)使用 apache 的commons-io包下的FileUtil
- 在本篇介绍的Winform界面样式改变及存储操作中,是指基于DevExpress进行界面样式的变化。一般情况下,默认我们会为客户提供多种De
- EventBus 可以很方便地进行各组件间的通信,解耦性更强,比广播更好用。EventBus 3 简介EventBus是一种为了优化Andr
- Java实现驼峰、下划线互转1.使用 Guava 实现先引入相关依赖<dependency> <
- 在有些开发场景,需要对 List 对象列表进行过滤处理,并将有用的数据存放到Map中。例如:告警对象,包含告警uuid(alarmUuid)
- 1.概述1、Spring 是轻量级的开源的 JavaEE 框架2、 Spring 可以解决企业应用开发的复杂性3、Spring 有两个核心部
- 1、获取表中最后一条数据public static String demo() throws SQLException { String s
- 大家好,欢迎来到老胡的博客,今天我们继续了解设计模式中的职责链模式,这是一个比较简单的模式。跟往常一样,我们还是从一个真实世界的例子入手,这
- 本文实例讲述了C#操作CSV文件类。分享给大家供大家参考。具体分析如下:这个C#类用于转换DataTable为CSV文件、CSV文件转换成D
- C#调用MFC 窗口 DLLMFC DLL创建一个窗口类,加public和AFX_EXT_CLASSMFC DLL属性注意MFC的使用:在共
- Android EditText限制输入字符类型的方法总结前言:最近的项目上需要限制EditText输入字符的类型,就把可以实现这个功能的方
- 首先什么是注解?@Override就是注解,它的作用是:1、检查是否正确的重写了父类中的方法。 2、标明代码,这是一个重写的方法。1、体现在
- ArrayList与Array的区别概述 ArrayList 是数
- 目录如何快速获取 相册分类一些异常情况的处理Recycleview-CursorAdapter还有必要用LoaderManager吗总结如何
- 本文实例讲述了Android编程设计模式之原型模式。分享给大家供大家参考,具体如下:一、介绍原型模式是一个创建型的模式。原型二字表明了该模型
- 代码很简单,功能也很简单,这里就不多废话了#include<stdio.h>int main(){ char ku[16]={&
- 你是否遇到过,出现异常的时候也需要给一个默认值,让程序可以继续运行下去?一般的做法就是 一个达到try catch,然后在finally里面
- 如何在多线程中使用随机数生成器(Random)避免 Random 实例被多线程使用,虽然共享该实例是线程安全的,但会因竞争同一seed 导致