Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem printing a picture box

Status
Not open for further replies.
Oct 5, 1999
105
GB
I have a picture box which I want to use for a 1 page report which I can the preview. However I am having problems trying to subsequently print the picture box.

E.g.

With a form containing one command button and one picture box and the following code
Code:
Private Sub Command1_Click()
    Picture1.AutoRedraw = True
    Picture1.CurrentX = 100
    Picture1.CurrentY = 100
    Picture1.Print "Text"
    Printer.PaintPicture Picture1.Picture, 0, 0
    Printer.EndDoc
End Sub
when I run it I get a run time error 481 "Invalid Picture" on the PaintPicture line.

Any ideas.

Also is there an easy way of doing the preview or do I have to put my own scroll bars and zoom in/out code in.

Thanks
 
But you may consider doing it like;

Private Sub CbPreview_Click()
DoReport Picture1
End Sub

Private Sub CbPrint_Click()
DoReport Printer
End Sub

Private Sub DoReport(PrintDevice As Object)

If TypeOf PrintDevice Is PictureBox Then
PrintDevice.Width = Printer.ScaleWidth
PrintDevice.Height = Printer.ScaleHeight
End If

PrintDevice.ScaleMode = vbCentimeters
' '2cm left margin
PrintDevice.Scale (-2, 0)-(PrintDevice.ScaleWidth - 2, PrintDevice.ScaleHeight)

PrintDevice.CurrentX = 0
PrintDevice.CurrentY = 5

PrintDevice.Print "Hello World"

If TypeOf PrintDevice Is Printer Then
Printer.EndDoc
End If

End Sub
 
That will be;
Printer.PaintPicture Picture1.Image, 0, 0

Foolish me, I read the help text which says "Must be the Picture property of a Form or PictureBox".

Thanks for that.

Hugh, I see the logic for the example, but presumably if the report is a summary from a large database then is it worthwhile generating it twice?

>do I have to put my own scroll bars and zoom in/out code in
Yes.
Well , I thought I would have to. I just wondered if there was some obscure routine someone had developed for what is a fairly common task.


Thanks for all your help
 
>generating it twice?
That appears to indicate that your report is being generated on the fly as the database query proceeds. If that is lengthy then it may be better to retrieve the required data once from the query eg. into a UDT then generate the preview/ printed reports based upon its contents, so...

Private Sub DoReport(PrintDevice As Object,MyRec as QueryResult)

The problem you may find if you print the picture is that the printout will be limited to the resolution of the screen. If printout quality is an issue you will find that printing directly to the Printer is better.
 
>Foolish me, I read the help text which says "Must be the Picture property of a Form or PictureBox".

Indeed (although not comprehensive)- however at the point at which you try to print your PictureBox doesn't have a valid Picture ...

As well as the solution presented abaove, you can also fix this with

Picture1.Picture=Picture1.Image
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top