private void timer_Tick(object sender, EventArgs e) { Bitmap bmp = Grabber.GrabScreen(); if (bmp == null) { return; } bmp = Parser.RemoveBorder(bmp); if (calibration != null) { if (calibration.Add(bmp) && !calibration.IsFinished) { taskbar.StepCalibration(); } else if (!calibration.WasSame) { WindowState = prevState; } bmp.Dispose(); return; } ColorInformation ci = new ColorInformation(); richTextBox.Text = Parser.Parse(bmp, ci); bmp.Dispose(); if (ci.ForegroundColors != null) { for (int y = 0; y < ci.ForegroundColors.Length / ci.ScreenWidth; y++) { for (int x = 0; x < ci.ScreenWidth; x++) { int idx = y * ci.ScreenWidth + x; richTextBox.Select(ci.MessageLength + y * (ci.ScreenWidth + 1) + x, 1); richTextBox.SelectionColor = ci.ForegroundColors[idx]; richTextBox.SelectionBackColor = ci.BackgroundColors[idx]; } } if (ci.CursorX != -1) { richTextBox.Select(ci.MessageLength + ci.CursorY * (ci.ScreenWidth + 1) + ci.CursorX, 1); richTextBox.SelectionFont = new Font(richTextBox.Font, FontStyle.Underline | FontStyle.Bold); } } richTextBox.Select(richTextBox.Text.Length, 0); WindowState = prevState; }
public static string Parse(Bitmap bmp, ColorInformation info) { int bestErrors = int.MaxValue, tmp; string bestResult = ""; Bitmap bestGlyphMap = null; int[] bestScreenSize = null; BitmapData data = bmp.LockBits(new Rectangle(Point.Empty, bmp.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); int bmpStride = data.Stride / 4; int[] bmpData = new int[bmpStride * data.Height]; Marshal.Copy(data.Scan0, bmpData, 0, bmpData.Length); bmp.UnlockBits(data); foreach (string mapFile in Directory.GetFiles("glyphmaps", "*." + bmp.Width + "x" + bmp.Height + ".glyphmap")) { Bitmap glyphMap = new Bitmap(mapFile); int parseErrors; int[] screenSize = new int[2]; string result = Parse(bmp, bmpData, bmpStride, glyphMap, out parseErrors, screenSize, null, null, out tmp, out tmp); if (parseErrors < bestErrors) { bestErrors = parseErrors; bestResult = result; if (bestGlyphMap != null) { bestGlyphMap.Dispose(); } bestGlyphMap = glyphMap; bestScreenSize = screenSize; if (parseErrors == 0) { break; } } else { glyphMap.Dispose(); } } string message = ""; if (bestErrors == int.MaxValue) { message = "Grabbing failed. Maybe you need to calibrate the font first?"; } else if (bestErrors != 0) { message = "*** WARNING: " + bestErrors + " characters could not be detected ***\n\n"; } if (info != null && bestGlyphMap != null) { info.MessageLength = message.Length; info.ScreenWidth = bestScreenSize[0]; int height = bestScreenSize[1]; info.ForegroundColors = new Color[info.ScreenWidth * height]; info.BackgroundColors = new Color[info.ScreenWidth * height]; Parse(bmp, bmpData, bmpStride, bestGlyphMap, out bestErrors, bestScreenSize, info.ForegroundColors, info.BackgroundColors, out info.CursorX, out info.CursorY); } if (bestGlyphMap != null) { bestGlyphMap.Dispose(); } return(message + bestResult); }