|
-
Nov 11th, 2002, 07:06 AM
#1
Thread Starter
New Member
Printing contents of a picture box?
Hi, I've written a graphing program in VB which uses BitBlt to draw graphs/axis onto a picturebox (well, the picturebox's DC).
However, I'm wondering what code I need to print the contents of this picturebox as I can't seem to get ``normal'' print code to work -- prints out blank picturebox etc.
I guess you have to bitblt the picbox's DC onto a printer object or something (as you can see I have no idea what I'm talking about). Any help appreciated
-
Nov 11th, 2002, 08:07 AM
#2
Try this
VB Code:
Private Sub PrintPictureToFitPage(Prn As Printer, Pic As Picture)
Dim PicRatio As Double
Dim PrnWidth As Double
Dim PrnHeight As Double
Dim PrnRatio As Double
Dim PrnPicWidth As Double
Dim PrnPicHeight As Double
Const vbHiMetric As Integer = 8 '
' Determine if picture should be printed in landscape
' or portrait and set the orientation.'
If Pic.Height >= Pic.Width Then
Prn.Orientation = vbPRORPortrait 'Taller than wide
Else
Prn.Orientation = vbPRORLandscape 'Wider than tall
End If '
' Calculate device independent Width to Height ratio for picture.'
PicRatio = Pic.Width / Pic.Height '
' Calculate the dimentions of the printable area in HiMetric.'
With Prn
PrnWidth = .ScaleX(.ScaleWidth, .ScaleMode, vbHiMetric)
PrnHeight = .ScaleY(.ScaleHeight, .ScaleMode, vbHiMetric)
End With '
' Calculate device independent Width to Height ratio for printer.'
PrnRatio = PrnWidth / PrnHeight '
' Scale the output to the printable area.'
If PicRatio >= PrnRatio Then '
' Scale picture to fit full width of printable area. '
PrnPicWidth = Prn.ScaleX(PrnWidth, vbHiMetric, Prn.ScaleMode)
PrnPicHeight = Prn.ScaleY(PrnWidth / PicRatio, vbHiMetric, Prn.ScaleMode)
Else
' Scale picture to fit full height of printable area. '
PrnPicHeight = Prn.ScaleY(PrnHeight, vbHiMetric, Prn.ScaleMode)
PrnPicWidth = Prn.ScaleX(PrnHeight * PicRatio, vbHiMetric, Prn.ScaleMode)
End If '
' Print the picture using the PaintPicture method.'
Call Prn.PaintPicture(Pic, 0, 0, PrnPicWidth, PrnPicHeight)
End Sub
Private Sub cmdPrintPicture_Click()
Call PrintPictureToFitPage(Printer, Picture1.Picture)
Printer.EndDoc
End Sub
-
Nov 11th, 2002, 10:23 AM
#3
Lively Member
Not sure if you have it solved already but . . .
If you take another form, or message box and drag it around and over your picture box (at run time) after the graph has been drawn. Will it remove the graph (sorta like acid trails )? or does the graph stay?
Reason: I ran across this a bit ago, and don't have VB in front of me right now. But I would use blt to paint a picture I had just converted to black and white into a picture box, and then try to save or print it; however, it wouldn't. I also found that dragging another form over it would erase the areas the form touched. There was a setting on the picture box's properties area that changed this, I want to say autoredraw but I am not certain. Anyhow with that setting it worked, won't get into the technical stuff that it does (cause I am not an expert with picture boxes) hehehe so will leave that up to the others.
-
Nov 12th, 2002, 02:40 AM
#4
Thread Starter
New Member
Thanks for the help but that code doesn't work for this picture box. The picture property of the the picture box isn't "valid" for this one -- nothing has been drawn to the "picture" -- only to the DC. I need to Bitblt to the picture object or something...
TheVoid -- yes it does leave "acid trails" -- but I have put in some code which redraws the graph image from a memory DC when the form is repainted.
Still looking for some print code, thanks
-
Nov 12th, 2002, 08:34 AM
#5
Lively Member
What error does hack's code give you? Or does it just not print? Am sure it should work just have to figure out what isn't set up right.
Here is another work around if you are in a rush. Of course for this to work it kinda helps to have the picture box on a form alone. Basically it should just resize the form to the picture box and then print everything on the form. Not the prefered method but should work.
VB Code:
'This is code to make form match size of the image and then print the image.
fForm.Top = iImage.Top
fForm.Left = iImage.Left
fForm.Width = iImage.Width
fForm.Height = iImage.Height
'prints the resized to the size of the picture form.
fForm.PrintForm
I don't use picture boxes alot, but with the image in the DC I wonder if it considers it on the picture box. If not that is probably why it prints the blank picture box. *shruggs* ok its too early, i need to stop reading these boards at 8am.
-
Dec 6th, 2002, 12:17 PM
#6
Addicted Member
Hey Hack, I was trying out this code for one of the programs that I had recently written. I keep getting an overflow error here:
VB Code:
PicRatio = Pic.Width / Pic.Height
because no values are being sent to the Pic.Height and Pic.Width attributes. When I pause my program and query the machine in the Immediate Window I get values if I try Picture1.Height or Picture1.Width. But apparently when I click the print button the values aren't being sent to the function. Any ideas?
Gary
Place Your VBForums Ad Here
-
Dec 28th, 2002, 07:56 PM
#7
New Member
Hi Bond ,
I also encounter the similar problem with you. I can't print you the picbox in the form nicely. The appearance of picbox is form location dependant. If i move the picbox to coordinate 0,0 of form then it can print out nicely. But I need to print it out at the center of form.
So did you solved your problem..? Could you kindly tell me how to solved it. Below is my printing code:
Private Sub cmdPrint_Click()
Dim intNumCopies As Integer, inti As Integer
'Assume cancel is set to true
On Error GoTo dbErrHandler 'Jump to command line named dbErrHandler
'Display the Print Dialog box
cdlPrint.ShowPrinter
'Get user-selected values from the dialog box
intNumCopies = cdlPrint.Copies
'Print as many copies as required
For inti = 1 To intNumCopies
PrintForm
Next inti
Exit Sub
dbErrHandler:
'User press Cancel Button
Exit Sub
End Sub
Anybody know, please help me. Thanks!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|