Click here to Skip to main content
15,896,496 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi
I have a working code for Visual C# express 2010, that records screen, it works great, but I cant work out how to get it to save to a different filename everytime I click save.


This is the full code:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            
 
        }

        private Bitmap Get_screen()
        {
            Size s = Screen.PrimaryScreen.Bounds.Size;
            Bitmap bt = new Bitmap(s.Width, s.Height);
            Graphics g = Graphics.FromImage(bt);
            g.CopyFromScreen(0, 0, 0, 0, s);
            return bt;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            pictureBox1.Image=Get_screen();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
        timer1.Enabled = false;
        }

        public void button3_Click(object sender, EventArgs e)
        {               
                Bitmap c = new Bitmap(pictureBox1.Image);
                c.Save("C:\\image.gif", System.Drawing.Imaging.ImageFormat.Gif);
        }

           

        }
                      
}


I need to save each image as image1, image2, etc because I plan to convert the images to avi.
Posted

Create an integer index that gets incremented every time the button is clicked.
Then add that index onto your file name in the c.Save command.

C#
c.Save("C:\\image"+index.ToString()+".gif", System.....)
 
Share this answer
 
This code sorts out problem:

C#
public int B =1;
        
private void button3_Click(object sender, EventArgs e)

{
Bitmap c = new Bitmap(pictureBox1.Image);
c.Save("C:\\image" + B.ToString() + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
B ++;
} 
 
Share this answer
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

// counter
private int counter=0;


public Form1()
{
InitializeComponent();
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;


}

private Bitmap Get_screen()
{
Size s = Screen.PrimaryScreen.Bounds.Size;
Bitmap bt = new Bitmap(s.Width, s.Height);
Graphics g = Graphics.FromImage(bt);
g.CopyFromScreen(0, 0, 0, 0, s);
return bt;
}

private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Image=Get_screen();

// increase counter here
counter++;
}

private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}

private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}

public void button3_Click(object sender, EventArgs e)
{
Bitmap c = new Bitmap(pictureBox1.Image);
//c.Save("C:\\image.gif", System.Drawing.Imaging.ImageFormat.Gif);
c.Save("C:\\image" + counter.ToString() + ".gif", System.Drawing.Imaging.ImageFormat.Gif);

}

}
}


If you are planing to convert the saved images to avi file, I think the image frames you saved in each click
is not sufficient in terms of numbers of the images for a smooth avi files. If you move the code inside the
"button3_Click" to an event which fires based on pre-setting times, like your timer1_Tick, you
can get good results.

You can find useful and convinient save image stuffs here:
http://www.artuxsoft.com
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900