如何为bmp.screenshotsave添加可选目录

时间:2013-05-13 08:31:37

标签: c# screenshot auto-increment fileopendialog

我需要帮助。我想在保存屏幕截图之前实现选择文件夹的功能。 这是我创建的工作代码:

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

pict_no++;

autoscreenshoter增加了许多屏幕截图。但是文件夹现在是不变的。我想在捕获自​​动截图之前创建文件夹选择。我用过这个:

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

    }

}

我不知道为什么它不起作用。

1 个答案:

答案 0 :(得分:0)

您可以在主线程中使用Invoke方法从Tick事件中访问GUI。

在您的Tick事件中:

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

然后,您可以在Proceed()方法中添加您想要执行的操作。

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