Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

c# - How to print a Windows Form without showing/displaying it

I am trying to print automatically a series of Windows-Forms. I don't need to show them. The code examples I found on the internet work only when I display the Form with show()! I need to initialize the form with data and send it to the printer Here is the code I am using:

public partial class Form2_withoutShow : Form{

     PrintDocument PrintDoc;

     Bitmap memImage;

     public Form2_withoutShow (Data data)
     {

        InitializeComponent();

        /*
           Initialize Data (texboxes, charts ect.) here
       */

        this.PrintDoc = new PrintDocument();
        this.PrintDoc.PrintPage += PrintDoc_PrintPage;
        this.PrintDoc.DefaultPageSettings.Landscape = true;
     }


     public void Print()
     {
        this.PrintDoc.Print();
     }

     void PrintDoc_PrintPage(object sender, PrintPageEventArgs e)
     {

        int x = SystemInformation.WorkingArea.X;
        int y = SystemInformation.WorkingArea.Y;
        int width = this.Width;
        int height = this.Height; 

        Rectangle bounds = new Rectangle(x, y, width, height);

        Bitmap img = new Bitmap(width, height); 

        this.DrawToBitmap(img, bounds);
        Point p = new Point(10, 10);

        e.Graphics.DrawImage(img, p);
     }

     private void Form2_withoutShow_Load(object sender, EventArgs e)
     {
         // remove TITLEBar
         this.ControlBox = false;
         this.Text = String.Empty;
     }
}

I call the Print() method from another class in for-loop and pass the Data to be initialized through the constructor.

The MSDN example captures the part on the screen where the forms should be displayed. This doesn't work for me. The approach I am using now yields only the print of the empty Window if I don't call show(). How do I get the data into the form without having to call the show() method? Approaches like mimize the windows when displaying it, also don't work because that is also the print result: a minimized window.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Before showing the form, the form and its controls are not in Created state. To force the form and its controls to be created it's enough to call internal CreateControl(bool fIgnoreVisible) method of your form:

var f = new Form1();
var createControl = f.GetType().GetMethod("CreateControl",
                BindingFlags.Instance | BindingFlags.NonPublic);
createControl.Invoke(f, new object[] { true });

var bm = new Bitmap(f.Width, f.Height);
f.DrawToBitmap(bm, new Rectangle(0, 0, bm.Width, bm.Height));
bm.Save(@"d:m.bmp");

Also remove codes which you have in your form Load event handler, and put them in constructor of your form.

Note

There are also other workarounds for the problem:

  • For example you can show the form in a location outside of boundary of the screen and then hide it again. Set the Location to (-32000, -32000) and set StartPosition to be Manual then Show and Hide the form before drawing to bitmap.
  • Or as another example, you can show the form with Opacity set to 0 and then Show and Hide the form before drawing to bitmap.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...