Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

i have some problems regarding this code from : Extracting Text From Image (C#.net) ~ BitsByta[^]

the provided code was converted into vb.net but i just keep getting this problem "Exception thrown: 'System.Runtime.InteropServices.COMException' in OCRtest.exe

Additional information: Object hasn't been initialized and can't be used yet"

on this line of code:
C#
doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, True, True)



VB.NET
Public Class frmOCRextract
    Private extractedText As String = String.Empty
    Private getFileName As String
    Dim openFile As New OpenFileDialog
    'Browsing an image
    Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click

        If openFile.ShowDialog = DialogResult.OK Then

            getFileName = openFile.FileName

            Dim targetImage As Image = Image.FromFile(getFileName)
            'Dim targetimage As Image = getFileName

            targetImage = fitInPBox(targetImage)


            pBox.Image = targetImage
        End If
    End Sub
    'This function fit the browsed image in picture box
    Private Function fitInPBox(img As Image) As Image
        Dim ixc As New Bitmap(img, New Size(pBox.Size.Width, pBox.Size.Height))
        Return DirectCast(ixc, Image)
    End Function
    'Extract button working, text is extracted from image
    Private Sub btnExtract_Click(sender As Object, e As EventArgs) Handles btnExtract.Click
        Dim doc As New MODI.Document()
        doc.Create(getFileName)
        doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, True, True)
        Dim img As MODI.Image = DirectCast(doc.Images(0), MODI.Image)
        Dim layout As MODI.Layout = img.Layout

        For i As Integer = 0 To layout.Words.Count - 1
            Dim word As MODI.Word = DirectCast(layout.Words(i), MODI.Word)

            If extractedText.Length > 0 Then
                extractedText += " "
            End If

            extractedText += word.Text
            RichTextBox1.Text = extractedText
        Next


    End Sub
End Class



tnx a lot

What I have tried:

i keep on getting those, even if i try to use C#. Still the same line gives me that error
Posted
Updated 27-Feb-16 22:49pm
Comments
Richard MacCutchan 28-Feb-16 3:31am    
Use your debugger to find out which object is invalid, and why.

1 solution

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

When asking for help on an error, the error message is the first thing to provide, but not the only one. Saying where (line number or position in source code) matters too.
At least the debugger will show you where your program fails. I recommend to put a comment in code to help spot where is the problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900