not able to get image from clipboard

meyya.aiht

New member
Joined
Aug 4, 2012
Messages
3
Programming Experience
Beginner
hi,

i am trying to copy a image from my clipboard to a file...
I am using the below code and it works inside a form button click...but when i place this file inside a class file and activate this code after a keyboard print screen event this doesn't work --- System.Windows.Forms.Clipboard.GetDataObject() is empty , but when i open mspaint and paste the image is copied to paint window.

Any help is appreciated

If Not System.Windows.Forms.Clipboard.GetDataObject() Is Nothing Then
Dim oDataObj As IDataObject = System.Windows.Forms.Clipboard.GetDataObject()
If oDataObj.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap) Then
Dim oImgObj As System.Drawing.Image = oDataObj.GetData(DataFormats.Bitmap, True)


oImgObj.Save("G:\1.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)

End If
End If

thanks in advance
 

Dunfiddlin

Well-known member
Joined
Jun 15, 2012
Messages
253
Programming Experience
5-10
If you activate this code on the print screen key press it will be run before the system screen copy is completed. It doesn't detect anything on the clipboard because there isn't yet anything there.
 

meyya.aiht

New member
Joined
Aug 4, 2012
Messages
3
Programming Experience
Beginner
If you activate this code on the print screen key press it will be run before the system screen copy is completed. It doesn't detect anything on the clipboard because there isn't yet anything there.

Thanks for the response...

The thing that you have stated is not true in my case... Because I keep a breakpoint at the step where I check for image on clip board , I manually press the print screen key and wait .. Still it doesn't word.... I execute this code from a class thread.......
 

Bryce Gough

Active member
Joined
Mar 8, 2011
Messages
36
Location
Perth, Australia
Programming Experience
1-3
This has been tested and is working ;)
If Not System.Windows.Forms.Clipboard.GetImage Is Nothing Then
    Dim oImgObj As System.Drawing.Image = System.Windows.Forms.Clipboard.GetImage
    oImgObj.Save("G:\1.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
End If
 

meyya.aiht

New member
Joined
Aug 4, 2012
Messages
3
Programming Experience
Beginner
This has been tested and is working ;)
If Not System.Windows.Forms.Clipboard.GetImage Is Nothing Then    Dim oImgObj As System.Drawing.Image = System.Windows.Forms.Clipboard.GetImage    oImgObj.Save("G:\1.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)End If
Yes I didn't say that this code doesn't work at all...This works inside a form but doesnt work inside a class thread...Any help or info on this is appreciated ...
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,604
Location
Norway
Programming Experience
10+

Dunfiddlin

Well-known member
Joined
Jun 15, 2012
Messages
253
Programming Experience
5-10
You're missing the point. Your code intercepts the printscreen key press event before it is passed to the clipboard API. Putting a breakpoint in doesn't affect this. It just suspends the operation of the code and the passage of the event to the clipboard. The reason it works in a button click event is because it does not affect the passage of the key press event to the clipboard.

key press > key press event (your code here) > API functions = your code is executed before the copy takes place

key press > API functions and button click > button click event (your code here) = your code is executed after the copy takes place
 

smitty

New member
Joined
Jun 22, 2022
Messages
1
Programming Experience
10+
add a line.
Application.DoEvents()
that will let the image get to the clipboard for processing
add that after sending image to clip but before trying to use the image from clip
 
Top Bottom