Results 1 to 11 of 11

Thread: Printing a Picture Box (non standard questions) [surely RESOLVED]

  1. #1

    Thread Starter
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Printing a Picture Box (non standard questions) [surely RESOLVED]

    I want to print a picture box using PainPicture but I have a few questions I could not find answered on the forum.

    1. What if the Picture Box is larger than the form it is on (a part of it is cut off)? Will it still print the whole picture?

    2. If I have labes with text (captions) embeded in the picture box along with the picture, will they be printed too? If not, how can that be achieved?
    Last edited by baja_yu; Sep 7th, 2004 at 12:55 AM.

  2. #2
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    1. If AutoRedraw property of PictureBox is true then it will print whole picture.
    2. Use Form1.PrintForm method

  3. #3

    Thread Starter
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989
    Originally posted by Deepak Sakpal
    1. If AutoRedraw property of PictureBox is true then it will print whole picture.
    2. Use Form1.PrintForm method
    Just to say that I dont have a printer here so I cant test myself to find out the answers.

    Regarding the answer to number 2: But I only want to print the contents of the Picture box, not the entire form. And wont PrintForm cut of the image if the picturebox can't fit the form?

  4. #4
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    i suggested the simplest method

    anyways here you go
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SendMessage Lib "user32.dll" Alias _
    4.    "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
    5.    ByVal wParam As Long, ByVal lParam As Long) As Long
    6.  
    7. Private Const WM_PAINT = &HF
    8. Private Const WM_PRINT = &H317
    9. Private Const PRF_CLIENT = &H4&    ' Draw the window's client area
    10. Private Const PRF_CHILDREN = &H10& ' Draw all visible child
    11. Private Const PRF_OWNED = &H20&    ' Draw all owned windows
    12.  
    13. 'Note - Box could also be a Form, if desired.
    14. Public Sub PrintPictureBox(Box As PictureBox, _
    15.                         Optional X As Single = 0, _
    16.                         Optional Y As Single = 0)
    17. Dim rv As Long
    18. Dim ar As Boolean
    19.  
    20.     On Error GoTo Exit_Sub
    21.  
    22.     With Box
    23.         'Save ReDraw value
    24.         ar = .AutoRedraw
    25.  
    26.         'Set persistance
    27.         .AutoRedraw = True
    28.    
    29.         'Wake up printer
    30.         Picture2.Print
    31.    
    32.         'Draw controls to picture box
    33.         rv = SendMessage(.hwnd, WM_PAINT, .hDC, 0)
    34.         rv = SendMessage(.hwnd, WM_PRINT, .hDC, _
    35.             PRF_CHILDREN Or PRF_CLIENT Or PRF_OWNED)
    36.    
    37.         'Refresh image to picture property
    38.         .Picture = .Image
    39.    
    40.         'Copy picture to Printer
    41.         Picture2.PaintPicture .Picture, X, Y
    42.    
    43.         'Restore backcolor  (Re-load picture if picture was used)
    44.         Box.Line (0, 0)-(.ScaleWidth, .ScaleHeight), .BackColor, BF
    45.    
    46.         'Restore ReDraw
    47.         .AutoRedraw = ar
    48.     End With
    49.  
    50. Exit_Sub:
    51.     If Err.Number Then MsgBox Err.Description, vbOKOnly, "Printer Error!"
    52.  
    53. End Sub
    54.  
    55.  
    56. 'USAGE - Add two picture boxes(Picture1,Picture2) and a command button (Command1) to a new form.
    57. '        Add various controls to the picture box.
    58. '        Run this code, and press the button to print.
    59. Private Sub Command1_Click()
    60.     PrintPictureBox Picture1, 1000, 1000
    61. End Sub

  5. #5

    Thread Starter
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989
    Thanks, I will try it out tomorrow when I get to a printer.

  6. #6
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    no need of printer

    start a new project, paste this code & boom.

  7. #7

    Thread Starter
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989
    Oh.... guess I didnt read the code right away.... so this does not send to the printer, just adds all the stuff to the Picture2 picture box.

    So I need to add the Printing part... no problem... That I know how...

  8. #8
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    u can just replace Picture2 with Printer.

  9. #9

    Thread Starter
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989
    Ok.. one more:

    PrintPictureBox Picture1, 1000, 1000

    This adds an offset to the image. I guess you added this for the margin so the image does not get cut on the printer (as the printer cant print all the way to the edges of the paper).
    If that is the case I will sett this to zero as I have compensated for that already in the picture itself.

  10. #10
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    Originally posted by baja_yu
    PrintPictureBox Picture1, 1000, 1000

    This adds an offset to the image. I guess you added this for the margin so the image does not get cut on the printer (as the printer cant print all the way to the edges of the paper).
    If that is the case I will sett this to zero as I have compensated for that already in the picture itself.
    Ur guess is absolutely correct.

  11. #11

    Thread Starter
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989
    Ok. 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