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);//获取截图的扫描结果。