Skip to content

Instantly share code, notes, and snippets.

@geandbe
Created January 19, 2014 19:21
Show Gist options
  • Save geandbe/8509716 to your computer and use it in GitHub Desktop.
Save geandbe/8509716 to your computer and use it in GitHub Desktop.
A snippet showing a very basic event wiring for printing of Winform containing FSharp.Charting chart
// Including F# Chart into WinForm and providing printing functionality
// nuget FSharp.Charting, add references to System.Drawing and System.Windows.Forms
// tested for FSharp.Charting 0.90.5
open System
open FSharp.Charting
open FSharp.Charting.ChartTypes
open System.Drawing
open System.Drawing.Printing
open System.Windows.Forms
[<STAThread; EntryPoint>]
let main args =
let captureScreen (form: Form) =
let myGraphics = form.CreateGraphics()
let size = form.Size
let memoryImage = new Bitmap(size.Width, size.Height, myGraphics)
let memoryGraphics = Graphics.FromImage(memoryImage)
memoryGraphics.CopyFromScreen(form.Location.X, form.Location.Y, 0, 0, size)
memoryImage
let myChart = [for x in 0.0 .. 0.1 .. 6.0 -> sin x + cos (2.0 * x)]
|> Chart.Line |> Chart.WithYAxis(Title="Test")
let chart = new ChartControl(myChart, Dock=DockStyle.Fill)
use printer = new System.Drawing.Printing.PrintDocument()
let printBtn = new Button(Text="Print", Dock=DockStyle.Bottom)
printBtn.Click.Add(fun prt -> printer.Print())
let form = new Form(Visible = true, TopMost = true, Width = 700, Height = 500)
printer.PrintPage.Add(fun prt ->
printBtn.Visible <- false
prt.Graphics.DrawImage(captureScreen(form), 0, 0)
printBtn.Visible <- true)
form.Controls.AddRange([|chart; printBtn |])
do Application.Run(form) |> ignore
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment