C#获取桌面某个窗体截图进行OCR识别
来源:互联网 发布:淘宝买家采集 编辑:程序博客网 时间:2024/04/27 23:51
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);//获取截图的扫描结果。
- C#获取桌面某个窗体截图进行OCR识别
- OpenCV进行OCR识别
- C#关于OCR识别
- C#对被遮挡的窗体进行截图
- C# 桌面截图代码
- C# 桌面截图
- C#控件窗体截图
- C# 使用OCR识别中文
- C#桌面磁性窗体
- C# 桌面截图工具 demo
- windows之C# 桌面截图
- c# 窗体截图并导出
- 应用OpenCV进行OCR字符识别
- ubuntu 下安装 PyTesser 进行OCR识别
- 应用OpenCV进行OCR字符识别
- Java调用OCR进行图片识别
- Tesseract-OCR 进行文字识别 VS2010
- 利用tesseract-ocr进行验证码识别
- DD备份script
- Permutations(improve)
- 2014-4-11 NPC战斗那点破事
- Linux windows文件 路径
- 物联网应用多样化,民用化趋势日渐明显
- C#获取桌面某个窗体截图进行OCR识别
- Web提速:避免php session拖慢运行速度
- Centralized Cache Management in HDFS
- 高性能网络编程6--reactor反应堆与定时器管理
- C#Winform文件操作总结
- 求ASPxGridView 当数据源发生改变时 怎么刷新
- json那点事2
- display:inline-block
- iOS开发 SVN配置 使用办法(二)