Click here to Skip to main content
15,885,365 members

How to crop a portion of a bitmap and save the cropped part

TechKidd asked:

Open original thread
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
Tags: Visual Basic

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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