By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,943 Members | 1,875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,943 IT Pros & Developers. It's quick & easy.

Saving form and contents as bitmap

dbanning
P: 19
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
Apr 17 '07 #1
Share this Question
Share on Google+
17 Replies


dbanning
P: 19
Sorry I am using VB6 to do this
Apr 17 '07 #2

Tig201
100+
P: 103
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.
Apr 17 '07 #3

dbanning
P: 19
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.
Apr 17 '07 #4

Expert 5K+
P: 8,434
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.
Apr 17 '07 #5

dbanning
P: 19
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?
Apr 18 '07 #6

Tig201
100+
P: 103
I looked around and found this:www.thescripts.com/forum/thread13968.html

I also played around and got the following code to work:
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Const VK_MENU = 18
  4. Const VK_SNAPSHOT = 44
  5. Const KEYEVENTF_EXTENDEDKEY = &H1
  6. Const KEYEVENTF_KEYUP = &H2
  7.  
  8. Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
  9.  
  10. Private Sub Command1_Click()
  11.   keybd_event VK_MENU, 0, 0, 0   'Send ALT key
  12.   keybd_event VK_SNAPSHOT, 0, 0, 0   'Send PRINT SCREEN key
  13.   keybd_event VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0   'Release PRINT SCREEN key
  14.   keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0   'Release ALT key
  15. End Sub
  16.  
This put a screen shot of my form in the clipboard hope it helps.
Apr 18 '07 #7

dbanning
P: 19
I looked around and found this:www.thescripts.com/forum/thread13968.html

I also played around and got the following code to work:
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Const VK_MENU = 18
  4. Const VK_SNAPSHOT = 44
  5. Const KEYEVENTF_EXTENDEDKEY = &H1
  6. Const KEYEVENTF_KEYUP = &H2
  7.  
  8. Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
  9.  
  10. Private Sub Command1_Click()
  11.   keybd_event VK_MENU, 0, 0, 0   'Send ALT key
  12.   keybd_event VK_SNAPSHOT, 0, 0, 0   'Send PRINT SCREEN key
  13.   keybd_event VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0   'Release PRINT SCREEN key
  14.   keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0   'Release ALT key
  15. End Sub
  16.  
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
Apr 18 '07 #8

dbanning
P: 19
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.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Const VK_MENU = 18
  4. Const VK_SNAPSHOT = 44
  5. Const KEYEVENTF_EXTENDEDKEY = &H1
  6. Const KEYEVENTF_KEYUP = &H2
  7.  
  8. Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
  9.  
  10. Private Function SaveFormPic() As Picture
  11. Dim pic As StdPicture
  12.  Set pic = Clipboard.GetData(vbCFBitmap)
  13.   keybd_event VK_MENU, 0, 0, 0   'Send ALT key
  14.   keybd_event VK_SNAPSHOT, 0, 0, 0   'Send PRINT SCREEN key
  15.   DoEvents
  16.   keybd_event VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0   'Release PRINT SCREEN key
  17.   keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0   'Release ALT key
  18.   DoEvents
  19.   Set SaveFormPic = Clipboard.GetData(vbCFBitmap)
  20.  Clipboard.SetData pic, vbCFBitmap
  21. End Function
  22.  
  23. Private Sub Command1_Click()
  24.  SavePicture SaveFormPic, "C:\MyPic.jpg" 'picture location
  25.  Clipboard.Clear
  26. End Sub
Thanks for all your help
Apr 18 '07 #9

Tig201
100+
P: 103
Glad to help
Apr 18 '07 #10

Expert 5K+
P: 8,434
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.)
Apr 18 '07 #11

Expert 5K+
P: 8,434
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.
Apr 18 '07 #12

dbanning
P: 19
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?
Apr 19 '07 #13

Tig201
100+
P: 103
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.shtml
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.
Expand|Select|Wrap|Line Numbers
  1. Const VK_ProblemKey = 30 'example assuming 30 represents a problem key
  2. ..........
  3.  keybd_event VK_ProblemKey, 0, KEYEVENTF_KEYUP, 0   'Release ProblemKey
  4.  
if you know of any key that does this please let me know so I can try releasing it.
Apr 19 '07 #14

Tig201
100+
P: 103
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.
Apr 19 '07 #15

dbanning
P: 19
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.
Form2 is deffinately the active window I am doing

Form2.show
Form1.hide
before the code runs

My program does a do while loop to capture multiple shots of the form with a square moving across the do loop shifts this squares position and saves each time. All pics are captured correctly except the first one. This shows that form2 is the active window but the delay to open it is causeing the bitmap to be captured before it comes into view.

The bitmap shows form1 but only a sction where form2 would be so its capturing form2 before it has come into view.

I am thinking of putting a delay in such as

application.wait Now + timevalue("00:00:01")

to make it wait a second before it runs do loop.
Have not tried this yet either that or put a timer in to do the same job.

It would be better to put some code in to actually wait for the form to load and then wait a second before running as form loading time is dependant on running tasks in the background a time delay may therefore not be effective in all situations.

I will have to have a think
Apr 19 '07 #16

dbanning
P: 19
My mistake the application.wait statement only works in vb macros for excel and not normal vb the Now refers to the current time which is an excell function.

Thats the problem with writing to many macros.

I decided to use the sleep command

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

then just placing sleep X in the code where X is time in milliseconds.

Still had the problem with the save though but have solved it now by using an if statement to save the first position before the do loop.

This means it overwrites itself again saving the correct image on the second attempt.

still using the

sleep 500
form2.refresh

in between square movement and save to make it work correctly.

Seams to have done the trick thanks for all you help.
Apr 19 '07 #17

vdraceil
100+
P: 236
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?
Isnt "PrintScreen" alone enough?This copies the contents of the active window to the clipboard.What is the need of "Alt+PrintScreen"?
Jul 19 '08 #18

Post your reply

Sign in to post your reply or Sign up for a free account.