C#获取桌面某个窗体截图进行OCR识别

1.首先电脑需要安装完整版的Microsoft Office并选择安装Microsoft Office 工具(默认是不安装的)。

 

2.添加项目对MODI的引用:右键单击项目下的引用--添加引用--选择COM下的--Microsoft Office Document Imaging 12.0 Object Library--确定。

 

3.添加 using System.Runtime.InteropServices; 命名空间,以调用windows API函数。

 

4.所用到的API函数:

 

[DllImport("user32.dll")]//获取窗口句柄

         public static extern IntPtr FindWindow(

         string lpClassName,

         string lpWindowName

         );

 [DllImport("user32.dll")]

        public static extern IntPtr GetWindowDC(

        IntPtr hwnd

        );

 [DllImport("gdi32.dll")]

        public static extern IntPtr CreateCompatibleBitmap(

        IntPtr hdc, // handle to DC

        int nWidth, // width of bitmap, in pixels

        int nHeight // height of bitmap, in pixels

        );

[DllImport("gdi32.dll")]

        public static extern IntPtr CreateCompatibleDC(

        IntPtr hdc // handle to DC

        );

 [DllImport("gdi32.dll")]

        public static extern IntPtr SelectObject(

        IntPtr hdc, // handle to DC

        IntPtr hgdiobj // handle to object

        );

 [DllImport("user32.dll")]

        public static extern bool PrintWindow(

        IntPtr hwnd, // Window to copy,Handle to the window that will be copied. 

        IntPtr hdcBlt, // HDC to print into,Handle to the device context. 

        UInt32 nFlags // Optional flags,Specifies the drawing options. It can be one of the following values. 

        );

 [DllImport("gdi32.dll")]

        public static extern int DeleteDC(

        IntPtr hdc // handle to DC

        );

 [DllImport("gdi32.dll")]

        public static extern int DeleteObject(

        IntPtr hdc 

        );

 

5.示例操作:

 

 private static IntPtr hWnd = IntPtr.Zero;

 private static string savePath = "";

 private static string resultStr = "";

 

 public static Bitmap GetImg(IntPtr hWnd, int Width, int Height)//得到窗口截图

        {

            IntPtr hscrdc = GetWindowDC(hWnd);

            IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, Width, Height);

            IntPtr hmemdc = CreateCompatibleDC(hscrdc);

            SelectObject(hmemdc, hbitmap);

            PrintWindow(hWnd, hmemdc, 0);

            Bitmap bmp = Bitmap.FromHbitmap(hbitmap);

            DeleteDC(hscrdc);//删除用过的对象

            DeleteObject(hbitmap);//删除用过的对象

            DeleteDC(hmemdc);//删除用过的对象

            return bmp;

        }

 

 private static string Recognition(string strFileName)//获取扫描信息

        {

            string strResult = string.Empty;

            MODI.Document modiDocument = new MODI.Document();

            modiDocument.Create(strFileName);

            MODI.Image modiImage = (MODI.Image)modiDocument.Images[0];

            modiImage.OCR(MODI.MiLANGUAGES.miLANG_CHINESE_SIMPLIFIED, false, false);//在这里设置要识别的语言的种类。

            strResult = modiImage.Layout.Text;

            modiDocument.Close(false);

            strResult = strResult.Replace(" ", "");

            return strResult;

        }

 

 hWnd = FindWindow(null, "记事本");//得到名称为“记事本”的窗口句柄。

 savePath="D\\temp.bmp";//设置图片的临时保存路径。

 Bitmap img = GetImg(hWnd, X, Y);//X,Y为所要获取截图的窗口宽度和高度。

 img.Save(savePath, ImageFormat.Bmp);//保存得到的截图。

 resultStr = Recognition(saveImg);//获取截图的扫描结果。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值