locked
printing a graphics buffer RRS feed

  • Question

  • I am using a picturebox and I draw graphics to it and everything works great. However I would like to explore the possibility of sending the contents to a printer. I use graphics (not a picture) to draw lines and things on a buffer and then show them on the picturebox. Now I know that it in itself is an image atleast I think it is but I feel as though I am out of my league here. Is there an easy way to send an image from a picturebox over to the printer?

    Thanks in advance,

    Les

    Monday, July 20, 2020 9:37 PM

Answers

  • Hi

    OK, here is the barest of code I could come up with. What ever Image is in your PictureBox would be sent directly to the attached Printer. This example needs a PictureBox1 and a Button1 on Form1, and a PrintDocument1. You don't need the drawing commands I included in the example as you already put an Image into the PictureBox.

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim myImage As New Bitmap(300, 200)
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    		' this just puts a drawn image into
    		' the PictureBox1
    		Dim g As Graphics = Graphics.FromImage(myImage)
    		g.DrawLine(New Pen(Color.Red, 3), 5, 5, 150, 100)
    		g.DrawLine(New Pen(Color.Blue, 2), 100, 5, 200, 120)
    		g.DrawRectangle(New Pen(Color.Green, 5), 5, 45, 150, 100)
    		PictureBox1.Image = myImage
    
    		' this sends direct to Printer 
    		PrintDocument1.Print()
    	End Sub
    	Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    		e.Graphics.DrawImage(PictureBox1.Image, New Point(0, 0))
    	End Sub
    End Class


    Regards Les, Livingston, Scotland



    • Edited by leshay Monday, July 20, 2020 11:35 PM
    • Marked as answer by Les2011 Tuesday, July 21, 2020 2:35 AM
    Monday, July 20, 2020 11:26 PM

All replies

  • Hi

    There is quite a bit to do when you want to Print stuff.

    Have you used PrintDocument and associated code previously?

    It isn't easy to describe the process, but, if needed, I could post

    a full example (quite a lot of code for the Printing past)

    For example:

    Here is an Image showing the Designer for a Printing example.

    You don't need the Dialogs if you are sending directly to a Printer, but you can use the PrintDocument to send to Printer. The default signature has a Graphics object that you can use to draw directly to the Printer. Here is an outline of the code blocks you could use.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      ' DIRECT PRINT
      ' calls PrintDocument1_BeginPrint
      PrintDocument1.Print()
    End Sub
    
    
    
    Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
    
    ' Pre-Print code here ..............
    ' then calls PrintDocument1_PrintPage
    
    End Sub
    
    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
    ' Prints from here ...........
    ' you can use e.Graphics to draw your Image here which
    ' will then go to Printer.
    
    End Sub


    Regards Les, Livingston, Scotland

    Monday, July 20, 2020 10:38 PM
  • Hi Les we meet again <S>

    I do have the image and just want to send it to the printer, hopefully that makes things more simple.  Is there still a lot of code to just print the image??  I mean I am not concerned with pages, margins etc.  I can control that I believe based on the size of my image but like I said I am out of my league here.  I just always thought that if all I want to do is send an image to the printer I was not expecting it to be complicating but I know you have plenty of experience and are familiar with this stuff.  I will see what else I can dig up regarding this and hopefully find an "OK" way of doing it.  As you know I respect your knowledge as always

    Thanks Les and stay safe

    Les

    Monday, July 20, 2020 11:15 PM
  • Hi

    OK, here is the barest of code I could come up with. What ever Image is in your PictureBox would be sent directly to the attached Printer. This example needs a PictureBox1 and a Button1 on Form1, and a PrintDocument1. You don't need the drawing commands I included in the example as you already put an Image into the PictureBox.

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim myImage As New Bitmap(300, 200)
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    		' this just puts a drawn image into
    		' the PictureBox1
    		Dim g As Graphics = Graphics.FromImage(myImage)
    		g.DrawLine(New Pen(Color.Red, 3), 5, 5, 150, 100)
    		g.DrawLine(New Pen(Color.Blue, 2), 100, 5, 200, 120)
    		g.DrawRectangle(New Pen(Color.Green, 5), 5, 45, 150, 100)
    		PictureBox1.Image = myImage
    
    		' this sends direct to Printer 
    		PrintDocument1.Print()
    	End Sub
    	Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    		e.Graphics.DrawImage(PictureBox1.Image, New Point(0, 0))
    	End Sub
    End Class


    Regards Les, Livingston, Scotland



    • Edited by leshay Monday, July 20, 2020 11:35 PM
    • Marked as answer by Les2011 Tuesday, July 21, 2020 2:35 AM
    Monday, July 20, 2020 11:26 PM
  • Hi Les,

    Thank you so much for the code snippet I will try it when I get home later this evening.

    Your the best

    Les

    Tuesday, July 21, 2020 12:38 AM
  • Hey Les what can I say that I haven't said before, that's exactly what I needed and provides me with  a good start into understanding this part of vb.net that I have never explored.  Thank you once again for the help and I will take it from here in the event I need to get more exotic.

    Les

    Tuesday, July 21, 2020 2:34 AM