2014-01-20 29 views
1

我有一個帶有計算數據和圖表的表格,即紙張尺寸(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) 

回答

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)) 
+0

,感謝你的幫助。真誠的Maarten – user3212668

+0

非常歡迎。 –