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

时间:2015-04-28 13:53:44

标签: vb.net module

我有一个小程序,它扫描文档并读取条形码(然后将其分成不同的目录和文件名)。这一切都运行正常,但我现在需要稍微扩展一下程序。我需要添加另外两个做类似事情的表单,所以我将执行工作的代码移动到扫描图像(即crop,rotate,getbarcode等)到模块中。

一旦我这样做,我就不能让代码去做它正在做的事情。

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

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

如果它与表单位于同一类中,那么它会执行它应该执行的操作并裁剪扫描图像并将其显示在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

结束模块

2 个答案:

答案 0 :(得分:0)

对我而言,我发现你在模块化的层面上宣布了许多事情,而且我并没有真正看到这样一个简单的结束函数。

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

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

答案 1 :(得分:0)

在你的代码中,注释一些行并使用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