选定区域的OCR

时间:2018-01-23 21:44:15

标签: c# winforms

我怎样才能获得selection area转换后的OCR? 目前,该过程是要打开的静态图像,然后转换整页。

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    selection.mouseDown(e);
    pictureBox1.Refresh();
}


private void button1_Click(object sender, EventArgs e)
{
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        var img = new Bitmap(openFileDialog.FileName);
        var ocr = new TesseractEngine("./tessdata", "eng", EngineMode.TesseractAndCube);
        var page = ocr.Process(img);
        txtResult.Text = page.GetText();
    }
}

1 个答案:

答案 0 :(得分:0)

使用variabel保存MouseDown位置。也许你的selection变量已经这样做了。并使用picturebox MouseUp事件来获取该区域。然后抓住该区域到位图。

private Point _MouseDownCoordinates;

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    //selection.mouseDown(e);
    _MouseDownCoordinates = new Point(e.X, e.Y);
    pictureBox1.Refresh();
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    Int32 deltaX = e.X - _MouseDownCoordinates.X;
    Int32 deltaY = e.Y - _MouseDownCoordinates.Y;
    Rectangle rect = new Rectangle(_MouseDownCoordinates, new Size(deltaX, deltaY));

    var img = new Bitmap(deltaX, deltaY, PixelFormat.Format24bppRgb);
    using (var g = Graphics.FromImage(img))
    {
        g.CopyFromScreen(panel1.PointToScreen(_MouseDownCoordinates), new Point(0, 0), rect.Size);

        var ocr = new TesseractEngine("./tessdata", "eng", EngineMode.TesseractAndCube);
        var page = ocr.Process(img);
        txtResult.Text = page.GetText();
     }
}