|
|
Title | Capture an image of a MSChart control |
Description | This example shows how to capture an image of a MS Chart control in Visual Basic 6. |
Keywords | MSChart, chart, control, image |
Categories | Controls |
|
|
Thanks to Gamal Ahmed.
Subroutine GetFormPicture gets an image of a form. It use the keybd_event API function to generate the equivalent of the Alt-PrntScrn key to copy the form's image to the clipboard. It then pastes the image into a hidden PictureBox.
|
|
' Copythe form's image into picHidden.
Public Function GetFormPicture()
With Form1
#Const WINDOWS_VERSION = "Windows2000"
Dim alt_key As Long
' Capture an image of the form in the clipboard.
' Press Alt.
alt_key = MapVirtualKey(VK_MENU, 0)
keybd_event VK_MENU, alt_key, 0, 0
DoEvents
' Press Print Scrn.
#If WINDOWS_VERSION = "Windows2000" Then
keybd_event VK_SNAPSHOT, 0, 0, 0
#Else
keybd_event VK_SNAPSHOT, 1, 0, 0
#End If
DoEvents
' Release Alt.
keybd_event VK_MENU, alt_key, KEYEVENTF_KEYUP, 0
DoEvents
' Make picHidden big enough.
.picHidden.BorderStyle = vbBSNone
.picHidden.Width = .Width
.picHidden.Height = .Height
' Paste the image into picHidden.
.picHidden.Picture = Clipboard.GetData(vbCFBitmap)
End With
End Function
|
|
Subroutine GetPictures calls GetFormPicture to get the form's image. It then calls GetFormOffset to find the MSChart control's offset within the form and uses CopyControlImage to copy that part of the form into a result PictureBox.
|
|
Sub GetPictures()
' Copy the form's image into picHidden.
With Form1
GetFormPicture
' Get the offset from our border to the form area.
GetFormOffset
' Clip out the control images.
CopyControlImage .picResult, .MSChart1
End With
End Sub
' Get the offset from the corner of the border.
Public Sub GetFormOffset()
With Form1
Dim pt As POINTAPI
pt.X = 0
pt.Y = 0
ClientToScreen .picResult.hwnd, pt
m_XOffset = pt.X - .ScaleX(.Left, vbTwips, vbPixels)
m_YOffset = pt.Y - .ScaleY(.Top, vbTwips, vbPixels)
End With
End Sub
' Copy the image of this control onto the
' destination PictureBox.
Public Sub CopyControlImage(ByVal destination As _
PictureBox, ByVal target As Control)
destination.Width = target.Width + destination.Width - _
destination.ScaleWidth
destination.Height = target.Height + destination.Height _
- destination.ScaleHeight
destination.PaintPicture _
Form1.picHidden.Picture, 0, 0, _
target.Width, target.Height, _
target.Left + m_XOffset, target.Top + _
m_YOffset, _
target.Width, target.Height
destination.Picture = destination.Image
End Sub
|
|
|
|
|
|