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.

HOW TO USE BARCODE IN C# WINDOWS FORM

USE BARCODE WITH C# WINDOWS FORM
Barcode or Qrcode is the technique to encode the data ex. Text,url or etc into encoded image format to provide confidential access to the data. In shopping malls you may see they are scaning those barcode prited on products and then get the description of the perticuler product from their server database. For those kind of activity we have to generate those barcode.

Step 1: Download QRCODE GENERATOR LIBRARY from onbarcode.com.

Step 2: Open Visual Studio - Create New Project - Windows Form.

Step 3: Add reference to OnBarcode.Barcode.Winforms.dll.

Step 4: Design form with some input fields for accepting data to encode and the targeted location to save barcode generated image.

Step 5: To generate Barcode as well as Qrcode images write two differen methods as follows.
        private void GenerateBacode(string _data, string _filename)
        {
            Linear barcode = new Linear();
            barcode.Type = BarcodeType.CODE11;
            barcode.Data = _data;
            barcode.drawBarcode(_filename);
        }        private void GenerateQrcode(string _data, string _filename)
        {
            QRCode qrcode = new QRCode();
            qrcode.Data = _data;
            qrcode.DataMode = QRCodeDataMode.Byte;
            qrcode.UOM = UnitOfMeasure.PIXEL;
            qrcode.X = 3;
            qrcode.LeftMargin = 0;
            qrcode.RightMargin = 0;
            qrcode.TopMargin = 0;
            qrcode.BottomMargin = 0;
            qrcode.Resolution = 72;
            qrcode.Rotate = Rotate.Rotate0;
            qrcode.ImageFormat = ImageFormat.Gif;
            qrcode.drawBarcode(_filename);
        }


Barcode:
Barcode in C#

Qrcode:

qrcode in C#


How To Shut Down a machine How to Log Off a machine How to forced log off a machine How to restart a machine using c# Windows form


To perform our task, very first let us create a windows application project.   On form drag and drop four buttons for four different operations.  After design form will look like below 

1.gif 

Navigate to code behind of form and add reference of System.Runtime.InteropServices

Add a static extern method to Form.cs

[DllImport("user32.dll")]
public static extern int ExitWindowsEx(int operationFlag, int operationReason);

Log off  the System 

On click event of Log Off button, call ExitWindowsEX method with proper flag.  For log off  operation flag value is 0. 

private void btnLogOff_Click(object sender, EventArgs e)
{
    ExitWindowsEx(0, 0);
}

Forced Log off the System 

On click event of Forced Log Off button, call ExitWindowsEX method with proper flag.  For Forced  log off  operation flag value is 4. 

private void btnForcedLogOff_Click(object sender, EventArgs e)
{
   ExitWindowsEx(4, 0);
}

Shut Down the System

On click event of Shut down button, call ExitWindowsEX method with proper flag.  For shut down  operation flag value is 1. 

private void btnShutDown_Click(object sender, EventArgs e)
{
    ExitWindowsEx(1, 0);
}

Restart the System 

On click event of Restart button, call ExitWindowsEX method with proper flag.  For Restart  operation flag value is 2. 

private void btnRestart_Click(object sender, EventArgs e)
{
    ExitWindowsEx(2, 0);
}

Now when you run the application all system operation should be performed. 

For your reference full source code is given here 

Form.cs

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.Runtime.InteropServices;

namespace SystmShutDownApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        [DllImport("user32.dll")]
        public static extern int ExitWindowsEx(int operationFlag, int operationReason);
        private void btnRestart_Click(object sender, EventArgs e)
        {
              ExitWindowsEx(2, 0);
        }
        private void btnLogOff_Click(object sender, EventArgs e)
        {
            ExitWindowsEx(0, 0);
        }
        private void btnForcedLogOff_Click(object sender, EventArgs e)
        {
           ExitWindowsEx(4, 0);
        }
        private void btnShutDown_Click(object sender, EventArgs e)
        {
            ExitWindowsEx(1, 0);
        }
    }
}