How to do print screen in VB & print + save file?

Does anyone have a sample program how to do PRINT SCREEN and print it out to a printer and save a copy of it with current date and running file number as name?

Thanks.

ckchew666Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RonaldBiemansCommented:
VB6 or VB.net ?
0
ckchew666Author Commented:
This shall work in Microsoft Visual Basic, not sure if it's equal to VB.net or VB6. SOrry. I'm noob in VB :(
0
pratap_rCommented:
here you go... the code is commented out... if you want any explanation let me know..

the application has a picturebox by name picture1 and command button by name command1, its on a fresh project with no other settings..

'imports of windows API, goes at the top of the file
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Const SRCCOPY = &HCC0020


Private Sub Command1_Click()
Dim hwnd As Long
hwnd = GetDesktopWindow()
Dim hdc As Long
hdc = GetDC(hwnd)

'prepare our picturebox
Picture1.AutoRedraw = True 'this is required otherwise we will not be able to save the picture
Picture1.Width = Screen.Width 'adjust the picturebox
Picture1.Height = Screen.Height

BitBlt Picture1.hdc, 0, 0, Screen.Width / Screen.TwipsPerPixelX, Screen.Height / Screen.TwipsPerPixelY, hdc, 0, 0, SRCCOPY

'release the dc
Call ReleaseDC(hwnd, hdc)

'save the picture
SavePicture Picture1.Image, "c:\test.bmp"

'print the picture
Printer.PaintPicture Picture1.Image, 0, 0

End Sub
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ckchew666Author Commented:
Hi,

I've tried to run the code, but error message 'Run-time error 424. Object required' prompt out.

When compile there is no error. When run the codes, error prompt and a yellow line highlighted "Picture1.AutoRedraw = True 'this is required otherwise we will not be able to save the picture"

Do I need to do anything b4 i can run this codes?

Basically this code will put into my engineering software (RSView32). It has Microsoft Visual Basic compiler inside, when I press a button, this code will run.

Thank.
0
pratap_rCommented:
you need to put the Picture1 in your form..
0
ckchew666Author Commented:
PRATAP,

What form are u referring to? Where can i get the form?
0
pratap_rCommented:
aren't you using Visual Basic 6.0? in the design mode you can add controls to your form.. just create a new project and you are shown a form, drag a picturebox into the form. you are using vb6 right?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ckchew666Author Commented:
Yes, I'm using vb6. But I have a 3rd party software which will call these codes in VB6 to executes. OK, will try to put the form and see later. Thanks.
0
eljakimCommented:
Use a driver like this one its mutch faster then BitBlt'ng it from the screen itself

http://www.demoforge.com/dst/mirage-sdk-1.0.27.0.zip
0
binsaridCommented:
Good program from Pratap r

I make modification to your programm, i have a project to copy from a Picturebox1 to another Picturebox2. In the Picturebox1 I put any image, label, shape by runtime program, I need all image in the Picturebox1 to be a background in the Picturebox2, so that it possible to draw line in Picturebox2 without distrub by another image. From this project has a surprise function, that is same as a cropping bitmap before save to file.
Testing the program need a form1, Picturebox1, Picturebox2 and  command1
before run the program, put image,label, shape inside the Picturebox1, for simulation only


'imports of windows API, goes at the top of the file
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long

Private Const SRCCOPY = &HCC0020


Private Sub Command1_Click()
Dim hwnd As Long
hwnd = GetDesktopWindow()
Dim hdc As Long
hdc = GetDC(hwnd)

'prepare our picturebox
Picture1.AutoRedraw = True  'this is required otherwise
                                            'we will not be able to save the picture

Picture1.Width = 472 'adjust the picturebox1
Picture1.Height = 474
Picture1.ScaleMode = 3
Picture1.Left = 8
Picture1.Top = 8
Picture1.ScaleHeight = 472
Picture1.ScaleWidth = 470

Picture2.Width = 472 'adjust the picturebox2
Picture2.Height = 474
Picture2.ScaleMode = 3
Picture2.Left = 504
Picture2.Top = 8
Picture2.ScaleHeight = 472
Picture2.ScaleWidth = 470

Picture2.AutoRedraw = True   'this is required otherwise
                                            'we will not be able to save the picture

BitBlt Picture2.hdc, 0, 0, 472, 474, hdc, 9, 35, SRCCOPY ' Save only need area

'More info for BitBlt at http://msdn.microsoft.com/library/default.asp?url=/library/en-s/gdi/bitmaps_0fzo.asp 

'release the dc
Call ReleaseDC(hwnd, hdc)

'save the picture is the same with Picturebox2 background
SavePicture Picture2.Image, "c:\test.bmp"

End Sub

-----------------------------------------
Thaks PRATAP
binsarid@yahoo.com
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.