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

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

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.
1,825 questions
{count} votes

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

    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. answered 2021-05-06T03:21:27.443+00:00
    Xingyu Zhao-MSFT 5,326 Reputation points

    Hi @Kairo Martins ,
    You can consider using DataGridView control to do it.
    First of all, you need to generate QR codes,
    Then display the QR Code image in DataGridView,
    Finally, print image from DataGridview(the code in the reference is C#, and you can convert it to vb. net).

    Hope them 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. answered 2021-05-10T03:18:19.707+00:00
    Xingyu Zhao-MSFT 5,326 Reputation points

    Hi @Kairo Martins ,
    The following code works for me:

            Dim ColImage As New DataGridViewImageColumn  
            Dim Img As New DataGridViewImageCell  
            ColImage.Name = "ColImg"  
            ColImage.HeaderText = "Your Image!"  
            DataGridView1.Columns.Add(ColImage)  
            Img.Value = Qrprevew.BackgroundImage  
            DataGridView1(0, 0) = Img  
    

    In order to add images in multiple columns in DataGridView, you can refer to the following code.

            Dim rowCount As Integer = ...  
            Dim colCount As Integer = ...  
            ' Add columns to DataGridView  
            For i As Integer = 0 To colCount - 1  
                Dim ColImage As New DataGridViewImageColumn  
                ColImage.Name = "ColImg" + i.ToString  
                ColImage.HeaderText = "Your Image" + i.ToString  
                DataGridView1.Columns.Add(ColImage)  
            Next  
            ' Add rows to DataGridView  
            For i As Integer = 0 To rowCount - 1  
                DataGridView1.Rows.Add()  
            Next  
            ' Assign values to DataGridView  
            For i As Integer = 0 To colCount - 1  
                For j As Integer = 0 To rowCount - 1  
                    Dim Img As New DataGridViewImageCell  
                    Img.Value = ...  
                    DataGridView1(i, j) = Img  
                Next  
            Next  
    

    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.


  3. answered 2021-05-11T13:13:34.947+00:00
    Kairo Martins 21 Reputation points

    @Xingyu Zhao-MSFT

    here

    Private Sub Button21_Click(sender As Object, e As EventArgs) Handles Button21.Click  
    
    
        Dim rowCount As Integer = 3  
        Dim colCount As Integer = 5  
        ' Add columns to DataGridView  
        For i As Integer = 0 To colCount - 1  
            Dim ColImage As New DataGridViewImageColumn  
            ColImage.Name = "ColImg" + i.ToString  
            ColImage.HeaderText = "Your Image" + i.ToString  
            DataGridView1.Columns.Add(ColImage)  
        Next  
        ' Add rows to DataGridView  
        For i As Integer = 0 To rowCount - 1  
            DataGridView1.Rows.Add()  
        Next  
        ' Assign values to DataGridView  
        For i As Integer = 0 To colCount - 1  
            For j As Integer = 0 To rowCount - 1  
                Dim Img As New DataGridViewImageCell  
                Img.Value = Qrprevew.BackgroundImage  
                DataGridView1(i, j) = Img  
            Next  
        Next  
    
    End Sub  
    
    Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage  
    
        Dim rowIndex As Integer = DataGridView1.CurrentCell.RowIndex  
        Dim bm As Bitmap = CType(DataGridView1.Rows(rowIndex).Cells(0).Value, Bitmap)  
        e.Graphics.DrawImage(bm, 0, 0)  
    
    
    End Sub  
    
    Private Sub Button22_Click(sender As Object, e As EventArgs) Handles Button22.Click  
        Dim printDialog As PrintDialog = New PrintDialog()  
        printDialog.Document = PrintDocument1  
    
        If printDialog.ShowDialog() = DialogResult.OK Then  
            PrintDocument1.Print()  
        End If  
    End Sub  
    
    No comments

  4. answered 2021-05-11T22:20:06.073+00:00
    Kairo Martins 21 Reputation points

    @Xingyu Zhao-MSFT
    Good night, I made a prototype to help you help me, I'll put the link down here.

    it has the same error described above.

    I hope it will be of great help.

    prototype repository.

    https://github.com/KairoMartins18/Qrencoder_exempleediton

    No comments