Click here to Skip to main content
16,011,647 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey there, i'm stuck on this!

I need to crop every letter square out of this picture, and store each letter in an image array of some sort, basically the reason for this is to compare the images to a preset alphabet of these letters to figure out what letter it is (OCR would work better for this, but i can't find a free working OCR class for vb.net).
I am using the System.Drawing Library
I hope someone can help me! :)
Picture here

here is what I had that doesn't work:
VB
Dim grid(4, 4) As Point
    Dim letters() As Image
    Dim boardCoord As New Point(0, 0)
    Dim curLetter As Integer = 0
    Dim b As Image 'The image file provided
    Dim gRec(4, 4) As Rectangle
    Private Sub captureBoard()
        Dim tPoint As Point
        b = PictureBox1.Image
       'Should set the coords of the upper left corner of each square.
        For i As Integer = 0 To 3
            ' down, y
            For ii As Integer = 0 To 3 ' -> |||| x
                tPoint = New Point(boardCoord.X + (25 * ii), boardCoord.Y + (25 * i))
                grid(ii, i) = tPoint
            Next
        Next

        'Should save all the rectangles for each of the letters, to easily use when cropping.
        For x As Integer = 1 To 4
            For y As Integer = 1 To 4
                gRec(x, y) = New Rectangle(grid(x, y), New Size(20, 20))
            Next
        Next
        'Should crop each of the 16 total squares, using the preset rectangles
        For i As Integer = 1 To 16
            For x As Integer = 1 To 4
                For y As Integer = 1 To 4
                    Dim j = gRec(x, y)
                    letters(i) = CropLetters(b, j.X, j.Y, j.Size.Width, j.Size.Height)(i)
                Next
            Next
        Next
    End Sub


    Private Function CropLetters(ByVal board As Bitmap, ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer) As Image()
        Dim rect As Rectangle = New Rectangle(X, Y, Width, Height) 'the current rectangle to crop
        Dim tBitmaps() As Bitmap 'the holding variable
        For i As Integer = 1 To 16 'cycles through the 16 squares and crops each one
            Dim g As Graphics = Graphics.FromImage(board)
            g.DrawImage(tBitmaps(i), 0, 0, rect, GraphicsUnit.Pixel) 'Should crop the "letter" to the holding var item
        Next
        Return tBitmaps
    End Function
Posted
Updated 14-Nov-12 18:37pm
v3
Comments
Sergey Alexandrovich Kryukov 13-Nov-12 18:32pm    
Would you explain why?! And your picture does not explain much -- letters as letters. Do you want to crop empty space out, or the graphics of the glyph itself?

Also, what have you done so far? It does not seem to be a problem.
Also, you need to tag the UI library or graphics library you want to use or are using.

But first of all, please explain why. I don't see how it might makes sense. And not many members will volunteer to waste time on something which may net make sense...

I removed your last question -- you should not re-post. It cannot help you, but you can get down-voted and abuse reports.

--SA
Sergey Alexandrovich Kryukov 14-Nov-12 22:31pm    
What's going on? I deleted one more re-post you have recently done. I have to warn you: by systematic re-posting, you risk not just down-voting, but also reports for the abuse, which can go as far as the reports for spamming, if re-posts become too annoying (in my opinion, this is not the case at all, but better be on safe side).

Instead, use "Improve question" above, or comment on existing posts...
--SA
TechKidd 15-Nov-12 0:28am    
Sorry about the re-post, i didn't notice your comment on here, and i only noticed my posts being deleted for no apparent reason..
Can you possibly delete the "solution" below?
Sergey Alexandrovich Kryukov 15-Nov-12 0:49am    
Sorry, I don't think deleting it would be correct treatment of the author of this post...
It does not really hurts you -- just don't accept it formally.
--SA
TechKidd 15-Nov-12 22:32pm    
Well, I'm just guessing, but doesn't that mean that my question is no longer under the "unanswered" questions?

1 solution

To work out what is a letter in an image is largely an OCR task, and not trivial. You didn't include a link to the picture, so the code is largely meaningless. If you want the user to SELECT the letters, then it becomes trivial. You need to create new bitmaps for each letter, there's no better way to store them.
 
Share this answer
 
Comments
TechKidd 13-Nov-12 16:48pm    
I know OCR would be A LOT easier, but i haven't found a way to use it without having to spend a lot of money.
I did include a picture..
But thanks for your feedback. :)

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