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#程序设计有所帮助。
0
投稿
猜你喜欢
- [LeetCode] 131.Palindrome Partitioning 拆分回文串Given a string s, par
- 1)字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strcat(p, p1)
- 就我们所知道的,java中有子类和父类,子类由于继承父类而形成,那么父类还有没有父类呢?答案是有了,父类的父类就是object类,一切父类都
- 前言本身我是一个比较偏向少使用Stream的人,因为调试比较不方便。但是, 不得不说,stream确实会给我们编码带来便捷。Stream流
- Android自带的跑马灯效果不太好控制,还必须要满足条件才能有效果,而且速度不受控制。前面我的博客中有一篇就是用Android自带的跑马灯
- java io操作中通常采用BufferedReader,BufferedInputStream等带缓冲的IO类处理大文件,不过java n
- XmlTextWriter类允许你将XML写到一个文件中去。这个类包含了很多方法和属性,使用这些属性和方法可以使你更容易地处理XML。为了使
- 概述Kryo 是一个快速序列化/反序列化工具,依赖于字节码生成机制(底层使用了 ASM 库),因此在序列化速度上有一定的优势,但正因如此,其
- Java super关键字super 关键字与 this 类似,this 用来表示当前类的实例,super 用来表示父类。super 可以用
- 在做业务开发时,遇到了一个事务不起作用的问题。大概流程是这样的,方法内部的定时任务调用了一个带事务的方法,失败后事务没有回滚。查阅资料后,问
- 如下所示:import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.
- JDK8中有双冒号的用法,就是把方法当做参数传到stream内部,使stream的每个元素都传入到该方法里面执行一下。代码其实很简单:以前的
- idea切换分支时,修改过的代码文件全部不见了找了一下问题,切换分支时,idea自动会创建暂存文件,点开,右边View --> 即可显
- 简要:EigenFace是基于PCA降维的人脸识别算法,PCA是使整体数据降维后的方差最大,没有考虑降维后类间的变化。 它是将图像
- 类似普通对象,通过new创建字符串对象。String str = new String("Hello"); 内存图如下图
- 一、项目简述功能包括(管理员和用户角色): 酒店预订,酒店管理,员工管理,入住原理,订单管理, 楼层管理,退房管理,营业额报表等等。二、项目
- List去重复,我们首先想到的可能是 利用List转Set集合,因为Set集合不允许重复。所以达到这个目的。 如果集合里面是简单对
- 前言Flutter (Channel stable, 2.10.3, on Microsoft Windows [Version 10.0.
- 什么是注解在早期的工作的时候 ,自定义注解写的比较多,可大多都只是因为 这样看起来 不会存在一堆代码耦合在一起的情况,所以使用了自定义注解,
- 本文通过JavaMailSender实现邮箱注册验证中遇到的问题开始着手,给大家详细分析了其原理以及问题的解决办法。使用邮箱注册验证,我们需