HOW TO TAKE SCEEN SHOT IN C# WINDOWS FORM

How take screenshot of desktop using Windows Application.
 
 
Step 1: Add the following namespaces which helps for generating image:
  1. using System.Windows.Forms;
  2. using System.Drawing.Imaging;
Step 2: Add the following code to capture the screenshot and save it to drive:
  1. Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);  
  2. Graphics graphics = Graphics.FromImage(bitmap as System.Drawing.Image);  
  3. graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);  
  4. bitmap.Save(@"D:\MyDemo\Reports\ScreenShot.bmp", ImageFormat.Jpeg);  
In preceding code: 
 
Line 1: It is fetching computer screen height and width. 
Line 2: Creating graphics object to draw/generate image.
Line 3: CopyFromScreen() method is copying background image and generate image through graphics object.
Line 4: It is saving the image to physical folder. 
 
Output:
 
 
                           Figure 1: Captured screenshot in Drive

HOW TO SEND MAIL USING C# WINDOWS FORM

SENDING EMAIL


Step 1: Open Your Visual Studio 2010, Go to File, New, Projects, then under Visual C# select Windows.

windows application

You can change the name of the project and browse your project to different location too. And then press OK.

Design chamber

Step 2: Now open your Form1.cs file, where we will create our design for sending email to gmail. We will drag 3 labels, 3 textboxes and one button from toolbox to Form1.cs. You will see your Form look like this.

Design

Code chamber

Right click on the blank part of Form1.cs, then click View Code. You will enter in the code part of the form. Write the following code and then Press F5 to run the project. 

For GmailSend.dll file - -> https://gmailsend.codeplex.com/

Add some of the namespaces for sending Gmail.

namespaces

If you can’t find these namespaces in the intellisense, then in the Solution Explorer - Right Click, Add References. In Add References, Browse and Find GmaiSend.dll that you had downloaded from the above link.

Add References:

Add References

Code chamber

Open your Form1.cs file where you have to write the code for sending Gmail.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using GmailSend;  
  10.   
  11.   
  12. namespace WindowsFormsApplication2  
  13. {  
  14.     public partial class Form1 : Form  
  15.     {  
  16.         public Form1()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.   
  21.         private void button1_Click(object sender, EventArgs e)  
  22.         {  
  23.             try  
  24.             {  
  25.                 gmail gmlsnd = new gmail();  
  26.                 gmlsnd.auth("your emailid""your password");  
  27.                 gmlsnd.To = textBox1.Text;  
  28.   
  29.   
  30.                 gmlsnd.Subject = textBox2.Text;  
  31.                 gmlsnd.Message = textBox3.Text;  
  32.   
  33.                 gmlsnd.Priority = 1;  
  34.                 gmlsnd.send();  
  35.   
  36.                 MessageBox.Show("Your Mail is sended");  
  37.             }  
  38.             catch (Exception ex)  
  39.             {  
  40.                 MessageBox.Show(ex.Message);  
  41.             }  
  42.              
  43.              
  44.         }  
  45.   
  46.           
  47.     }  
Output chamber

Output


HOW TO READ EXCEL DATA WITH C# WINDOWS FORM

READ EXCEL DATA 
  1. Open Visual Studio. Select New Project, then Windows Form Application.
  2. Name it as you want. My applications name is ReadExcelFileApp.

    window form application
  3. First of all add Reference of Excel library. Right click on the Reference in the solution explorer. 

    Assemblies, Extensions, then Microsoft.Office.Interop.Excel.

    If you are using Visual Studio 2010, then you can add Reference using two ways:

    Right click on References then select Add Reference. After that a dialog appears,

    1. Browse, C drive, Microsoft Office, Office12, then open EXCEL.EXE.
    2. Select .NET tab then choose Microsoft.Office.Interop.Excel.


  4. After adding Reference add namespace using Excel = Microsoft.Office.Interop.Excel; and other namespaces as shown in the figure.
  5. Add two buttons Choose and Read File and Close.
  6. Add a DataGridView to see the result (excel data).

    DataGridView
  7. Create a method ReadExcel who returns a datatable using the following logic.
    1. public DataTable ReadExcel(string fileName, string fileExt) {  
    2.     string conn = string.Empty;  
    3.     DataTable dtexcel = new DataTable();  
    4.     if (fileExt.CompareTo(".xls") == 0)  
    5.         conn = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties='Excel 8.0;HRD=Yes;IMEX=1';"//for below excel 2007  
    6.     else  
    7.         conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0;HDR=NO';"//for above excel 2007  
    8.     using(OleDbConnection con = new OleDbConnection(conn)) {  
    9.         try {  
    10.             OleDbDataAdapter oleAdpt = new OleDbDataAdapter("select * from [Sheet1$]", con); //here we read data from sheet1  
    11.             oleAdpt.Fill(dtexcel); //fill excel data into dataTable  
    12.         } catch {}  
    13.     }  
    14.     return dtexcel;  
    15. }  
    Let's discuss something about ReadExcel() method.

    Firstly, we will decide whether the file has extension .xls or .xlsx because there is a difference between the connection strings of both the files.

    If the file has extension .xls, then the connection string will be the following:

    provider=Microsoft.Jet.OLEDB.4.0;Data Source='fileName';Extended Properties='Excel 8.0;HRD=Yes;IMEX=1';

    Otherwise

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source='FileName';Extended Properties='Excel 12.0;HDR=NO';"

    Here HDR is the header field ,depend upon you,whether you want to add or not,

    IMEX=1 is used to retrieve the mixed data from the columns.

    Now by using the OleDbConnection define an OleDbDataAdapter.
    1. OleDbDataAdapter oleAdpt = new OleDbDataAdapter("select * from [Sheet1$]", con);  
    Here Sheet1 is the sheet number that you want to select, you can select any sheet e.g. Sheet2, Sheet3, etc. If you want to choose some specific columns,   then you can. For example, you want to read just 2 columns say Name and Salary from the excel file then your query be like the following:

    Select Name,Salary from [Sheet1$]

    If there are no headers in complex excel files then you can select columns like F1, F20 etc. In that case the query be like the following:

    Select F11,F41,F70 from [Sheet1$]
  8. Add the following logic in button click events.
    1. private void btnChooseFile_Click(object sender, EventArgs e) {  
    2.     string filePath = string.Empty;  
    3.     string fileExt = string.Empty;  
    4.     OpenFileDialog file = new OpenFileDialog(); //open dialog to choose file  
    5.     if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK) //if there is a file choosen by the user  
    6.     {  
    7.         filePath = file.FileName; //get the path of the file  
    8.         fileExt = Path.GetExtension(filePath); //get the file extension  
    9.         if (fileExt.CompareTo(".xls") == 0 || fileExt.CompareTo(".xlsx") == 0) {  
    10.             try {  
    11.                 DataTable dtExcel = new DataTable();  
    12.                 dtExcel = ReadExcel(filePath, fileExt); //read excel file  
    13.                 dataGridView1.Visible = true;  
    14.                 dataGridView1.DataSource = dtExcel;  
    15.             } catch (Exception ex) {  
    16.                 MessageBox.Show(ex.Message.ToString());  
    17.             }  
    18.         } else {  
    19.             MessageBox.Show("Please choose .xls or .xlsx file only.""Warning", MessageBoxButtons.OK, MessageBoxIcon.Error); //custom messageBox to show error  
    20.         }  
    21.     }  
    22. }  
    23.   
    24. private void btnClose_Click(object sender, EventArgs e) {  
    25.     this.Close(); //to close the window(Form1)  
    26. }  
