Saving form and contents as bitmap

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dbanning
    New Member
    • Apr 2007
    • 19

    Saving form and contents as bitmap

    I am trying to save the contents of a form as a bitmap and am unsure how to do this. I can use the savepicture command to save the form as a bitmap but the contents is not intact as you would expect. There is 1 picture box on the form which I wish to save with the form so really a full screen shot of the form.

    Taking a screen shot and saving it won't do it either as the form is only a section of the screen say 640 X 480 and is centred on the screen for my program to work. Is there any way of linking the picturebox to the form and saving both or taking a screenshot of the form alone and saving just this section?

    Daniel
  • dbanning
    New Member
    • Apr 2007
    • 19

    #2
    Sorry I am using VB6 to do this

    Comment

    • Tig201
      New Member
      • Mar 2007
      • 103

      #3
      in windows if you hold the alt key while pressing the print screen key it will capture a screen shot of only the active window. you could then paste it into paint or whatever picture editor you use. as to a way to do it with VB6 I don't know.

      Comment

      • dbanning
        New Member
        • Apr 2007
        • 19

        #4
        Originally posted by Tig201
        in windows if you hold the alt key while pressing the print screen key it will capture a screen shot of only the active window. you could then paste it into paint or whatever picture editor you use. as to a way to do it with VB6 I don't know.
        Thanks I did not know that shortcut I might be able to do something with that, Thanks againg all help is apprieciated.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by dbanning
          Thanks I did not know that shortcut I might be able to do something with that, Thanks againg all help is apprieciated.
          Yes, that's the quick and easy way to grab a shot of the current window in Windows.

          If you need to do it in your code, I'd suggest you start by searching for how to build a screensaver in VB. I have done this, many years ago, and the first thing many screensavers do is to grab a copy of the screen, so they can play with it. I'm sure this could be adapted to capture just the part displaying your window.

          Comment

          • dbanning
            New Member
            • Apr 2007
            • 19

            #6
            Originally posted by Killer42
            Yes, that's the quick and easy way to grab a shot of the current window in Windows.

            If you need to do it in your code, I'd suggest you start by searching for how to build a screensaver in VB. I have done this, many years ago, and the first thing many screensavers do is to grab a copy of the screen, so they can play with it. I'm sure this could be adapted to capture just the part displaying your window.
            Thanks I will look into that, What about sendkey command is there any way to use this to press alt and print screen and then pull the image from the clipboard and save it.

            I have tried this a bit but think I must need a timer to release the keypress alo if I code

            Sendkey "Alt + print screen",true

            for some reson numlock comes on so obviously im guessing print screen is not the name vb gives to this key?

            Comment

            • Tig201
              New Member
              • Mar 2007
              • 103

              #7
              I looked around and found this:www.thescripts. com/forum/thread13968.htm l

              I also played around and got the following code to work:
              Code:
              Option Explicit
              
              Const VK_MENU = 18
              Const VK_SNAPSHOT = 44
              Const KEYEVENTF_EXTENDEDKEY = &H1
              Const KEYEVENTF_KEYUP = &H2
              
              Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
              
              Private Sub Command1_Click()
                keybd_event VK_MENU, 0, 0, 0   'Send ALT key
                keybd_event VK_SNAPSHOT, 0, 0, 0   'Send PRINT SCREEN key
                keybd_event VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0   'Release PRINT SCREEN key
                keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0   'Release ALT key
              End Sub
              This put a screen shot of my form in the clipboard hope it helps.

              Comment

              • dbanning
                New Member
                • Apr 2007
                • 19

                #8
                Originally posted by Tig201
                I looked around and found this:www.thescripts. com/forum/thread13968.htm l

                I also played around and got the following code to work:
                Code:
                Option Explicit
                
                Const VK_MENU = 18
                Const VK_SNAPSHOT = 44
                Const KEYEVENTF_EXTENDEDKEY = &H1
                Const KEYEVENTF_KEYUP = &H2
                
                Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
                
                Private Sub Command1_Click()
                  keybd_event VK_MENU, 0, 0, 0   'Send ALT key
                  keybd_event VK_SNAPSHOT, 0, 0, 0   'Send PRINT SCREEN key
                  keybd_event VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0   'Release PRINT SCREEN key
                  keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0   'Release ALT key
                End Sub
                This put a screen shot of my form in the clipboard hope it helps.
                That is very useful I will attemp to use this later once i return from work thanks for going to the effort of looking into this for me it is much apprieciated.

                I will let post an outcome later

                Comment

                • dbanning
                  New Member
                  • Apr 2007
                  • 19

                  #9
                  you code works perfectly thanks again, I have tuned it up abit and added in some extra bis and now it works exactly as i want.

                  Code:
                  Option Explicit
                  
                  Const VK_MENU = 18
                  Const VK_SNAPSHOT = 44
                  Const KEYEVENTF_EXTENDEDKEY = &H1
                  Const KEYEVENTF_KEYUP = &H2
                  
                  Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
                  
                  Private Function SaveFormPic() As Picture
                  Dim pic As StdPicture
                   Set pic = Clipboard.GetData(vbCFBitmap)
                    keybd_event VK_MENU, 0, 0, 0   'Send ALT key
                    keybd_event VK_SNAPSHOT, 0, 0, 0   'Send PRINT SCREEN key
                    DoEvents
                    keybd_event VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0   'Release PRINT SCREEN key
                    keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0   'Release ALT key
                    DoEvents
                    Set SaveFormPic = Clipboard.GetData(vbCFBitmap)
                   Clipboard.SetData pic, vbCFBitmap
                  End Function
                  
                  Private Sub Command1_Click()
                   SavePicture SaveFormPic, "C:\MyPic.jpg" 'picture location
                   Clipboard.Clear
                  End Sub
                  Thanks for all your help
                  Last edited by Killer42; Apr 18 '07, 10:18 PM. Reason: Added [CODE] tag

                  Comment

                  • Tig201
                    New Member
                    • Mar 2007
                    • 103

                    #10
                    Glad to help

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by dbanning
                      you code works perfectly thanks again, I have tuned it up abit and added in some extra bis and now it works exactly as i want.
                      ...code...
                      Thanks for sharing that. If it's OK with you, I'll add it to the Tips & Tricks at the top of the VB forum. (Once I've had time to try it out, of course.)

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        Originally posted by Tig201
                        I looked around and found this:
                        ...
                        Thanks Tig20. I plan to put this stuff in the Tip & Tricks for the VB forum.

                        Question, though - do you know whether this would be affected by the existing keyboard state?

                        I mean, say the user pressed the ALT key just before this code executed, ot was holding the key down - would it make a difference?

                        I'll try it out myself and see, when I get a chance. But thought you may have already done so.

                        Comment

                        • dbanning
                          New Member
                          • Apr 2007
                          • 19

                          #13
                          Your welcome to use the code I have tried the original version and my new verison with the saving of the file and both work perfectly.

                          Very useful code i will make note of it incase i need to use it again.

                          I also try holding alt while clicking the command button and it does not have any effect on the code and still does what it is suppose to do.

                          My program has linked the save file path to a text box on my form where I have the path of a drive and dir box to select a folder snd then automatically generating the file name from some variables.

                          My programs purpose is using a second form with a picture box on the first form can change the backcolour of this form and alos the square color(picture box) it then calculates the square size dependin on how many squares you want across and down and will run the image(form2). clicking the form moves the square through all its positions.

                          I also wanted to be able to save the indiviual bitmaps which is what I wanted this code for.


                          The only problem I have now is that the first image shows the first form as the second form has not quite displayed yet.

                          I need a timer in my code to delay for a while until the form has loaded either that or ask it to wait until form2 has loaded.

                          Any suggestions on the second choice?

                          Comment

                          • Tig201
                            New Member
                            • Mar 2007
                            • 103

                            #14
                            Originally posted by Killer42
                            Thanks Tig20. I plan to put this stuff in the Tip & Tricks for the VB forum.
                            Sure go right ahead. You may want to add this link as well I found it through a link in the thread I linked to, and it was where I found the “keybd_event” code and examples. allapi.mentalis .org/apilist/keyb_event.shtm l
                            Originally posted by Killer42
                            Question, though - do you know whether this would be affected by the existing keyboard state?

                            I mean, say the user pressed the ALT key just before this code executed, ot was holding the key down - would it make a difference?

                            I'll try it out myself and see, when I get a chance. But thought you may have already done so.
                            I tried it in a button and could not click on the button while holding the Print screen key on the keyboard so you may want to check that if you are calling this through a timer or something like that.

                            The other thought I had is if a specific key interferes with the screen capture and is held down, it may be a problem but I don’t know of any keys that will do this. If there is a specific key you could probably release that key to get around it.
                            Code:
                            Const VK_ProblemKey = 30 'example assuming 30 represents a problem key
                            ..........
                             keybd_event VK_ProblemKey, 0, KEYEVENTF_KEYUP, 0   'Release ProblemKey
                            if you know of any key that does this please let me know so I can try releasing it.

                            Comment

                            • Tig201
                              New Member
                              • Mar 2007
                              • 103

                              #15
                              Originally posted by dbanning
                              My program has linked the save file path to a text box on my form where I have the path of a drive and dir box to select a folder snd then automatically generating the file name from some variables.

                              My programs purpose is using a second form with a picture box on the first form can change the backcolour of this form and alos the square color(picture box) it then calculates the square size dependin on how many squares you want across and down and will run the image(form2). clicking the form moves the square through all its positions.

                              I also wanted to be able to save the indiviual bitmaps which is what I wanted this code for.


                              The only problem I have now is that the first image shows the first form as the second form has not quite displayed yet.

                              I need a timer in my code to delay for a while until the form has loaded either that or ask it to wait until form2 has loaded.

                              Any suggestions on the second choice?
                              I'm not sure about this but I think that calling the code from your first form is keeping it as the active window, you might want to figure a way out to call it from your second form so it will be the active window. Perhaps the “Form_GotFocus” function with a public flag to determine whether you want to capture a screen shot when you activate the form.

                              Comment

                              Working...