private void mnuCaptureScreen_Click(object sender, System.EventArgs e) { picScreen.Image = CaptureScreen.GetDesktopImage(); }
// Public method to get A screen rectangle as ASCII text where screen is cleaned before // recognition public static string OCRSDK_Get_CleanedScreen_Ascii(int OCRSDK_ScrAx, int OCRSDK_ScrAy, int OCRSDK_ScrBx, int OCRSDK_ScrBy) { int OCRSDK_Stat; OCRSDK_Source_struct OCRSDK_Source; OCRSDK_Dest_struct OCRSDK_Dest; int OCRSDK_Dest_Fmt; string retstring; // the string that is returned // Filename of output .bmp file as a array for OCRSDK // Got to use bytes because char is now the unicode crap byte[] OCRSDKfilename = new byte[20]; OCRSDKfilename[0] = 0x73; // s OCRSDKfilename[1] = 0x66; // f OCRSDKfilename[2] = 0x2E; // . OCRSDKfilename[3] = 0x62; // b OCRSDKfilename[4] = 0x6D; // m OCRSDKfilename[5] = 0x70; // p OCRSDKfilename[6] = 0; // Filename for C# System.IO classes is a (Unicode chars) string string filename = "sf.bmp"; // C# filename // They want a clean version we will be recognising from sf.bmp // which we will build before the call to textract as follows // 1. Capture the rectangle from a screen area // 2. Clean the rectangle pixel by pixel so that any non white pixel is set black // 3. write the bmp for the cleaned rectangle to cleanrect.bmp // Capture from the screen rectangle into a Bitmap // (I'll use my own code rather than the OCRSDK way to contain the unknowns) // Get the raw Bitmap from the screen into mybmp1 Image screen_rect_img = CaptureScreen.GetDesktopImage(); Bitmap mybmp1 = new Bitmap(screen_rect_img); // Clean it // Can use a C# string for filename here // And can also use the C# ref here image_clean(ref mybmp1); mybmp1.Save(filename, System.Drawing.Imaging.ImageFormat.Bmp); // Check whether or Not OCRSDK has been inited and init if required if (!OCRSDK_Inited) { // Set up and send a message about status before the call OCRSDK_Stat = -1; // System.Windows.Forms.MessageBox.Show("Goinginto OCRSDK TextractInit with = OCRSDK_Stat=" + OCRSDK_Stat); // Make the call to init OCRSDK OCRSDK_Stat = TextractInit(); //System.Windows.Forms.MessageBox.Show("Back from OCRSDK TextractInit with = OCRSDK_Stat=" + OCRSDK_Stat); OCRSDK_Inited = true; } // Check whether or Not OCRSDK pattern file has been built and build if required if (!OCRSDK_Built) { // Set up and send a message about status before the call OCRSDK_Stat = -1; //System.Windows.Forms.MessageBox.Show("Goinginto OCRSDK TextractBuild with = OCRSDK_Stat=" + OCRSDK_Stat); // Make the call to init OCRSDK OCRSDK_Stat = TextractBuild(); //System.Windows.Forms.MessageBox.Show("Back from OCRSDK TextractBuild with = OCRSDK_Stat=" + OCRSDK_Stat); OCRSDK_Built = true; } // Fix the byte string for filename in memory while the OCRSDK.DLL does its foots around // fixed(byte *fixpointer = OCRSDKfilename) { // all this crap with fixpointer fixed so OCRSDKfilename is locked into memory // and the memory manager will leave it alone // shouldnt need to reset the filename - but it wont hurt OCRSDKfilename[0] = 0x73; // s OCRSDKfilename[1] = 0x66; // f OCRSDKfilename[2] = 0x2E; // . OCRSDKfilename[3] = 0x62; // b OCRSDKfilename[4] = 0x6D; // m OCRSDKfilename[5] = 0x70; // p OCRSDKfilename[6] = 0; // Source for recognition will be cleaned bmp file we created OCRSDK_Source.OCRSDK_Source_Bmp = (byte *)fixpointer; // Pointer to a bmp file name char array OCRSDK_Source.OCRSDK_Source_WindH = null; // Pointer to a Window Handle (not used) OCRSDK_Source.OCRSDK_Source_Ax = 0; // Screen rectangle top LH X coord (not used) OCRSDK_Source.OCRSDK_Source_Ay = 0; // Screen rectangle top LH X coord (not used) OCRSDK_Source.OCRSDK_Source_Bx = 0; // Screen rectangle top LH X coord (not used) OCRSDK_Source.OCRSDK_Source_By = 0; // Screen rectangle top LH X coord (not used) // Set the destination the way we want it OCRSDK_Dest.OCRSDK_Dest_Bmp = null; // Pointer to a bmp destination file name (not used) // Set the format to Ascii OCRSDK_Dest_Fmt = 0; // Set up and send a message about status before the call OCRSDK_Stat = -1; //System.Windows.Forms.MessageBox.Show("Goinginto OCRSDK Textract from bmpfile with = OCRSDK_Stat=" // + OCRSDK_Stat); // Make the call that should get text from the file sf.bmp OCRSDK_Stat = Textract(&OCRSDK_Source, // Source structure pointer &OCRSDK_Dest, // Dest structure pointer OCRSDK_Dest_Fmt); // Required format //System.Windows.Forms.MessageBox.Show("Backfrom OCRSDK Textract from bmpfile with = OCRSDK_Stat=" // + OCRSDK_Stat); //System.Windows.Forms.MessageBox.Show("OCRSDK Textract No of chars found =" + OCRSDK_Dest.OCRSDK_Dest_Size); } // Close out the scope where we fixed the pointer to the filename // Examine and set the return string // If there was nothing there return "" in the string, else return what OCRSDK found if ((OCRSDK_Dest.OCRSDK_Dest_Size != 0) && ((sbyte *)OCRSDK_Dest.OCRSDK_Dest_Area != null)) { // build a C# string (unicode chars etc etc) from the ASCII bytes in Dest_Area string mystring = new string((sbyte *)OCRSDK_Dest.OCRSDK_Dest_Area, 0, OCRSDK_Dest.OCRSDK_Dest_Size); retstring = mystring; } else { string mystring = ""; retstring = mystring; } // System.Windows.Forms.MessageBox.Show(" found =" + retstring); return(retstring); }// Ends OCRSDK_Get_CleanScreen_Ascii