How to capture Screenshots in C#.Net


In this article, you will know how to capture Screenshots in C#.Net. You can check this article to get guideline on capturing Screenshots in C#.Net. Code returns of bitmap image save the image in computer.

How to capture Screenshots in C#.Net


This C# code for capturing screen shots. This code returns bitmap image save this image in computer.


private static Bitmap BitMapCreater()
{
Rectangle rect = Screen.PrimaryScreen.Bounds;
int color = Screen.PrimaryScreen.BitsPerPixel;
PixelFormat pFormat;
switch (color)
{
case 8:
case 16:
pFormat = PixelFormat.Format16bppRgb565;
break;

case 24:
pFormat = PixelFormat.Format24bppRgb;
break;

case 32:
pFormat = PixelFormat.Format32bppArgb;
break;

default:
pFormat = PixelFormat.Format32bppArgb;
break;
}
Bitmap bmp = new Bitmap(rect.Width, rect.Height, pFormat);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(rect.Left, rect.Top, 0, 0, rect.Size);
return bmp;
}
call this method to SAVE file

on Button Click()
{
Bitmap b = BitMapCreater();
printScreen = string.Format("{0}{1}", Path.GetTempPath(), "screen" + i + ".jpg");
b.Save(printScreen, ImageFormat.Jpeg);

}


Comments

Author: vijetha29 May 2008 Member Level: Gold   Points : 2

U can Try this also

this.Hide();
Bitmap Bitmap;
Graphics Graps;
Bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
Graps = Graphics.FromImage(Bitmap);
Graps.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
Bitmap.Save(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\YourShot.jpeg", ImageFormat.Jpeg);
MessageBox.Show("Your Screen Shot Save in Desktop");
this.Show();

Author: Raju Melveetilpurayil29 May 2008 Member Level: Gold   Points : 2

Ok vijetha

Author: komaladevi29 May 2008 Member Level: Gold   Points : 2

good one

Author: Kumar Velu30 May 2008 Member Level: Gold   Points : 2

nice...

Author: nagendra reddy panyam02 May 2012 Member Level: Bronze   Points : 5

Thanq Raju Melveetilpurayil but my program i,print screen shows errors How can we rectify that error i paste my code could you please correct it...please guide me

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace rectestvideo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
public 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 Form1_Load(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;
}

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

private void button3_Click(object sender, EventArgs e)
{
Bitmap b = BitMapCreater();
printScreen = string.Format("{0}{1}", Path.GetTempPath(), "screen" + i + ".jpg");
b.Save(printScreen, ImageFormat.Jpeg);

}

private static Bitmap BitMapCreater()
{
Rectangle rect = Screen.PrimaryScreen.Bounds;
int color = Screen.PrimaryScreen.BitsPerPixel;
PixelFormat pFormat;
switch (color)
{
case 8:
case 16:
pFormat = PixelFormat.Format16bppRgb565;
break;

case 24:
pFormat = PixelFormat.Format24bppRgb;
break;

case 32:
pFormat = PixelFormat.Format32bppArgb;
break;

default:
pFormat = PixelFormat.Format32bppArgb;
break;
}
Bitmap bmp = new Bitmap(rect.Width, rect.Height, pFormat);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(rect.Left, rect.Top, 0, 0, rect.Size);
return bmp;
}
// call this method to SAVE file





}
}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: