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

本文实例讲述了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#程序设计有所帮助。

(0)

相关推荐

  • C#图像灰度级拉伸的方法

    本文实例讲述了C#图像灰度级拉伸的方法.分享给大家供大家参考.具体如下: //定义图像灰度拉伸函数 private static Bitmap GrayLP (Bitmap a) { Rectangle rect = new Rectangle(0, 0, a.Width, a.Height); System.Drawing.Imaging.BitmapData srcData = a.LockBits(rect, System.Drawing.Imaging.ImageLockMode.Rea

  • C#图像线性变换的方法

    本文实例讲述了C#图像线性变换的方法.分享给大家供大家参考.具体如下: //定义图像线性运算函数(y=kx+v) private static Bitmap LinearOP(Bitmap a, double k, double v) { Rectangle rect = new Rectangle(0, 0, a.Width, a.Height); System.Drawing.Imaging.BitmapData srcData = a.LockBits(rect, System.Drawi

  • C#实现TIF图像转PDF文件的方法

    本文实例讲述了C#实现TIF图像转PDF文件的方法.分享给大家供大家参考.具体实现方法如下: 这里介绍使用TIFtoPDF的用法.该工具可以将多个TIF图像文件合并成一个PDF文件 TIFtoPDF.rar文件点击此处本站下载. Program.cs文件如下: using System; using System.Collections.Generic; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; usi

  • C#图像透明度调整的方法

    本文实例讲述了C#图像透明度调整的方法.分享给大家供大家参考.具体如下: //定义图像透明度调整函数 public Bitmap PTransparentAdjust(Bitmap src,int num) { try { int w = src.Width; int h = src.Height; Bitmap dstBitmap = new Bitmap(src.Width, src.Height, System.Drawing.Imaging.PixelFormat.Format32bpp

  • C#图像处理之图像均值方差计算的方法

    本文实例讲述了C#图像处理之图像均值方差计算的方法.分享给大家供大家参考.具体如下: //本函数均是基于RGB颜色空间计算 //定义图像均值函数(RGB空间) public double AnBitmap(Bitmap a) { double V = 0; Rectangle rect = new Rectangle(0, 0, a.Width, a.Height); System.Drawing.Imaging.BitmapData bmpData = a.LockBits(rect, Sys

  • C#控制图像旋转和翻转的方法

    本文实例讲述了C#控制图像旋转和翻转的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication2 { public par

  • C#图像处理之头发检测的方法

    本文实例讲述了C#图像处理之头发检测的方法.分享给大家供大家参考.具体如下: //发色检测(YCbCr颜色空间) public Bitmap HairD(Bitmap a) { Rectangle rect = new Rectangle(0, 0, a.Width, a.Height); System.Drawing.Imaging.BitmapData bmpData = a.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWr

  • C#数字图像处理之图像缩放的方法

    本文实例讲述了C#数字图像处理之图像缩放的方法.分享给大家供大家参考.具体如下: //定义图像缩放函数 private static Bitmap ZoomP(Bitmap a, float s, float v) { Bitmap bmp = new Bitmap((int)(a.Width * s), (int)(a.Height * v), System.Drawing.Imaging.PixelFormat.Format24bppRgb); Graphics g = Graphics.F

  • C#图像伪彩色处理方法

    本文实例讲述了C#图像伪彩色处理方法.分享给大家供大家参考.具体如下: //灰度图转伪彩色图像函数 public Bitmap PGrayToColor(Bitmap src) { try { Bitmap a = new Bitmap(src); Rectangle rect = new Rectangle(0, 0, a.Width, a.Height); System.Drawing.Imaging.BitmapData bmpData = a.LockBits(rect, System.

  • C#图像颜色聚类高效方法实例

    本文实例讲述了C#图像颜色聚类高效方法.分享给大家供大家参考.具体分析如下: 图像颜色聚类的方法有很多,但是对于视频监控而言,现有方法很难满足实时性的要求,这里介绍一种位屏蔽压缩的方法实现颜色聚类,可以满足实时性的要求. 位屏蔽法就是在3D的RGB真彩空间中近似均匀采样的颜色压缩方法,即将屏蔽的颜色位置设置为0,具体可以采用移位运算来实现,这里我们以屏蔽RGB颜色分量末6位为例: public Bitmap PCluster(Bitmap a) { try { Rectangle rect =

  • 浅谈Visual C#进行图像处理(读取、保存以及对像素的访问)

    这里之所以说"浅谈"是因为我这里只是简单的介绍如何使用Visual C#进行图像的读入.保存以及对像素的访问.而不涉及太多的算法. 一.读取图像 在Visual C#中我们可以使用一个Picture Box控件来显示图片,如下: 复制代码 代码如下: private void btnOpenImage_Click(object sender, EventArgs e) {     OpenFileDialog ofd = new OpenFileDialog();     ofd.Fi

随机推荐