question

KairoMartins-8632 avatar image
0 Votes"
KairoMartins-8632 asked KairoMartins-8632 answered

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

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.

dotnet-visual-basic
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @KairoMartins-8632 ,
According to your description, I have two questions to confirm with you.
What's the type of your application? WPF?
Could you provide more details about 'number define the amount of cells and be printable'? It will help us make a test.
We are waiting for your update.

0 Votes 0 ·

the application is Winforms.
page numbers = 2

0 Votes 0 ·
XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered KairoMartins-8632 commented

Hi @KairoMartins-8632 ,
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.



1.png (73.5 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

when it displays a page print dialog and when the number of codes exceeds 10. it skips pages indefinitely

0 Votes 0 ·
KairoMartins-8632 avatar image
0 Votes"
KairoMartins-8632 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered KairoMartins-8632 commented

Hi @KairoMartins-8632 ,
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.



1.png (73.5 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@XingyuZhao-MSFT


There is only one problem in the Code, it just centralizes the image on the sheet. How can I use the entire A4 sheet? and also that I can define the size of the sheet.

0 Votes 0 ·

Hi @KairoMartins-8632 ,
I have two questions to confirm with you.
How many qr codes do you want to print on each line?
Are there any restrictions on the length and width of each qr code?
We are waiting for your update

0 Votes 0 ·

three codes per line in rm three columns of three.
there are no restrictions and the length and width of the codes.

0 Votes 0 ·
KairoMartins-8632 avatar image
0 Votes"
KairoMartins-8632 answered

@XingyuZhao-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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

KairoMartins-8632 avatar image
0 Votes"
KairoMartins-8632 answered

@XingyuZhao-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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered XingyuZhao-MSFT commented

Hi @KairoMartins-8632 ,
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.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

As I am going to print all this the link you left, only works with 1 Qr code.

0 Votes 0 ·

Hi @KairoMartins-8632 ,
Could you provide the code you have used so far? It will help us analyze your problem.

0 Votes 0 ·
XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered KairoMartins-8632 edited

Hi @KairoMartins-8632 ,
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.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I followed the tutorial from end to end and adapting to my project. I'll put a code tweak here.


///// code
Private Sub Button21_Click(sender As Object, e As EventArgs) Handles Button21.Click
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.Rows(0).Cells.Add(Img)****
 End sub 

/// end code



--> DataGridView1.Rows(0).Cells.Add(Img)
System.InvalidOperationException: 'The collection already belongs to a DataGridView control. The operation is no longer valid. '


a great question. how do i define the amount of qrcodes and column in a sheet?











0 Votes 0 ·