Thursday, April 10, 2014

C# WPF Load Image from file

How to load image WPF:
 
image1.Source = new BitmapImage(new Uri(@"C:\IMAGE.JPG"));
Additional Reading at :
  1. MSDN BitmapImage
  2. MSDN Uri

C# Load Image to PictureBox

How to load image to picturebox:
Method 1 :
 
PictureBox1.Image = Image.FromFile(@"C:\IMAGE.JPG");
Method 2 :
 
PictureBox1.Image = New Bitmap(@"C:\IMAGE.JPG");
Additional Reading at :
  1. MSDN Bitmap
  2. MSDN Image

C# Convert Image to byte array (byte[]) or C# convert byte array (byte[]) to Image

Convert Image to byte[] method:
 
public byte[] imageToByteArray(Image imageIn)
{
    using (var ms = new System.IO.MemoryStream())
    {
        System.Drawing.Imaging.ImageFormat format;
        switch (imageIn.MimeType())
        {
            case "image/png":
                format = ImageFormat.Png;
                break;
            case "image/gif":
                format = ImageFormat.Gif;
                break;
            default:
                format = ImageFormat.Jpeg;
                break;
        }
        imageIn.Save(ms, format);
        return ms.ToArray();
    }
}
Convert byte[] to Image method:
 
public Image byteArrayToImage(byte[] byteArrayIn)
{
    using (var ms = new System.IO.MemoryStream(byteArrayIn))
    {
        Image returnImage = Image.FromStream(ms);
        return returnImage;
    }
}
Additional Reading at :
  1. MSDN ImageFormat
  2. MSDN Image.Save
  3. MSDN MemoryStream

Tuesday, August 20, 2013

C# Capturing Keys - KeyPress Event, KeyDown Event or KeyUp Event

Differences of the three:
  1. Keydown: happens when the key is pressed.
  2. KeyPressed: happens when a key is pressed and then released.
  3. KeyUp: happens when the key is released.

Sample Code below :-

1.Create a Textbox control.

2.Adding event to your control in form1.designer.cs
 
this.textbox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CaptureKeyPress);
this.textbox1.KeyDown += new System.Windows.Forms.KeyEventHandler(CaptureKeyDown);
this.textbox1.KeyUp += new System.Windows.Forms.KeyEventHandler(CaptureKeyUp);
3.Creating event handler
 
private void CaptureKeyPress(object sender, KeyPressEventArgs e)
{
    //key press capture key char
    //you can compare with keycode also
    if (e.KeyChar == (char)Keys.Enter)
    {
        MessageBox.Show("Enter Key Press");
    }
    //or can compare with ascii code
    if (e.KeyChar == 13) //13 in ascii code for enter
    {
        MessageBox.Show("Enter Key Press");
    }
}

private void CaptureKeyDown(object sender, KeyEventArgs e)
{
    //key down capture key code
    if (e.KeyCode == Keys.Enter) 
    {
        MessageBox.Show("Enter Key Down");
    }
}

private void CaptureKeyUp(object sender, KeyEventArgs e)
{
    //key up capture key code
    if (e.KeyCode == Keys.Enter) 
    {
        MessageBox.Show("Enter Key Up");
    }
}
get list of key char from ascii table.. link below..
get list of key code from keys enum.. link below..

Additional Reading at :
  1. ASCII Table
  2. List Keys Enumeration
  3. MSDN KeyPress
  4. MSDN KeyDown
  5. MSDN KeyUp

Thursday, May 2, 2013

C# ComboBox using DataTable

Load Combobox Using DataTable.
Make an OleDbConnection.
Create DataTable from OleDbDataReader.

Sample code:
 
//include this
using System.Data.OleDb;

//declare 
OleDbConnection OCON = null;
OleDbDataReader ODR = null;
//create connection
OCON = new OleDbConnection(@"Provider=SQLOLEDB;Data Source=192.168.0.1;Initial Catalog=TESTDATABASE;User ID=TESTUSER;Password=TESTPASS;");

try{
    //open connection
    OCON.Open();            
    
    //create command    
    OleDbCommand OCMD = null;        
    OCMD.CommandText = "SELECT ID, DESCRIPTION FROM TABLE";
    //execute command
    ODR = OCMD.ExecuteReader();
    
    //load datareader to datatable       
    DataTable DT = new DataTable();
    DT.Load(ODR);

    //attach datatable to combobox
    comboBox1.DisplayMember = "DESCRIPTION";
    comboBox1.ValueMember = "ID";
    comboBox1.DataSource = DT;

    //close connection
    OCON.Close();
}catch(OleDbException ex){
    MessageBox.Show(ex.ToString());
}
Additional Reading at :
  1. MSDN OleDb
  2. MSDN ComboBox