Question

I have a form, with calculated data and charts, that is about paper size (A4 format). This makes that the CopyFromScreen method can't screenshot the entire form. Changing the screen resolution is not a good idea, since the program should work on several computers. In the code below you'll find two captureScreen functions (only one should be used), but none of them print the entire form. Only the upper part of my report is printed to the image. Any suggestions?

open System.IO
open System.Drawing
open System.Windows.Forms
open System.Drawing.Printing
open Microsoft.FSharp.Control

// declaration Form (main) and Button (print)

// captureScreen CopyFromScreen version

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

// captureScreen bitmap version

let captureScreen (form: Form) =
    let myGraphics = form.CreateGraphics()
    let size = form.Size
    let rectangle = new Rectangle(Height=size.Width, Width=size.Width)
    let memoryImage = new Bitmap(size.Width, size.Height, myGraphics)
    main.DrawToBitmap(memoryImage,rectangle)
    memoryImage

// rest of program

let printdoc = new System.Drawing.Printing.PrintDocument()
let printdia = new PrintDialog(Document=printdoc, AllowSomePages=true, ShowHelp=true)
let mutable Image = new Bitmap(main.Size.Width, main.Size.Height)

print.Click.Add(fun prt -> Image <- captureScreen(main)
                       printdia.ShowDialog() |> ignore
                       printdoc.Print())
printdoc.PrintPage.Add(fun prt ->
                            print.Visible <- false
                            prt.Graphics.DrawImage(Image, 0, 0)
                            print.Visible <- true)
Was it helpful?

Solution

I'm assuming you want the whole report to fit onto one printer page, right? Then you may perform scaling from the whole form to the whole printer page. Further assuming printer DPI=300 and A4 is 8.3x11.7 inches the working prototype of scaler might be:

let printFormScaled (form: Form) (printer: PrintPageEventArgs) pageSizeInch =
    let mutable target = new Rectangle(0,0,int((fst pageSizeInch)*300.),int((snd pageSizeInch)*300.))
    let bitmap = new Bitmap(form.Width, form.Height)
    form.DrawToBitmap(bitmap, new Rectangle(0,0, bitmap.Width, bitmap.Height))
    let xScale:double = (double bitmap.Width)/(double target.Width)
    let yScale:double = (double bitmap.Height)/(double target.Height)
    if (xScale < yScale) then
        target.Width <- int(xScale * (double target.Width) / yScale)
    else
        target.Height <- int(yScale * (double target.Height) / xScale)
    printer.Graphics.PageUnit <- GraphicsUnit.Pixel
    printer.Graphics.DrawImage(bitmap, target)

and change printer PrintPage event handler to

printer.PrintPage.Add(fun prt -> printFormScaled form prt (8.3,11.7))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top