Unable to copy from clipboard

A

andrew

I have a MSchart object (COM Component) which I wish to
insert as an image into a picture box so that I can print
it out.

'I call the chart controls's EditCopy to pass data to the
clipboard.

Dim Img As Image
MyChart.EditCopy()
'I then declare an IDataObject
Dim data As IData = Clipboard.GetDataObject()

'I THen check to see whether emf data is present
If (data.GetDataPresent(DataFormats.EnhancedMetafile)) Then
'(Checking this statement in at debug evaluates to true)(I
also am satisfied that the data is on the clipboard
because I am able to Paste Special it into EXCEL)
'I then try and recreate the emf as an image and have
tried the following combinations
'Img.FromFile(data.GetData(DataFormats.EnhancedMetafile))
'Img = CType(data.GetData(DataFormats.EnhancedMetafile)),
Image)
UNFORTUNATELY THE ABOVE CODE SEEMS TO RETURN NOTHING FOR
THE Img VARIABLE, AND INDICATES TO ME THAT I HAVE TO
SOMEHOW INSTATIATE AND CONSTRUCT THE Img FROM THE METAFILE
DATA, HOWEVER I HAVE NO IDEA HOW TO DO THIS.
'I then hope to assign my Image to the PictureBox Control
PictureBox1.Image= Img
End If

I PRESUME IT IS POSSIBLE TO COPY IMAGES OF COM OBJECTS TO
WIN FORM PICTURE BOXES (WITHOUT RESORTING TO PHOTOCOPYING
AND SCANNING!), CAN ANYONE GIVE ME SOME POINTERS PLEASE.

THANKS IN ADVANCE

ANDREW
 
K

Ken Tucker [MVP]

Hi,

This procedure will create a bitmap from a form or control given its hwnd.
You can print the bitmap.

Private Structure RECT

Public Left As Integer

Public Top As Integer

Public Right As Integer

Public Bottom As Integer

End Structure

Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Integer,
_

ByRef lpRect As RECT) As Integer



Public Declare Function BitBlt Lib "gdi32" Alias "BitBlt" _

(ByVal hDestDC As Integer, ByVal x As Integer, _

ByVal y As Integer, ByVal nWidth As Integer, _

ByVal nHeight As Integer, ByVal hSrcDC As Integer, _

ByVal xSrc As Integer, ByVal ySrc As Integer, _

ByVal dwRop As Integer) As Integer



Public Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC" _

(ByVal hwnd As Integer) As Integer



Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Integer, _

ByVal hObject As Integer) As Integer

Public Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Integer, _

ByVal hdc As Integer) As Integer



Public Const SRCCOPY As Integer = &HCC0020

Public Const SRCINVERT As Integer = &H660046

Public Const MERGECOPY As Integer = &HC000CA

Public Const MERGEPAINT As Integer = &HBB0226

Private Function GetWindowPicture(ByVal hWnd As Integer) As Bitmap

Dim g As Graphics

Dim hdcDest As IntPtr

Dim hdcSrc As Integer

Dim bm As Bitmap

Dim r As New RECT()

Dim w, h As Integer

GetWindowRect(hWnd, r)

w = r.Right - r.Left

h = r.Bottom - r.Top

bm = New Bitmap(w, h)

g = g.FromImage(bm)

hdcSrc = GetWindowDC(hWnd)

hdcDest = g.GetHdc



BitBlt( _

hdcDest.ToInt32, 0, 0, w, h, hdcSrc, 0, 0, SRCCOPY)

g.ReleaseHdc(hdcDest)

ReleaseDC(hWnd, hdcSrc)

Return bm

End Function

Ken

----------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top