1. physicaly store
2. Store in Database (Blob)
i give the code of store image in oracle database..
//when the image has a valid path
if(imgname!="")
{
FileStream fs;
fs=new FileStream(@imgname,FileMode.Open,FileAccess.Read);
//a array of byte to read the image
byte[] blobimg = new byte[fs.Length];
fs.Read(blobimg,0,System.Convert.ToInt32(fs.Length));
fs.Close();
// open database and excute below query...
// emp is a table name where three column is id (nvarchar), name(nvarchar) and photo(img)
query="insert into emp(id,name,photo) values(" + txtid.Text + "," + "`" + txtname.Text + "`," + " :BlobParameter )";
OracleParameter blobParameter = new OracleParameter();
blobParameter.OracleType = OracleType.Blob;
blobParameter.ParameterName = "BlobParameter";
blobParameter.Value = blobimg;
cmnd=new OracleCommand(query,conn);
cmnd.Parameters.Add(blobParameter);
cmnd.ExecuteNonQuery();
MessageBox.Show("Succesfully done!");
}
Read img from database
There is use following contros:
1. combobox:cmbname
2. label:lblname
3. buttons:btnshow,btnclose
4. picturebox:img
global declare these variable:
OracleConnection con;
OracleDataAdapter empadp;
DataSet ds;
string constr;
//bind name of img in combobox at page load event
constr="User Id=sc;Password=t;Data Source=st;";
con=new OracleConnection(connstr);
con.Open();
empadp=new OracleDataAdapter();
empadp.SelectCommand=new OracleCommand("SELECT * FROM emp",conn);
ds=new DataSet("dset");
empadp.Fill(ds);
DataTable dt;
dt=ds.Tables[0];
cmbname.Items.Clear();
foreach(DataRow dr in dt.Rows)
{
cmbname.Items.Add(dr[1].ToString());
cmbname.SelectedIndex=0;
}
// show img when select name of img from combobox,
DataTable dt = ds.Tables[0];
//if there is an already an image in picturebox, then delete it
if(img.Image != null)
{
img.Image.Dispose();
}
//using filestream object write the column as bytes and store it as an image
FileStream FS = new FileStream("image.jpg", FileMode.Create);
foreach(DataRow dr in dt.Rows)
{
if(dr[1].ToString() == cmbname.SelectedItem.ToString())//check name of the image
{
byte[] blob = (byte[])dr[2];
FS.Write(blob,0,blob.Length);
FS.Close();
FS = null;
img.Image = Image.FromFile("image.jpg");
img.SizeMode = PictureBoxSizeMode.StretchImage;
img.Refresh();