Imprimer un formulaire / UserControl en C #

Mon programme: contient un formulaire avec quelques zones de texte et un bouton. “Imprimante par défaut” est défini sur Adobe PDF sur mon ordinateur.

Mon objective: vous souhaitez prendre une capture d’écran d’un formulaire / usercontrol lorsque l’utilisateur clique sur le bouton “Imprimer”. La capture d’écran est ensuite enregistrée sur le bureau au format .pdf.

Mon problème: J’ai deux problèmes avec le code:

  1. Taille de la capture d’écran: La taille de la capture d’écran est trop grande et ne correspond pas à la taille de la page (taille de page par défaut) lors de son impression / conversion en .pdf . Veuillez vous référer aux deux images ci-dessous. Je veux que l’intégralité de la capture d’écran tienne dans la page.
  2. Demande deux fois où convertir et enregistrer: Lorsque je clique sur le bouton ‘Imprimer le formulaire’, le programme me demande DEUX FOIS où imprimer / convertir et sauvegarder le fichier. Je veux que le programme me demande seulement une fois, où imprimer et sauvegarder le fichier.

Problème 1: La capture d’écran capturée par le programme ne correspond pas à la page lorsqu’elle est imprimée. Problème 1: La capture d'écran capturée par le programme ne correspond pas à la page lorsqu'elle est imprimée.

Je veux que la capture d’écran s’adapte comme ceci sur une page de .pdf : entrez la description de l'image ici

Code:

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); } } 

Merci d’avance pour votre aide. Je suis un débutant, j’apprends la langue c # et votre aide sera grandement appréciée. 🙂

Ok, jetez un coup d’ printDocument1_PrintPage à cela, et printDocument1_PrintPage particulièrement au printDocument1_PrintPage modifié:

  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); } 

J'ai déplacé tous les fils de l'événement dans InitializeComponent où il est normalement supposé aller mais c'est du code plus impliqué:

 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; } }