Results 1 to 16 of 16

Thread: any one knows how to print a Picturebox with controls in it?

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2002
    Location
    Canada
    Posts
    50

    Question any one knows how to print a Picturebox with controls in it?

    Hi all
    I scroll the forum for a quick and simple answer about printing a picturebox with any control in it ,Well i was supprise and shock
    that there was nothing clear and easy to use .
    Any idea before i get sick?

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Try this...
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, _
    4.                                              ByVal x As Long, _
    5.                                              ByVal y As Long, _
    6.                                              ByVal nWidth As Long, _
    7.                                              ByVal nHeight As Long, _
    8.                                              ByVal hSrcDC As Long, _
    9.                                              ByVal xSrc As Long, _
    10.                                              ByVal ySrc As Long, _
    11.                                              ByVal dwRop As Long) _
    12.                                              As Long
    13.  
    14. Private Sub Command1_Click()
    15. Dim pWidth As Long, pHeight As Long
    16.  
    17.     pWidth = Picture1.Width \ Screen.TwipsPerPixelX
    18.     pHeight = Picture1.Height \ Screen.TwipsPerPixelY
    19.        
    20.     Printer.Print
    21.     Call BitBlt(Printer.hDC, 0, 0, pWidth, pHeight, Picture1.hDC, 0, 0, vbSrcCopy)
    22.     Printer.EndDoc
    23.    
    24. End Sub

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2002
    Location
    Canada
    Posts
    50
    Hi crptcblade

    It works The picturebox with the controls in it appear smaller then at the screen what do i need to change ,the picture scalemode or something in your code?

    Thanks i'm on the good way

  4. #4
    Hyperactive Member Matt-D's Avatar
    Join Date
    Nov 1999
    Location
    Mettmann, Germany
    Posts
    305

    Re: any one knows how to print a Picturebox with controls in it?

    Is anyone out there who could give me a hint how to make the dimensions of the printed result bigger? However, the code from crptcblade works great!

    Matt-D

  5. #5
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: any one knows how to print a Picturebox with controls in it?

    did you try StretchBlt?

  6. #6
    Hyperactive Member Matt-D's Avatar
    Join Date
    Nov 1999
    Location
    Mettmann, Germany
    Posts
    305

    Resolved

    Yes! That's it! Thanks for your hint

    Matt-D

  7. #7
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: any one knows how to print a Picturebox with controls in it?

    I was looking to do something like this but when the image is saved, shows the common dialog control covering the complete image. hm.

    Quote Originally Posted by crptcblade
    Try this...
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, _
    4.                                              ByVal x As Long, _
    5.                                              ByVal y As Long, _
    6.                                              ByVal nWidth As Long, _
    7.                                              ByVal nHeight As Long, _
    8.                                              ByVal hSrcDC As Long, _
    9.                                              ByVal xSrc As Long, _
    10.                                              ByVal ySrc As Long, _
    11.                                              ByVal dwRop As Long) _
    12.                                              As Long
    13.  
    14. Private Sub Command1_Click()
    15. Dim pWidth As Long, pHeight As Long
    16.  
    17.     pWidth = Picture1.Width \ Screen.TwipsPerPixelX
    18.     pHeight = Picture1.Height \ Screen.TwipsPerPixelY
    19.        
    20.     Printer.Print
    21.     Call BitBlt(Printer.hDC, 0, 0, pWidth, pHeight, Picture1.hDC, 0, 0, vbSrcCopy)
    22.     Printer.EndDoc
    23.    
    24. End Sub


  8. #8
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: any one knows how to print a Picturebox with controls in it?

    Add another Picturebox (invisible)
    VB Code:
    1. Private Sub Command1_Click()
    2.     With Picture2
    3.         .AutoRedraw = True
    4.         .Width = Picture1.Width
    5.         .Height = Picture1.Height
    6.     End With
    7.     Call BitBlt(Picture2.hDC, 0, 0, Picture1.Width, Picture1.Height, Picture1.hDC, 0, 0, vbSrcCopy)
    8.     CommonDialog1.Filter = "Images .BMP|*.BMP"
    9.     CommonDialog1.ShowSave
    10.     If CommonDialog1.FileName <> vbNullString Then SavePicture Picture2.Image, CommonDialog1.FileName
    11. End Sub
    This will work if Picture1 is 100% visible when you press the button, example: if the user moved the Form and the picture is half visible, only that half will be saved.
    Last edited by jcis; Aug 7th, 2006 at 02:34 PM.

  9. #9
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: any one knows how to print a Picturebox with controls in it?

    Picture2 is the hidden picturebox?
    Quote Originally Posted by jcis
    Add another Picturebox (invisible)
    VB Code:
    1. Private Sub Command1_Click()
    2.     With Picture2
    3.         .AutoRedraw = True
    4.         .Width = Picture1.Width
    5.         .Height = Picture1.Height
    6.     End With
    7.     Call BitBlt(Picture2.hDC, 0, 0, Picture1.Width, Picture1.Height, Picture1.hDC, 0, 0, vbSrcCopy)
    8.     CommonDialog1.Filter = "Images .BMP|*.BMP"
    9.     CommonDialog1.ShowSave
    10.     If CommonDialog1.FileName <> vbNullString Then SavePicture Picture2.Image, CommonDialog1.FileName
    11. End Sub
    This will work if Picture1 is 100% visible when you press the button, example: if the user moved the Form and the picture is half visible, only that half will be saved.

  10. #10
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: any one knows how to print a Picturebox with controls in it?

    Affirmative

  11. #11
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: any one knows how to print a Picturebox with controls in it?

    Quote Originally Posted by jcis
    Affirmative
    gotcha, will see what happens here

  12. #12
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: any one knows how to print a Picturebox with controls in it?

    jcis, the code works; however when i put the common dialog on frmMain, it disables my user control, so I put the code into another form and when I did that, it saves correctly but then that form is on top of the screen shot. any way i can take the other form away? this is what i mean (screen shot attached)
    Attached Images Attached Images  

  13. #13
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: any one knows how to print a Picturebox with controls in it?

    Could you upload your project?

  14. #14
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: any one knows how to print a Picturebox with controls in it?

    Quote Originally Posted by jcis
    Could you upload your project?
    i have found that if you add a common dialog control or an image list to the form that has a user control, the user control no longer works. the user control i am using is to resize and move the controls that are added to the picturebox container at run-time. that is why i have to put the common dialogs on different forms which suck badly. i also want to add icons to my menu editor but you cant do that if the image list isnt on the same form, ugh. complete project attached.

    frmMain is my main form, and frmSaveAsImage is where your code is located. I also put the BitBlt stuff in a module, just in case i need to use it again.

  15. #15
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: any one knows how to print a Picturebox with controls in it?

    Replace your cmdYes_Click with this
    VB Code:
    1. Private Sub cmdYes_Click()
    2.     'assign a directory name to a string variable
    3.     m_strScreenShotsDirectory = "Project Screen Shots"
    4.    
    5.     'check to see if the directory exists, if it doesnt, create it
    6.     If Len(Dir$(App.Path & "\" & m_strScreenShotsDirectory & "\", vbDirectory)) > 0 Then
    7.         'do nothing because the Screen Shots directory exists
    8.     Else
    9.         MkDir (App.Path & "\" & m_strScreenShotsDirectory & "\")
    10.     End If
    11.    
    12.     'auto redraw the hidden picturebox, set its height and width to the same as the work area
    13.     With frmMain.pbHidden
    14.         .AutoRedraw = True
    15.         .Width = frmMain.pbWorkArea.Width
    16.         .Height = frmMain.pbWorkArea.Height
    17.         .Appearance = 0
    18.         .BorderStyle = 1
    19.     End With
    20.    
    21.     'open the common dialog and allow the file to be saved as a BMP in the screen shots
    22.     'directory
    23.     With CD
    24.         .Filter = "Images (*.BMP)|*.BMP"
    25.         .InitDir = App.Path & "\" & m_strScreenShotsDirectory & "\"
    26.         .ShowSave
    27.     End With
    28.    
    29.     Me.Visible = False 'With this you wont capture this form
    30.     DoEvents           'With this you wont capture the common dialog window
    31.    
    32.     'Copy the contents of the work area to the hidden picturebox using BitBlt
    33.     With frmMain.pbWorkArea
    34.         Call BitBlt(frmMain.pbHidden.hDC, 0, 0, .Width, .Height, .hDC, 0, 0, vbSrcCopy)
    35.     End With
    36.    
    37.     If CD.FileName <> vbNullString Then SavePicture frmMain.pbHidden.Image, CD.FileName
    38.    
    39.     Unload Me
    40. End Sub
    By the way, nice splash screen fading

  16. #16
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: any one knows how to print a Picturebox with controls in it?

    yep, that worked perfectly. thanks jcis.

    question:
    i am using initdir to specify the default directory to open, sometimes the directory specified isnt the defaulted (sometimes opens somewhere else, but not often), how can i get it to always be the default?

    Quote Originally Posted by jcis
    Replace your cmdYes_Click with this
    VB Code:
    1. Private Sub cmdYes_Click()
    2.     'assign a directory name to a string variable
    3.     m_strScreenShotsDirectory = "Project Screen Shots"
    4.    
    5.     'check to see if the directory exists, if it doesnt, create it
    6.     If Len(Dir$(App.Path & "\" & m_strScreenShotsDirectory & "\", vbDirectory)) > 0 Then
    7.         'do nothing because the Screen Shots directory exists
    8.     Else
    9.         MkDir (App.Path & "\" & m_strScreenShotsDirectory & "\")
    10.     End If
    11.    
    12.     'auto redraw the hidden picturebox, set its height and width to the same as the work area
    13.     With frmMain.pbHidden
    14.         .AutoRedraw = True
    15.         .Width = frmMain.pbWorkArea.Width
    16.         .Height = frmMain.pbWorkArea.Height
    17.         .Appearance = 0
    18.         .BorderStyle = 1
    19.     End With
    20.    
    21.     'open the common dialog and allow the file to be saved as a BMP in the screen shots
    22.     'directory
    23.     With CD
    24.         .Filter = "Images (*.BMP)|*.BMP"
    25.         .InitDir = App.Path & "\" & m_strScreenShotsDirectory & "\"
    26.         .ShowSave
    27.     End With
    28.    
    29.     Me.Visible = False 'With this you wont capture this form
    30.     DoEvents           'With this you wont capture the common dialog window
    31.    
    32.     'Copy the contents of the work area to the hidden picturebox using BitBlt
    33.     With frmMain.pbWorkArea
    34.         Call BitBlt(frmMain.pbHidden.hDC, 0, 0, .Width, .Height, .hDC, 0, 0, vbSrcCopy)
    35.     End With
    36.    
    37.     If CD.FileName <> vbNullString Then SavePicture frmMain.pbHidden.Image, CD.FileName
    38.    
    39.     Unload Me
    40. End Sub
    By the way, nice splash screen fading

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