簡體   English   中英

在C#中打印Form / UserControl

[英]Printing a Form/UserControl in C#

我的程序:包含一個包含少量文本框和一個按鈕的表單。 “默認打印機”在我的計算機上設置為Adobe PDF

我的目標:當用戶點擊“打印”按鈕時,想要截取表單/ usercontrol的屏幕截圖。 然后屏幕截圖以.pdf格式保存在桌面上。

我的問題:我在代碼中遇到兩個問題:

  1. 屏幕截圖的大小:屏幕截圖的大小太大,在打印/轉換為.pdf時,它不符合頁面大小(默認頁面大小)。 請參考下面的兩張圖片。 我希望整個屏幕截圖適合頁面。
  2. 詢問兩次轉換和保存的位置:當我點擊“打印表單”按鈕時,程序會詢問我TWICE打印/轉換的位置並保存文件。 我希望程序只詢問一次,在哪里打印並保存文件。

問題1:程序捕獲的屏幕截圖在打印時不適合頁面。 問題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#語言,非常感謝你的幫助。 :)

好的,請檢查一下,特別是修改后的printDocument1_PrintPage

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            // calculate width and height scalings taking page margins into account
            var wScale = e.MarginBounds.Width / (float)_memoryImage.Width;
            var hScale = e.MarginBounds.Height / (float)_memoryImage.Height;

            // choose the smaller of the two scales
            var scale = wScale < hScale ? wScale : hScale;

            // apply scaling to the image
            e.Graphics.ScaleTransform(scale, scale);

            // print to default printer's page
            e.Graphics.DrawImage(_memoryImage, 0, 0);
        }

我將所有事件連接移動到InitializeComponent中,它通常應該去,但它涉及的代碼更多:

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace testScreenCapScale
{
    public partial class Form1 : Form
    {
        public Form1() { InitializeComponent(); }

        private void button1_Click(object sender, EventArgs e)
        {
            CaptureScreen();
            printDocument1.Print();
        }

        private Bitmap _memoryImage;

        private void CaptureScreen()
        {
            // put into using construct because Graphics objects do not 
            //  get automatically disposed when leaving method scope
            using (var myGraphics = CreateGraphics())
            {
                var s = Size;
                _memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
                using (var memoryGraphics = Graphics.FromImage(_memoryImage))
                {
                    memoryGraphics.CopyFromScreen(Location.X, Location.Y, 0, 0, s);
                }
            }
        }

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            // calculate width and height scalings taking page margins into account
            var wScale = e.MarginBounds.Width / (float)_memoryImage.Width;
            var hScale = e.MarginBounds.Height / (float)_memoryImage.Height;

            // choose the smaller of the two scales
            var scale = wScale < hScale ? wScale : hScale;

            // apply scaling to the image
            e.Graphics.ScaleTransform(scale, scale);

            // print to default printer's page
            e.Graphics.DrawImage(_memoryImage, 0, 0);
        }
    }
}

Form1.Designer.cs

namespace testScreenCapScale
{
    partial class Form1
    {
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
                components.Dispose();
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.printDocument1 = new System.Drawing.Printing.PrintDocument();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // printDocument1
            // 
            this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(64, 220);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(384, 377);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        private System.Drawing.Printing.PrintDocument printDocument1;
        private System.Windows.Forms.Button button1;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM