Ryn

In my program I have a print routine that prints one or more graphics pages using the PrintPage event. This works fine with print preview, page setup and all the normal things. Now I want to add the capability to create and save a bitmap of the print image instead of printing to the printer. Clearly, all the code is in place to create the print image, I just need to save it. It does not make sense to me to make a bitmap from the screen. But how do I create a bitmap from the PrintPage event


Re: Visual Basic General need help saving a print image

ahmedilyas

you could print it to file Would that help It would be a document instead If so, set the "PrintToFile" property to true in the printersettings object and set the filename too!




Re: Visual Basic General need help saving a print image

Ryn

No that won't help, as I remember print to file. Normally print to file creates a postscript file which one can copy to a printer, in other words it already contains the printer instructions. I want to create a jpeg or similar file that can then be interted into docoment.




Re: Visual Basic General need help saving a print image

Bruno Yu - MSFT

Ryn,

According to your description, I will give you’re a piece of code sample and an article which will help you.

  • The following example prints the file that is specified through the command line to the default printer.

Public Class PrintingExample

Private printFont As Font

Private streamToPrint As StreamReader

Private Shared filePath As String

Public Sub New()

Printing()

End Sub

' The PrintPage event is raised for each page to be printed.

Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)

Dim linesPerPage As Single = 0

Dim yPos As Single = 0

Dim count As Integer = 0

Dim leftMargin As Single = ev.MarginBounds.Left

Dim topMargin As Single = ev.MarginBounds.Top

Dim line As String = Nothing

' Calculate the number of lines per page.

linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)

' Iterate over the file, printing each line.

While count < linesPerPage

line = streamToPrint.ReadLine()

If line Is Nothing Then

Exit While

End If

yPos = topMargin + count * printFont.GetHeight(ev.Graphics)

ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, _

yPos, New StringFormat())

count += 1

End While

' If more lines exist, print another page.

If Not (line Is Nothing) Then

ev.HasMorePages = True

Else

ev.HasMorePages = False

End If

End Sub

' Print the file.

Public Sub Printing()

Try

streamToPrint = New StreamReader(filePath)

Try

printFont = New Font("Arial", 10)

Dim pd As New PrintDocument()

AddHandler pd.PrintPage, AddressOf pd_PrintPage

' Print the document.

pd.Print()

Finally

streamToPrint.Close()

End Try

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

End Sub 'Printing

' This is the main entry point for the application.

Public Shared Sub Main()

Dim args() As String = System.Environment.GetCommandLineArgs()

Dim sampleName As String = args(0)

If args.Length <> 1 Then

Console.WriteLine("Usage: " & sampleName & " <file path>")

Return

End If

filePath = args(0)

End Sub

End Class

  • The following link will give you an example on print the Bitmap:

http://support.microsoft.com/kb/890894






Re: Visual Basic General need help saving a print image

Tall Dude

Hey everybody, He wants to capture the printpage

e.graphic images into reuseable images or bitmaps!

See:

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1032424&SiteID=1






Re: Visual Basic General need help saving a print image

leog naprad

I am struglling with the very same problem. But I need a solution in VC++. Could anyone help me with it

I am just restating the problem below:

My program creates a document to be printed on a printer. The document may contain text, geometrical shapes and images. In addition to the the physical copy of this document I also want to save a bitmap of what was printed.





Re: Visual Basic General need help saving a print image

Bruno Yu - MSFT

leog,

Could you please open a new thread Because this post is marked as answered. I am afraid few people would like to see this post any more. Thank you!






Re: Visual Basic General need help saving a print image

leog naprad

Done that in GDI section. Thanks.