Question

I need help please. I want to implement the ability to choose a folder before saving screenshots. This is my working code that I have created:

bmpScreenshot.Save("C:\\test\\pictures\\scr_(" + pict_no + ").Jpeg", ImageFormat.Jpeg);

pict_no++;

It is autoscreenshoter which increments a number of screenshot. But the folder now is constant. I want to create selection of folder before capturing auto screenshot. I used this:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    public void Tick(Object stateInfo)
    {
        string s;
        s = textBox3.Text+ "\\scr_(" + pict_no + ").Jpeg";

        // Set the bitmap object to the size of the screen

        bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);

        // Create a graphics object from the bitmap

        gfxScreenshot = Graphics.FromImage(bmpScreenshot);

        // Take the screenshot from the upper left corner to the right bottom corner

        gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

        // Save the screenshot to the specified path that the user has chosen

        bmpScreenshot.Save(s, ImageFormat.Jpeg);

        pict_no++;


    }

    private void button1_Click(object sender, EventArgs e)
    {    
        MessageBox.Show("Printscreenování právě začalo a bude probíhat po " +textBox1.Text+ " sekundách.\nPřeji příjemný den :D");
         Cas();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        int anInteger;
        anInteger = Convert.ToInt32(textBox1.Text);
        anInteger = int.Parse(textBox1.Text);

    }

    public void Cas()
    {
        int anInteger;
        anInteger = Convert.ToInt32(textBox1.Text);
        anInteger = int.Parse(textBox1.Text);
        TimerCallback callback = new TimerCallback(Tick);
        // create a timer tick
        System.Threading.Timer stateTimer = new System.Threading.Timer(callback, null, 0, anInteger*1000);
        // loop here forever
        for (; ; )
        {
            int enInteger;
            enInteger = Convert.ToInt32(textBox2.Text);
            enInteger = int.Parse(textBox2.Text);
            if (pict_no == enInteger) Environment.FailFast("Konec programu.");
        }
    }

    public void ChooseFolder()
    {
        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
        {
            textBox3.Text = folderBrowserDialog1.SelectedPath;
        }
    }

    public static Bitmap bmpScreenshot { get; set; }

    public static Graphics gfxScreenshot { get; set; }

    public static int pict_no { get; set; }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {
        ChooseFolder();

    }

}

I don't know why it doesn't work.

Was it helpful?

Solution

You can Invoke methods in your main thread where you can access the GUI from the Tick Event.

In your Tick event:

this.Invoke(new MethodInvoker(delegate { this.Proceed(); }), null);

Then you can put what you want to do in the Proceed() method.

private void Proceed()
{
    string s;
    s = textBox3.Text+ "\\scr_(" + pict_no + ").Jpeg";

    // Set the bitmap object to the size of the screen

    bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);

    // Create a graphics object from the bitmap

    gfxScreenshot = Graphics.FromImage(bmpScreenshot);

    // Take the screenshot from the upper left corner to the right bottom corner

    gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

    // Save the screenshot to the specified path that the user has chosen

    bmpScreenshot.Save(s, ImageFormat.Jpeg);

    pict_no++;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top