how to create a grid with cells with images that is dynamic and printable.

Kairo Martins 21 Reputation points
2021-04-30T17:57:48.9+00:00

good afternoon like how can i create a grid with each of the cells having an image that would be a qr code already generated. that by number define the amount of cells and be printable (vb.net)

if the text has a stranger forgive me use the google translator.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,581 questions
{count} votes

Accepted answer
  1. Xingyu Zhao-MSFT 5,356 Reputation points
    2021-05-14T08:38:21.923+00:00

    Hi @Kairo Martins ,
    Leave preview controls(DataGridView or ListView), you can print images directly.

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            ImageList1.ImageSize = New Size(PictureBox1.Width, PictureBox1.Height)  
        End Sub  
      
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click              
            ImageList1.Images.Add(PictureBox1.BackgroundImage)  
            Label1.Text = ImageList1.Images.Count  
        End Sub  
        Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage  
            Dim picCount As Integer = ImageList1.Images.Count  
            If picCount > 0 Then  
                Dim rect As Rectangle = e.MarginBounds  
                Dim width As Single = 1.0F * rect.Width / 3  
                Dim height As Single = width  
                Dim columns = 3  
                Dim ypos = 0  
                Dim index As Integer = 0  
                While ypos + height < 1.0F * rect.Height  
                    For c As Integer = 0 To columns - 1  
      
                        Dim pRect As RectangleF = New RectangleF(c * width, ypos, width, height)  
                        e.Graphics.DrawImage(ImageList1.Images(index), pRect)  
                        index += 1  
                        If index > ImageList1.Images.Count - 1 Then  
                            Return  
                        End If  
                    Next  
                    ypos = ypos + height  
                End While  
                e.HasMorePages = True  
            Else  
                MessageBox.Show("You must generate qr code first")  
            End If  
        End Sub  
            
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
            Dim qrGenerator As QRCodeGenerator = New QRCodeGenerator()  
            Dim qrCodeData As QRCodeData = qrGenerator.CreateQrCode("Hello world", QRCodeGenerator.ECCLevel.Q)  
            Dim qrCode As QRCode = New QRCode(qrCodeData)  
            Dim qrCodeImage As Bitmap = qrCode.GetGraphic(20)  
            PictureBox1.BackgroundImage = qrCodeImage  
        End Sub  
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click  
      
            Dim printDialog As PrintDialog = New PrintDialog()  
            printDialog.Document = PrintDocument1  
            If printDialog.ShowDialog() = DialogResult.OK Then  
                PrintDocument1.Print()  
            End If  
        End Sub  
    

    Result of the test.
    96682-1.png

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


6 additional answers

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,356 Reputation points
    2021-05-12T07:14:23.613+00:00

    Hi @Kairo Martins ,
    Thanks for your feedback.
    It seems that you need to use listview to do it.
    Here's an whole code you can refer to.

        Private index As Integer = 0  
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            ImageList1.ImageSize = New Size(50, 50) ' You can change the size as you want.  
        End Sub  
      
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click  
            ' Add ImageList and ListView to your project.  
            ImageList1.Images.Add(PictureBox1.BackgroundImage)  
            ListView1.LargeImageList = ImageList1  
      
            Dim lst As ListViewItem = New ListViewItem()  
            lst.Text = index + 1  
            lst.ImageIndex = index  
            ListView1.Items.Add(lst)  
            index += 1  
        End Sub  
        Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage  
            Dim bitmap As Bitmap = New Bitmap(ListView1.Width, ListView1.Height)  
            ListView1.DrawToBitmap(bitmap, ListView1.ClientRectangle)  
            e.Graphics.DrawImage(bitmap, New Point(ListView1.Width, ListView1.Height))  
        End Sub  
        ...  
    

    Result of my test.
    95945-1.png
    Besides, if you need more printing capabilities, see : Easily turn a ListView into a nicely printed report, complete with print preview
    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Kairo Martins 21 Reputation points
    2021-05-15T12:48:03.983+00:00

    @Xingyu Zhao-MSFT when it displays a page print dialog and when the number of codes exceeds 10. it skips pages indefinitely

    0 comments No comments