我的程序:包含一個幾個文本框和一個按鈕的窗體。 'Default Printer'在我的計算機上設置爲Adobe PDF。在C中打印窗體/用戶控件#
我的目標:想要在用戶單擊「打印」按鈕時截取窗體/用戶控件。屏幕截圖然後以.pdf格式保存在桌面上。
我的問題:我有以下兩個問題的代碼:
- 截圖的大小:截圖的尺寸過大,它不適合頁面大小(默認頁面大小)打印/轉換爲.pdf。請參考下面的兩張圖片。我希望整個屏幕截圖適合頁面內容。
- 詢問兩次在哪裏轉換並保存:當我點擊「打印表格」按鈕時,程序會問我TWICE在哪裏打印/轉換並保存文件。我希望程序只問我一次,在哪裏打印和保存文件。
問題1:打印時,程序捕獲的屏幕截圖不適合頁面。
我想要的屏幕顯示圖像,以適應這樣.PDF的一個頁面上:
代碼:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Text = "Print Form";
button1.Click += new EventHandler(button1_Click);
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
this.Controls.Add(button1);
}
private void button1_Click(object sender, EventArgs e)
{
CaptureScreen();
printDocument1.Print();
}
Bitmap memoryImage;
private void CaptureScreen()
{
Graphics myGraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}
}
感謝您的幫助提前。我是一名新手,學習c#語言,您的幫助將非常感謝。 :)
看看這個Q/A的C#圖像縮放樣本:http://stackoverflow.com/questions/249587 –
對於第二個問題(雙對話框),這只是一個猜測,但嘗試刪除這一行'printDocument1 .PrintPage + = ...'從你的構造函數中。我猜這已經發生在'InitializeComponent()'裏面,這意味着你顯式地處理了兩次事件。打開你的Form1.Designer.cs文件,看看是否有其他東西你正在複製。 (除了你的Init調用在構造函數中的其他所有內容可能是多餘的。) –
@PaulSasik:不。對於問題2,刪除該行會給我一個白色的PDF,並且無法保存。 ._。 – Smith