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

问题描述:

我有一个小程序,用于扫描文档并读取条形码(然后将其分割为不同的目录和文件名)。这一切工作正常,但我现在需要扩大该计划一点。我需要添加另外几个表单来完成类似的事情,因此我将完成工作的代码移到了扫描图像中(即裁剪,旋转,getbarcode等)到一个模块中。将代码移动到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 

如果这是在同一个班的形式,那么它做什么是应该做的,裁剪扫描的图像和将它显示在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 

前端模块

+0

你能用你的模块发布代码?因为我不明白为什么你从Form(类)移动到模块.. anf你可以检查Dim g As Graphics = Graphics.FromImage(cropBitmap)cropBitmap什么Widht和Height有..?也许你foget一些大小varibale和图像是0 px – CristiC777

+0

编辑我的问题,包括模块代码 – Gazza

对我来说,我看到你有很多在模块化层次上声明的东西,而且我并没有真正明白这一点一个简单的结束功能。

考虑一个更简单,更可重用的方式像这样:

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 
+0

我怎么会称这个功能的图像,从主窗体扫描和保存。 – Gazza

+0

croppedimg = Cropbitmap(sourceImg,CropRect) –

在你的代码,注释部分线路,并使用保罗·伊沙克的功能:

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