Link to home
Start Free TrialLog in
Avatar of picsnet
picsnet

asked on

How to print an Image

Hello, I have a windows service with a tcp listener that receives order numbers and then retreives a report over a web service as an image... It can be anything... Png, Tiff, whatever.  Anyways I get the image as a byte array.  

I want to send that image to the printer in a nice easy elegant way.  If I just send the byte array to the printer using sockets... I get nothing but garbage.  I don't know really understand or know how printing works at anything more than the highest levels.  Here is my code where I'm ready to print.  The part where i was using sockets is commented out.  I have another part there where I create an Image object from the byte array.  

Please shed some light for me on a nice way to send this Image object or byte array to the printer.  I don't think importing system.windows.forms and using a picturebox is a very elegant way to go about it though it'd probably be simplest.  

 If objBytes.Length > 0 Then

          Dim objStream As New MemoryStream(objBytes)
          Dim objImage As Image = Image.FromStream(objStream)

          'objSocket = New Sockets.Socket(Sockets.AddressFamily.InterNetwork, Sockets.SocketType.Stream, Sockets.ProtocolType.Tcp)
          'objSocket.Connect(objEndPoint)

          'If objSocket.Connected Then
          '  objSocket.SendTo(objBytes, 0, objBytes.Length, Sockets.SocketFlags.None, objEndPoint)
          'End If

        End If

Thanks, Tony
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi picsnet;

This is a quick and dirty way to print the image as long as the image does not exceed the bounds of the page on the default printer.

    ' Class level variables
    Private PrtDoc As PrintDocument
    Private objImage As Image


        Dim ImageName As String = "The Name of the Image"
        Dim objStream As New MemoryStream(objBytes)
        Dim objImage As Image = Image.FromStream(objStream)
        ' Create and print the document
        PrtDoc = New PrintDocument
        PrtDoc.DocumentName = ImageName
        AddHandler PrtDoc.PrintPage, AddressOf PrtDoc_PrintPage
        PrtDoc.Print()
        RemoveHandler PrtDoc.PrintPage, AddressOf PrtDoc_PrintPage


    Private Sub PrtDoc_PrintPage(ByVal sender As Object, _
        ByVal e As PrintPageEventArgs)

        Dim g As Graphics = e.Graphics

        ' The PointF is the location on the page where to place the
        ' upper left of the image on the page. Page 0, 0 is upper
        ' left corner of the page
        g.DrawImage(objImage, New PointF(0, 0))


    End Sub

Hope this gets you on the right track.

Fernando
Avatar of picsnet
picsnet

ASKER

I found code that looked pretty much exactly like that about an hour after I posted.  The image is a report.  I don't have much control on whether the report is 1 page... 2 pages... etc.  It depends on the data.  What happens if it is more than 1 page.  How do I correct this?

Tony
This event handler will be called each time a page needs to be printed. You will need to set the e.HasMorePages to reflect if you have more to print or not.

  Private Sub PrtDoc_PrintPage(ByVal sender As Object, _
        ByVal e As PrintPageEventArgs)

        Dim g As Graphics = e.Graphics

        g.DrawImage(objImage, New PointF(0, 0))

        ' See if you have more to print, if so set to true
        If I_Have_More_To_Print then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If

    End Sub


Fernando
Avatar of picsnet

ASKER

I know about the e.hasmorepages variable.  I just don't really know how to make much sense of it though. You have to understand that I know nothing about this printing stuff.  It seems way too complicated.  I would think that the framework would take care of all this stuff.  How am I supposed to know whether or not I have more to print?  Aren't I going to have to split the image up into multiple parts and send each part to the print page method.  If I draw the image for each page it seems like it is just going to draw the whole image over and over again.  

I just can't believe that you just can't tell it to print and it'll print.  

Tony
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of picsnet

ASKER

Hi, I went ahead and accepted cause you've been tons of help.  I haven't looked into how hard it is yet... but I thought I'd go ahead and ask... You wouldn't happen to have any code handy that cuts an image up would you??

tony
No I do not do much Image processing, sorry :=(