After choosing the file Result will be like the following whether you upload .xls or .xlsx file.

Choose and read file

HOW TO INSERT IMAGE IN DATA GRID VIEW IN C# WINDOWS FORM

Step 1: Open Visual Studio 2010, Go to File, New, Projects and under Visual C# select Windows.

Windows Application 

You can change the name of the project and browse your project to different location too. And then press – OK.

Design chamber

Step 2: Now open your Form1.cs file, where we create our design for DataGridView and Picture Image Control. We will drag a PictureBox, a button, and a DataGridView from toolbox to Form1.cs. You will see your Form look like this.

Form1.cs [Design]

design

Code chamber

Right Click on the blank part of Form1.cs and View Code. You will see you are entered in the code part of the form. Write the following code for Form1.cs.

Form1.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.IO;  
  10. namespace WindowsFormsApplication4  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.   
  19.         private void pictureBox1_Click(object sender, EventArgs e)  
  20.         {  
  21.             OpenFileDialog opnfd = new OpenFileDialog();  
  22.             opnfd.Filter = "Image Files (*.jpg;*.jpeg;.*.gif;)|*.jpg;*.jpeg;.*.gif";  
  23.             if (opnfd.ShowDialog() == DialogResult.OK)  
  24.             {  
  25.                 pictureBox1.Image = new Bitmap(opnfd.FileName);  
  26.   
  27.             }  
  28.   
  29.         }  
  30.   
  31.         private void button1_Click(object sender, EventArgs e)  
  32.         {  
  33.             MemoryStream mmst = new MemoryStream();  
  34.             pictureBox1.Image.Save(mmst, pictureBox1.Image.RawFormat);  
  35.             byte[] img = mmst.ToArray();  
  36.             dataGridView1.Rows.Add(img);  
  37.         }  
  38.   
  39.         private void Form1_Load(object sender, EventArgs e)  
  40.         {  
  41.   
  42.               
  43.             DataGridViewImageColumn dgvimgcol = new DataGridViewImageColumn();  
  44.             dgvimgcol.HeaderText = "Uploaded Image";  
  45.             dgvimgcol.ImageLayout = DataGridViewImageCellLayout.Stretch;  
  46.             
  47.             dataGridView1.Columns.Add(dgvimgcol);  
  48.          
  49.             dataGridView1.RowTemplate.Height = 250;  
  50.            
  51.             dataGridView1.AllowUserToAddRows = false;  
  52.         }  
  53.     }  
  54. }  
Output chamber

Output  



Hope you liked it. Thank you for reading. Have a good day.