[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB.NET Clipboard questions with Print Screens

Posted on 2006-05-25
7
Medium Priority
?
893 Views
Last Modified: 2012-06-22
I want to build an app that on any computer's PC, when they hit the print screen button, I want my VB.NET app to instantly capture that and send it to either a database table, email as an attachment, or as a jpg or something in a share on our network.

Questions:

1) I know you can use the clipboard class in the Forms namespace but I'm wondering should I check for a bitmap after the user hits their print screen or another type?

2) I'm using SQL 2005, and I want to insert this data into a table however not sure what datatype to use.   I believe SQL 2005 has a new datatype to handle images...

3) About the clipboard itself, what type of memory is it stored in?  I assume it's not saved on the hard disk?

4) Should I use GetData or GetImage in the Clipboard Class?

5) How would I grab the event, and awaken my VB.NET code to grab the picture from the clipboard?  If a user clicks the print screen button, is there any way possible to capture that event then fire off my VB.NET which I'll code to somehow shove the information into my SQL 2005 table?
0
Comment
Question by:dba123
  • 3
  • 3
7 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 16766345
Play with this...

(Form1 has a PictureBox and a Timer on it)
(If you're working in VB.Net 2005, the keyboard hook will only work in Release mode and not in the IDE)

Imports System.Runtime.InteropServices

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private WithEvents llkb As LowLevelKeyBoardhook

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        llkb = New LowLevelKeyBoardhook
        Timer1.Interval = 100
        Timer1.Enabled = False
    End Sub

    Private Sub llkb_PrtScr() Handles llkb.PrtScr
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim data As IDataObject = Clipboard.GetDataObject()
        If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
            Timer1.Stop()
            Dim bmp As Bitmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Bitmap)

            ' do someting with bmp...
            PictureBox1.Image = bmp
        End If
    End Sub

End Class

' --------------------------------------------------
'  Class LowLevelKeyBoardhook
' --------------------------------------------------
Public Class LowLevelKeyBoardhook

    Private Const HC_ACTION As Integer = 0
    Private Const WH_KEYBOARD_LL As Integer = 13
    Private Const WM_KEYDOWN As Integer = &H100
    Private Const WM_SYSKEYDOWN As Integer = &H104

    Public Structure KBDLLHOOKSTRUCT
        Public vkCode As Integer
        Public scancode As Integer
        Public flags As Integer
        Public time As Integer
        Public dwExtraInfo As Integer
    End Structure

    Private Declare Function SetWindowsHookEx Lib "user32" _
        Alias "SetWindowsHookExA" ( _
        ByVal idHook As Integer, _
        ByVal lpfn As LowLevelKeyboardProcDelegate, _
        ByVal hmod As Integer, _
        ByVal dwThreadId As Integer) As Integer

    Private Declare Function CallNextHookEx Lib "user32" ( _
        ByVal hHook As Integer, _
        ByVal nCode As Integer, _
        ByVal wParam As Integer, _
        ByVal lParam As KBDLLHOOKSTRUCT) As Integer

    Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
        ByVal hHook As Integer) As Integer

    Private Delegate Function LowLevelKeyboardProcDelegate( _
        ByVal nCode As Integer, _
        ByVal wParam As Integer, _
        ByRef lParam As KBDLLHOOKSTRUCT) As Integer

    Public Event PrtScr()

    Private hhkLowLevelKeyboard As Integer
    Private keyboardDelegate As LowLevelKeyboardProcDelegate

    Public Sub New()
        keyboardDelegate = New LowLevelKeyboardProcDelegate(AddressOf Me.LowLevelKeyboardProc)
        hhkLowLevelKeyboard = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardDelegate, _
            Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
    End Sub

    Private Function LowLevelKeyboardProc( _
        ByVal nCode As Integer, _
        ByVal wParam As Integer, _
        ByRef lParam As KBDLLHOOKSTRUCT) As Integer

        If (nCode = HC_ACTION) Then
            Select Case wParam
                Case WM_KEYDOWN, WM_SYSKEYDOWN
                    If lParam.vkCode = Keys.PrintScreen Then
                        RaiseEvent PrtScr()
                    End If
            End Select
        End If

        Return CallNextHookEx(hhkLowLevelKeyboard, nCode, wParam, lParam)
    End Function

    Protected Overrides Sub Finalize()
        UnhookWindowsHookEx(hhkLowLevelKeyboard)
        MyBase.Finalize()
    End Sub

End Class
0
 
LVL 1

Author Comment

by:dba123
ID: 16766346
Then again, I can't just provide a web button that they can click on to perform the print screen for them because then they would loose focus of whatever they were trying to print screen because they would have to gain focus on the IE webform and that's all the print screen of the desktop would show...them print screening the button that fires off the print screen.  So I really do need to figure out if I can trigger my VB.NET code based off them clicking on the print screen key, and somehow capture that event...not sure if that's possible?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16766362
"So I really do need to figure out if I can trigger my VB.NET code based off them clicking on the print screen key, and somehow capture that event...not sure if that's possible?"

The code I just posted does this...but it is for WINFORMS...not sure if this is possible in WebForms as I don't develop for the web.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:LordWabbit
ID: 16767968
The only way to get something like this to work with webforms would be writing an activex control and give it full permissions to execute on the client's pc.  The problem there is getting a digital signature for the control so ie doesn't have a nervous breakdown, the certs cost around $500 (the last time i looked which was a while ago) and they only give certs to companies afaik, and they want to talk to you first which in South Africa is a real pain cos you have to be at work at 3 in the friggin morning.
0
 
LVL 1

Author Comment

by:dba123
ID: 16768257
Idle_Mind , so yours traps an even when they press a certain key on their keboard?  does your current example account for the print screen key?
0
 
LVL 1

Author Comment

by:dba123
ID: 16768260
I don't want to show the user a picturebox, just send it.  If I have to show a picture box, then pop it up and close it real fast so they don't really have to know much about it if you really need it to get this to work after they press the print screen key
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16768734
"does your current example account for the print screen key?"

Yes...it traps the PrtScr key...even when the apps is not in focus.



"I don't want to show the user a picturebox, just send it.  If I have to show a picture box, then pop it up and close it real fast so they don't really have to know much about it if you really need it to get this to work after they press the print screen key"

The PictureBox is ONLY there for illustrative purposes and is NOT needed for the app to work.

The screenshot is captured into a Bitmap instance called "bmp".  In the code below, you can do anything in the part where it says "do something with bmp...":

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim data As IDataObject = Clipboard.GetDataObject()
        If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
            Timer1.Stop()
            Dim bmp As Bitmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Bitmap)

            ' do someting with bmp...
            PictureBox1.Image = bmp
        End If
    End Sub

So there you could send it to your database, email it, or save it to the network share...
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Integration Management Part 2
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

864 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