Results 1 to 7 of 7

Thread: Printing contents of a picture box?

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    4

    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

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Try this

    VB Code:
    1. Private Sub PrintPictureToFitPage(Prn As Printer, Pic As Picture)
    2.  
    3. Dim PicRatio     As Double
    4. Dim PrnWidth     As Double
    5. Dim PrnHeight    As Double
    6. Dim PrnRatio     As Double
    7. Dim PrnPicWidth  As Double
    8. Dim PrnPicHeight As Double
    9. Const vbHiMetric As Integer = 8 '
    10. ' Determine if picture should be printed in landscape
    11. ' or portrait and set the orientation.'
    12. If Pic.Height >= Pic.Width Then
    13.     Prn.Orientation = vbPRORPortrait   'Taller than wide
    14. Else
    15.     Prn.Orientation = vbPRORLandscape  'Wider than tall
    16. End If '
    17. ' Calculate device independent Width to Height ratio for picture.'
    18. PicRatio = Pic.Width / Pic.Height '
    19. ' Calculate the dimentions of the printable area in HiMetric.'
    20. With Prn
    21.     PrnWidth = .ScaleX(.ScaleWidth, .ScaleMode, vbHiMetric)
    22.     PrnHeight = .ScaleY(.ScaleHeight, .ScaleMode, vbHiMetric)
    23. End With '
    24. ' Calculate device independent Width to Height ratio for printer.'
    25. PrnRatio = PrnWidth / PrnHeight '
    26. ' Scale the output to the printable area.'
    27. If PicRatio >= PrnRatio Then    '
    28.     ' Scale picture to fit full width of printable area.    '
    29.     PrnPicWidth = Prn.ScaleX(PrnWidth, vbHiMetric, Prn.ScaleMode)
    30.     PrnPicHeight = Prn.ScaleY(PrnWidth / PicRatio, vbHiMetric, Prn.ScaleMode)
    31. Else
    32.     ' Scale picture to fit full height of printable area.    '
    33.     PrnPicHeight = Prn.ScaleY(PrnHeight, vbHiMetric, Prn.ScaleMode)
    34.     PrnPicWidth = Prn.ScaleX(PrnHeight * PicRatio, vbHiMetric, Prn.ScaleMode)
    35. End If '
    36. ' Print the picture using the PaintPicture method.'
    37. Call Prn.PaintPicture(Pic, 0, 0, PrnPicWidth, PrnPicHeight)
    38. End Sub
    39.  
    40. Private Sub cmdPrintPicture_Click()  
    41. Call PrintPictureToFitPage(Printer, Picture1.Picture)
    42. Printer.EndDoc
    43. End Sub

  3. #3
    Lively Member
    Join Date
    Oct 2001
    Location
    Florida
    Posts
    98
    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.


  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    4
    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

  5. #5
    Lively Member
    Join Date
    Oct 2001
    Location
    Florida
    Posts
    98
    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:
    1. 'This is code to make form match size of the image and then print the image.
    2.     fForm.Top = iImage.Top
    3.     fForm.Left = iImage.Left
    4.     fForm.Width = iImage.Width
    5.     fForm.Height = iImage.Height
    6.    
    7.     'prints the resized to the size of the picture form.
    8.     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.

  6. #6
    Addicted Member run_GMoney's Avatar
    Join Date
    May 2002
    Location
    Detroit
    Posts
    186
    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:
    1. 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

  7. #7
    New Member
    Join Date
    Dec 2002
    Posts
    5

    Angry

    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
  •  



Click Here to Expand Forum to Full Width