将代码移动到 vb.net 中的模块

moving code to modules in vb.net

我有一个小程序可以扫描文档并读取条形码(然后将条形码分成不同的目录和文件名)。一切正常,但我现在需要稍微扩展程序。我需要添加另外几个执行类似操作的表单,因此我将执行扫描图像工作的代码(即裁剪、旋转、获取条形码等)移到了一个模块中。

一旦我这样做,我就无法让代码执行它正在做的事情。

例如,我必须裁剪图像的代码如下:

Public Sub Crop()

    Dim imageAttr1 As New ImageAttributes()
    imageAttr1.SetGamma(2.2F)
    bitmap1 = System.Drawing.Bitmap.FromFile(path & StrFileName & ".bmp")
    Try

        PicBitmap = bitmap1 'CType(Bitmap.FromStream(MyImage), Bitmap)
        OKTickets.ImgTicket.SizeMode = PictureBoxSizeMode.CenterImage
        If bitmap1 IsNot Nothing Then
            bitmap1.RotateFlip(RotateFlipType.Rotate270FlipNone)
            OKTickets.ImgTicket.Image = bitmap1
        End If

        cropX = 2700
        cropY = 600
        cropWidth = 1200 'PictureBox1.Width
        cropHeight = 500 'PictureBox1.Height

        'a rectangle to set the location and size from the source image
        Dim rect As Rectangle = New Rectangle(cropX, cropY, cropWidth, cropHeight)

                    Dim bit As Bitmap = New Bitmap(OKTickets.ImgTicket.Image, OKTickets.ImgTicket.Width, OKTickets.ImgTicket.Height)

        'create a new bitmap with the width/height values that were specified in the textboxes.
        cropBitmap = New Bitmap(cropWidth, cropHeight)

        'a new Graphics object that will draw on the cropBitmap
        Dim g As Graphics = Graphics.FromImage(cropBitmap)

        'draw the portion of the image that you supplied cropping values for.
        g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel)
        g.DrawImage(cropBitmap, rect, 0, 0, OKTickets.ImgTicket.Width, OKTickets.ImgTicket.Height, GraphicsUnit.Pixel, imageAttr1)
        'OKTickets.ImgTicket.Image = cropBitmap

        MsgBox("image done")

        'Dim rect As New Rectangle(250, 20, 200, 200)

    Catch ex As System.IO.FileNotFoundException
        MessageBox.Show("There was an error. Check the path to the bitmap.")
    End Try

End Sub

如果它与表单在同一个 class 中,那么它将执行它应该执行的操作并裁剪扫描的图像并将其显示在 form1 的图片框 (ImgTicket) 中。如果我将它移动到模块,则图片框中不会显示任何内容。我已逐步完成代码,它运行正常,但只是没有在图片框中显示裁剪后的图像。

我还没有t used modules before so any beginner explanations would be helpful as all the things I读过让我完全困惑。

模块代码如下:

Imports System.Drawing

进口System.Drawing.Imaging

模块 ScanImage

Dim bitmap1 As Bitmap
Dim cropBitmap As Bitmap
Dim cropX As Integer
Dim cropY As Integer
Dim cropWidth As Integer
Dim cropHeight As Integer
Dim path As String = "C:\ProSys\Images\Test\Temp\"
Dim StrFileName As String = "1"
Dim PicBitmap As Bitmap

