打印大于屏幕尺寸的表格

时间:2014-01-20 15:16:00

标签: f# printdocument

我有一张带有计算数据和图表的表格,它与纸张尺寸(A4格式)有关。这使得CopyFromScreen方法无法截取整个表单。更改屏幕分辨率不是一个好主意,因为该程序应该可以在多台计算机上运行。在下面的代码中,您将找到两个captureScreen函数(只应使用一个),但它们都不会打印整个表单。只有我的报告的上半部分打印到图像。有什么建议吗?

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)

1 个答案:

答案 0 :(得分:0)

我假设您希望整个报告适合一个打印机页面,对吧?然后,您可以执行从整个表单到整个打印机页面的缩放。进一步假设打印机DPI = 300且A4为8.3x11.7英寸,缩放器的工作原型可能是:

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)

并将打印机PrintPage事件处理程序更改为

printer.PrintPage.Add(fun prt -> printFormScaled form prt (8.3,11.7))