Ebikeneser
Ebikeneser

Reputation: 2364

Take screenshot of WinForm after it has loaded

I have a screenshot application where a user can pass command line arguments, and based on those arguments will determine the behaviour of the form.

I am trying to take a screenshot after the form has loaded, however I have the functionality to do this after InitializeComponent();. Like so-

if (counts > 0)
        {
            generateScreenshotButton_Click(null, null);

            button2_Click(null, null);
        }

My problem being these functions are firing before the form has loaded. Therefore the screen shot is blank. How can I resolve this?

Upvotes: 1

Views: 326

Answers (2)

Ionică Bizău
Ionică Bizău

Reputation: 113385

Add Load event in your Form.cs file.

There is a difference between InitializeComponent and Form Load. See What does InitializeComponent() do, and how does it work in WPF? It is for WPF but is same in Windows Forms.

Try this code:

public Form1()
        {
            InitializeComponent(); //this is the InitializeComponent method. this This method locates a URI to the XAML for the Window/UserControl that is loading, and passes it to the System.Windows.Application.LoadComponent() static method. 
            this.Load += new EventHandler(Form1_Load); //this create Load Form event
        }

        void Form1_Load(object sender, EventArgs e) //after your form is completely loaded, your program will run the code from here...
        {
            //your code goes here
        }

Hope that I helped you.

Upvotes: 1

Franz1986
Franz1986

Reputation: 144

Have you tried using MainForm Loaded event?
And in that event handler do your stuff? :)

Or maybe using Shown event?

Upvotes: 1

Related Questions