Public Sub Crop()

    Dim imageAttr1 As New ImageAttributes()
    imageAttr1.SetGamma(2.2F)
    bitmap1 = System.Drawing.Bitmap.FromFile(path & StrFileName & ".bmp")
    Try

        PicBitmap = bitmap1 'CType(Bitmap.FromStream(MyImage), Bitmap)
        OKTickets.ImgTicket.SizeMode = PictureBoxSizeMode.CenterImage
        If bitmap1 IsNot Nothing Then
            bitmap1.RotateFlip(RotateFlipType.Rotate270FlipNone)
            OKTickets.ImgTicket.Image = bitmap1
        End If

        cropX = 2700
        cropY = 600
        cropWidth = 1200 'PictureBox1.Width
        cropHeight = 500 'PictureBox1.Height

        'a rectangle to set the location and size from the source image
        Dim rect As Rectangle = New Rectangle(cropX, cropY, cropWidth, cropHeight)

        Dim bit As Bitmap = New Bitmap(OKTickets.ImgTicket.Image, OKTickets.ImgTicket.Width, OKTickets.ImgTicket.Height)

        'create a new bitmap with the width/height values that were specified.
        cropBitmap = New Bitmap(cropWidth, cropHeight)

        'a new Graphics object that will draw on the cropBitmap
        Dim g As Graphics = Graphics.FromImage(cropBitmap)

        'draw the portion of the image that you supplied cropping values for.
        g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel)
        g.DrawImage(cropBitmap, rect, 0, 0, OKTickets.ImgTicket.Width, OKTickets.ImgTicket.Height, GraphicsUnit.Pixel, imageAttr1)
        OKTickets.ImgTicket.Image = cropBitmap

    Catch ex As System.IO.FileNotFoundException
        MessageBox.Show("There was an error. Check the path to the bitmap.")
    End Try

End Sub

模块结束

对我来说,我看到你在模块化级别上声明了很多东西,但对于这样一个简单的结束函数,我真的看不出有什么意义。

考虑一种更简单、更可重用的方法,例如:

Option Strict On
Option Explicit On
Option Infer Off
Module Module1
    Public Function CropBitmap(bm As Bitmap, cropRect As Rectangle) As Image
        Dim CropImage As New Bitmap(cropRect.Width, cropRect.Height)
        Using g As Graphics = Graphics.FromImage(CropImage)
            g.DrawImage(bm, New Rectangle(0, 0, cropRect.Width, cropRect.Height), cropRect, GraphicsUnit.Pixel)
        End Using
        Return CropImage
    End Function
End Module

在您的代码中,注释一些行并使用 Paul Ishak 的函数:

Dim bitmap1 As Bitmap
Dim cropBitmap As Bitmap
Dim cropX As Integer
Dim cropY As Integer
Dim cropWidth As Integer
Dim cropHeight As Integer
Dim path As String = "C:\ProSys\Images\Test\Temp\"
Dim StrFileName As String = "1"
Dim PicBitmap As Bitmap

Public Sub Crop()

Dim imageAttr1 As New ImageAttributes()
imageAttr1.SetGamma(2.2F)
bitmap1 = System.Drawing.Bitmap.FromFile(path & StrFileName & ".bmp")
Try

    PicBitmap = bitmap1 'CType(Bitmap.FromStream(MyImage), Bitmap)
    OKTickets.ImgTicket.SizeMode = PictureBoxSizeMode.CenterImage
    If bitmap1 IsNot Nothing Then
        bitmap1.RotateFlip(RotateFlipType.Rotate270FlipNone)
        OKTickets.ImgTicket.Image = bitmap1
    End If

    cropX = 2700
    cropY = 600
    cropWidth = 1200 'PictureBox1.Width
    cropHeight = 500 'PictureBox1.Height

    'a rectangle to set the location and size from the source image
    Dim rect As Rectangle = New Rectangle(cropX, cropY, cropWidth, cropHeight)

    'Dim bit As Bitmap = New Bitmap(OKTickets.ImgTicket.Image, OKTickets.ImgTicket.Width, OKTickets.ImgTicket.Height)

    'create a new bitmap with the width/height values that were specified.
    'cropBitmap = New Bitmap(cropWidth, cropHeight)

    'a new Graphics object that will draw on the cropBitmap
    'Dim g As Graphics = Graphics.FromImage(cropBitmap)

    'draw the portion of the image that you supplied cropping values for.
    'g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel)
    'g.DrawImage(cropBitmap, rect, 0, 0, OKTickets.ImgTicket.Width, OKTickets.ImgTicket.Height, GraphicsUnit.Pixel, imageAttr1)
    OKTickets.ImgTicket.Image = cropBitmap(bitmap1, rect)

Catch ex As System.IO.FileNotFoundException
    MessageBox.Show("There was an error. Check the path to the bitmap.")
End Try

End Sub