
MS Chart - save chart as bitmap?
I had a real {*filter*} of a time trying to do this, which should have been
really easy, this is how I ended up doing it, Not a particularly graceful
method, but it works, ignore the printing part.
Private Sub cmdPreview_Click()
On Error GoTo ErrTrap
Const PRINTER_MARGIN As Single = 1440
Dim sngGraphWidth As Single ' Original Values
Dim sngGraphHeight As Single
Dim bytPageOrientation As Byte
Dim OriginalClipBoardPic As IPictureDisp
Dim OriginalClipboardText As String
' we need to resize the graph so that it always takes the whole
Printed Page
' Minus an inch for margins
Me.MousePointer = vbHourglass
picFrame.Visible = False
LockWindowUpdate Me.hwnd
sngGraphHeight = Me.Graph.Height
sngGraphWidth = Me.Graph.Width
bytPageOrientation = Printer.Orientation
Printer.Orientation = vbPRORLandscape
Me.Graph.Width = (Printer.Width - PRINTER_MARGIN * 2) ' Can add *2
in here for better resolution, note
Me.Graph.Height = (Printer.Height - PRINTER_MARGIN * 2) ' have to
change fonts to larger first.
' Copy old items from Clipboard and clear it
If Clipboard.GetFormat(vbCFText) Then
OriginalClipboardText = Clipboard.GetText
End If
If Clipboard.GetFormat(vbCFBitmap) Or Clipboard.GetFormat(vbCFMetafile)
Or Clipboard.GetFormat(vbCFDIB) Then
Set OriginalClipBoardPic = Clipboard.GetData()
End If
Clipboard.Clear
' Copy Graph to clipboard then send to printer
Graph.EditCopy
Printer.PaintPicture Clipboard.GetData(), PRINTER_MARGIN,
PRINTER_MARGIN, (Printer.Width - PRINTER_MARGIN * 2), (Printer.Height -
PRINTER_MARGIN * 2)
Printer.EndDoc
' Restore everything
Me.Graph.Height = sngGraphHeight
Me.Graph.Width = sngGraphWidth
Printer.Orientation = bytPageOrientation
' Restore Clipboard
Clipboard.Clear
If Not (OriginalClipBoardPic Is Nothing) Then
Clipboard.SetData OriginalClipBoardPic
End If
If Len(OriginalClipboardText) Then
Clipboard.SetText OriginalClipboardText
End If
picFrame.Visible = True
LockWindowUpdate 0
ExitSub:
Me.MousePointer = vbDefault
Exit Sub
ErrTrap:
MsgBox Err.Description
Resume ExitSub
End Sub
Hope it helps.
Paul
Quote:
> How can I save chart that I produced with MS Chart Control to some bitmap
> file or even better to gif or jpg file?
> Thanks!
> --
> Zeljko