?
Solved

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

Posted on 2004-11-24
12
Medium Priority
?
35,369 Views
Last Modified: 2011-08-18
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.

0
Comment
Question by:ckchew666
10 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12663695
VB6 or VB.net ?
0
 

Author Comment

by:ckchew666
ID: 12663981
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
 
LVL 11

Expert Comment

by:pratap_r
ID: 12664071
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:ckchew666
ID: 12678904
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
 
LVL 11

Expert Comment

by:pratap_r
ID: 12678913
you need to put the Picture1 in your form..
0
 

Author Comment

by:ckchew666
ID: 12680013
PRATAP,

What form are u referring to? Where can i get the form?
0
 
LVL 11

Accepted Solution

by:
pratap_r earned 1400 total points
ID: 12680090
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
 

Author Comment

by:ckchew666
ID: 12682323
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
 
LVL 2

Expert Comment

by:eljakim
ID: 13346590
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
 

Expert Comment

by:binsarid
ID: 14991148
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Simple Linear Regression
Introduction to Processes

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question