Page 1 of 1

ScreenShot Maker [Well Explained Tutorial - C#]

Posted: Sun Sep 25, 2011 5:37 am
by Code
Create a new Windows From Application.
Add:
3 buttons
1 Picture Box
Details: Follow this tutorial and you won't be confused!
Double click on your form and put this code above everything.
Code: Select all
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;
using System.Drawing.Imaging;
This is just your simple dim's(I am use too VB.net so thats what i know them as)
Insert this under the public form1()
Code: Select all
        {
            InitializeComponent();
        }
        private static Bitmap bmpScreenshot;
        private static Graphics gfxScreenshot;
Double click your form and insert this code for your capture button.
This basically means that when your going to take a screenshot for the application to hide first then take the screenshot as a bmp then show the application. This way we do not have the physical application in the picture.
Button1: Capture
Code: Select all
{
            this.Hide();
            System.Threading.Thread.Sleep(1000);
            bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
            gfxScreenshot = Graphics.FromImage(bmpScreenshot);
            gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
            pictureBox1.Image = bmpScreenshot;
            System.Threading.Thread.Sleep(1000);
            this.Show();
        }
Now you may use a button, toolstrip button or anything for this.
This is basically your clear button. If you do not like the picture you just took this will clear your picture box.
Button2: Clear
Code: Select all
{
            pictureBox1.Image = null;
        }
This is how we will save your screenshot, this code tells the program too open a save window where you can chose where you want to save your image.
Button3: Save
Code: Select all
{
            saveFileDialog1.FileName = "";
            saveFileDialog1.ShowDialog();
            try
            {
                pictureBox1.Image.Save(saveFileDialog1.FileName);
                System.Diagnostics.Process.Start(saveFileDialog1.FileName);
                saveFileDialog1.FileName = "";
            }
            catch
            {
            }
        }
I hope this tutorial made sense and was helpful as this was my first C# tutorial!
If you see any errors or mistakes please point them out!

Re: ScreenShot Maker [Well Explained Tutorial - C#]

Posted: Sun Sep 25, 2011 9:00 am
by MrAksel
You cant convert a string "" to a image. It will be an error at the clear code
Code: Select all
{
      pictureBox1.Image = "";
}

Re: ScreenShot Maker [Well Explained Tutorial - C#]

Posted: Sun Sep 25, 2011 10:18 am
by Axel
I think an empty string represents null(vbNull) but I don't know

Re: ScreenShot Maker [Well Explained Tutorial - C#]

Posted: Sun Sep 25, 2011 11:07 am
by Codex
i don't know about c# but in vb.net you can do
Code: Select all
picturebox1.image = nothing

Re: ScreenShot Maker [Well Explained Tutorial - C#]

Posted: Sun Sep 25, 2011 11:22 am
by Agust1337
Code: Select all
picturebox1.Image = null;
works fine

Re: ScreenShot Maker [Well Explained Tutorial - C#]

Posted: Sun Sep 25, 2011 4:44 pm
by Zulf
Agust1337 wrote:
Code: Select all
picturebox1.Image = null;
works fine
Or
Code: Select all
pictureBox1.Clear();

Re: ScreenShot Maker [Well Explained Tutorial - C#]

Posted: Sun Sep 25, 2011 6:18 pm
by Code
Sorry, i was not home. Just got back, i fixed up the code. Thanks for the help!