2013-05-18 109 views
0

在VB.NET中,我需要通過以表格格式排列多個條形碼圖像。現在我正在做的是創建條形碼並將它們添加到新圖片框中。這些圖片框被添加到我在運行時在窗體上創建的面板上並打印該面板(圖片框位於4x9表格中)。在vb.net中打印多個圖像

但是,當我需要打印更多的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

那麼,在面板上打印圖像不是一個好主意..我更換了面板並創建了一組圖像,並直接使用打印文檔並在排列圖像後進行打印。

謝謝。