如何裁剪位图的一部分并保存裁剪的部分 [英] How to crop a portion of a bitmap and save the cropped part

查看:99
本文介绍了如何裁剪位图的一部分并保存裁剪的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我被困住了!

我需要裁剪出这张图片中的每个字母方块,并将每个字母存储在某种图像数组中,基本上是这样做的原因是将图像与这些字母的预设字母进行比较,以找出它是什么字母(OCR对此会更好,但是我无法在vb.net上找到免费的OCR类). /> 我正在使用System.Drawing库
我希望有一个人可以帮助我! :)
图片在这里

这是我无法使用的内容:

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:

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

推荐答案

确定图像中的字母主要是OCR任务,而不是琐碎的.您没有包括图片的链接,因此代码在很大程度上没有意义.如果您希望用户选择字母,那么它就变得微不足道了.您需要为每个字母创建新的位图,没有更好的方法来存储它们.
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.


这篇关于如何裁剪位图的一部分并保存裁剪的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