Click here to Skip to main content
13,047,567 members (59,727 online)

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
I am using the System.Drawing Library
I hope someone can help me! :)
Picture here

here is what I had that doesn't work:
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
        '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))
        '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)
    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
        Return tBitmaps
    End Function
Tags: VB, VB.NET


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.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100