在vb.net中打印多个图像

时间:2013-05-18 05:05:58

标签: vb.net vb.net-2010

在VB.NET中,我需要以表格格式排列条形码的多个图像。现在我要做的是,创建条形码并将它们添加到新的图片框中。这些图片框添加在我在运行时在表单上创建的面板上并打印该面板(图片框在4x9表中)。

但是,当我需要打印更多36个条形码时,这个想法不起作用。

所以,请建议我对我的代码或任何其他方式做一些改进。


对不起,这是用于生成图像并将其添加到面板的代码..

''' Method for create bar code images with a loop and adding them to the panel by picture box...    
Private Function GetBarcodeText(RowId As Guid)  
        Dim BarcodeValue As StringBuilder = New StringBuilder(96)  
        Dim temp As New StringBuilder  
        Dim data As String  
        Dim param = New SqlParameter()  
        Dim imageNo As Integer = 0  
        Dim colorValue As String = ""  
        Dim scaleValue As String = ""  

    '' Adding the panel on the form which is dynamically created
    Me.Controls.Add(outputPanel)

    '' Setting the Initial size for the panel
    outputPanel.Size = New Point(794, 112)
    outputPanel.Name = "outputPanel"
    outputPanel.BackColor = Drawing.Color.White
    param.ParameterName = "@RowId"
    param.Value = RowId
    param.SqlDbType = SqlDbType.UniqueIdentifier

    ' Get the particular row of data from database
    dt = objStockProvider.GetBarcodeDetails(param)

    ' GET colour code
    Dim color As String = dt.Rows(0)("COLOUR").ToString()

    Dim countColors As Integer = 0
    ' Get the color code numbers
    param.ParameterName = "@Dscale"
    param.Value = dgvViewTickets.CurrentRow.Cells("SCALE").Value.ToString()
    countColors = objStockProvider.CountColorCodes(param)

    For i = 1 To countColors
        For j As Integer = 1 + ((12 / countColors) * (i - 1)) To (12 / countColors) * i
            If dt.Rows(0)("S" + j.ToString()) <> 0 Then
                Dim totalTicketsForASize As Integer
                totalTicketsForASize = dt.Rows(0)("S" + j.ToString())
                For k As Integer = 1 To totalTicketsForASize                     
                    ' Set Bar code value which has to create
                    BarcodeValue = "123456789012"
                    ' Create Barcode Image for given value
                    Dim image = GetBarcodeImage(BarcodeValue, colorValue, scaleValue)
                    If image IsNot Nothing Then
                        '' Create picture box to contain generated Image.
                        Dim pcbImage As New PictureBox
                        pcbImage.Width = W
                        pcbImage.Height = H
                        pcbImage.Image = image
                        pcbImage.Location = New Point(X, Y)
                        imageNo += 1
                        If imageNo Mod 4 = 0 Then
                            X = 15
                            Y += H
                            outputPanel.Height += H
                        Else
                            X += W
                            Y = Y
                        End If
                        pcbImage.Visible = True
                        '' Adding picture box to panel
                        outputPanel.Controls.Add(pcbImage)
                    End If
                Next
            End If
        Next
        color = color.Substring(color.IndexOf(",") + 1, color.Length - color.IndexOf(",") - 1)
    Next
    PrintGeneratedTickets()
End Function

现在,我按照以下方法打印面板:

Private Sub PrintGeneratedTickets()

    bmp = New Bitmap(outputPanel.DisplayRectangle.Width, outputPanel.DisplayRectangle.Height)
    Dim G As Graphics = Graphics.FromImage(bmp)

    G.DrawRectangle(Pens.White, New Rectangle(0, 0, Me.outputPanel.DisplayRectangle.Width, Me.outputPanel.DisplayRectangle.Height))
    Dim Hdc As IntPtr = G.GetHdc()
    SendMessage(outputPanel.Handle, WM_PRINT, Hdc, DrawingOptions.PRF_OWNED Or DrawingOptions.PRF_CHILDREN Or DrawingOptions.PRF_CLIENT Or DrawingOptions.PRF_NONCLIENT)
    G.ReleaseHdc(Hdc)
    pndocument.DocumentName = bmp.ToString()

    Dim previewmode As New PrintPreviewDialog
    previewmode.Document = pndocument
    previewmode.WindowState = FormWindowState.Maximized
    previewmode.PrintPreviewControl.Zoom = 1

    pndocument.DefaultPageSettings.Margins.Top = 10
    pndocument.DefaultPageSettings.Margins.Bottom = 30
    pndocument.DefaultPageSettings.Margins.Left = 16
    pndocument.DefaultPageSettings.Margins.Right = 16
    pndocument.DefaultPageSettings.Landscape = False
    ' Set other properties.
    previewmode.PrintPreviewControl.Columns = 4
    previewmode.PrintPreviewControl.Rows = 9

    previewmode.ShowDialog()

    Dim file As String = DateTime.Now.ToString()
    file = Path.GetFullPath("D:\Bar Codes\" + file.Replace("/", "-").Replace(":", ".") + ".bmp")
    bmp.Save(file)
    G.Dispose()
    outputPanel.Controls.Clear()

End Sub

此代码工作正常但我需要做的是修复每页图像的数量(4x9)。但是,当我尝试创建更多内容时,所有内容都在压缩大小的单个页面上打印。

同样在尝试重新运行代码时,它在预览中没有显示任何内容。

有些正文请建议修改代码,以便我可以重新打印门票并使用分页超过36张图片。

1 个答案:

答案 0 :(得分:1)

好吧,在面板上打印图像并不是一个好主意。我更换了面板并创建了一个图像数组,并直接使用了打印文档,并在将图像排列后进行打印。

感谢。