maybe it is the best if I post some sample code:
I hope you understand a bit German :)
Code:
public Dictionary<string, string> OCR_Table;
public static Bitmap GetBitmap(int x, int y, int dx, int dy)
{
Bitmap screenCopy = new Bitmap(dx, dy);
using (Graphics gdest = Graphics.FromImage(screenCopy))
using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero))
{
IntPtr hSrcDC = gsrc.GetHdc();
IntPtr hDC = gdest.GetHdc();
int retval = BitBlt(hDC, 0, 0, dx, dy, hSrcDC, x, y, (int)CopyPixelOperation.SourceCopy);
gdest.ReleaseHdc();
gsrc.ReleaseHdc();
}
return screenCopy;
}
private string AnalyseNPC_Name_Phase2(int Mode, Bitmap bild, int x, int y, int x2, int y2)
{
// Bild abmalen und ins log schreiben
string Zeile = "";
for (int yy = y; yy <= y2; yy++)
{
for (int xx = x; xx <= x2; xx++)
{
Color c = bild.GetPixel(xx, yy);
if (Mode == 0)
{
if (c.B < 80 && c.R < 80 & c.G < 80)
Zeile += "X";
else if (c.B < 100 && c.R < 100 & c.G < 100)
Zeile += "x";
else if (c.B < 120 && c.R < 120 & c.G < 120)
Zeile += "+";
else if (c.B < 140 && c.R < 140 & c.G < 140)
Zeile += ".";
else
Zeile += "_";
}
else
{
if (c.B > 100 && c.R > 100 & c.G > 100)
Zeile += "X";
else
Zeile += "_";
}
}
Zeile += "\n";
}
//AddToProtokoll(Zeile);
//
string Buchstabe;
if (OCR_Table.TryGetValue(Zeile, out Buchstabe) == false)
{
if (LearnMode == true)
{
// Dialog anzeigen
OCR_Ask_for_Letter dlg = new OCR_Ask_for_Letter();
dlg.richTextBox1.AppendText(Zeile);
dlg.ShowDialog();
Buchstabe = dlg.textBox1.Text;
if (Buchstabe.Length > 0)
{
OCR_Table.Add(Zeile, Buchstabe);
}
}
else
Buchstabe = "?";
}
return Buchstabe;
}
private string AnalyseNPC_Name_Phase1(int Mode, Bitmap bild)
{
// 1. Bild in Buchstaben trennen
string erg = "";
bool inBuchstabe = false;
int minX = 9999;
int minY = 9999;
int maxY = -1;
int UngueltigeZeichenFaktor = 0;
for (int x = 0; x < bild.Width; x++)
{
bool SchwarzFlag = false;
for (int y = 0; y < bild.Height; y++)
{
// stehen in der aktuellen Row Daten ?
Color c = bild.GetPixel(x, y);
if (Mode == 0)
{
// Schwarze Schrift auf grauem grund
if (c.B < 100 && c.R < 100 & c.G < 100)
{
SchwarzFlag = true;
if (y < minY) minY = y;
if (y > maxY) maxY = y;
if (x < minX) minX = x;
inBuchstabe = true;
}
}
else
{
// weiße schrift auf schwarzem grund
if (c.B > 100 && c.R > 100 & c.G > 100)
{
SchwarzFlag = true;
if (y < minY) minY = y;
if (y > maxY) maxY = y;
if (x < minX) minX = x;
inBuchstabe = true;
}
}
}
if ((SchwarzFlag == false) && (inBuchstabe == true))
{
// erste leere Reihe hinter einem Buchstaben gefunden
string text = "Phase 1: x:" + minX.ToString() + "-" + x.ToString() + " y:" + minY.ToString() + "-" + maxY.ToString() + "\n";
//AddToProtokoll(text);
string Zeichen = AnalyseNPC_Name_Phase2(Mode, bild, minX, minY, x, maxY);
erg += Zeichen;
if (Zeichen == "?")
UngueltigeZeichenFaktor -= 1;
else
UngueltigeZeichenFaktor += 1;
// alles wieder auf Anfang
minX = 9999; minY = 9999; maxY = -1; inBuchstabe = false;
}
}
if (UngueltigeZeichenFaktor < 0) // um zufallstreffer zu vermeiden.
erg = "";
// AddToProtokoll(">>>:" + erg + "\n");
return erg;
}
private string AnalyseNPC_Name(int Mode /* 0 = normaler Text , 1 = xp in % ( sw hintergrund bei ws schrift ) */)
{
Bitmap bild=null;
if (Mode == 0)
{
bild = Tools.GetBitmap(BasisX + 743, BasisY + 22, 185, 13);
}
else if (Mode == 1)
{
bild = Tools.GetBitmap(BasisX + 6, BasisY + 94, 40, 9);
}
// MainWindow.mainWindow.pictureBox1.Image = bild;
string erg = AnalyseNPC_Name_Phase1(Mode, bild);
return erg;
}
This is an very easy Bitmap to String Converter.
Function AnalyseNPC_Name_Phase1() is splitting the text into single charaters.
Function AnalyseNPC_Name_Phase2() is converting the bitmap of a single character into a string and stores in a Dictionary.
The LearnMode variable controlls if the code is in bot-mode or in learning Mode.
In LearnMode it will ask the user for unknowen letters with an dialog.
Not in the example is the code for saving the OCR_Table Dictionary and the Dialog that asks the User for a Charater in the case it found something new.