| Author: Shivshanker Cheral 11 Jun 2007 | Member Level: Diamond | Rating:  Points: 2 |
you can store the physical address of the image into database and while load in a page set this image to image and show it
|
| Author: ChandraShekar Thota 11 Jun 2007 | Member Level: Diamond | Rating:  Points: 2 |
field of type IMAGE
<PRE lang=sql>CREATE TABLE [dbo].[tblImgData] (
[ID] [int] NOT NULL ,
[Name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Picture] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]</PRE>
Actually IMAGE field is just holding the reference to the page containing the binary data so we have to convert our image into bytes. I used a file open dialog box to locate the file. this.openFileDialog1.ShowDialog(this); string strFn=this.openFileDialog1.FileName; By using FileInfo class, I retrieved the file size: FileInfo fiImage=new FileInfo(strFn); Declare an array of that size. this.m_lImageFileLength=fiImage.Length; m_barrImg=new byte[Convert.ToInt32(this.m_lImageFileLength)]; By using FileStream object, I filled the byte array. FileStream fs=new FileStream(strFn,FileMode.Open, FileAccess.Read,FileShare.Read); int iBytesRead=fs.Read(m_barrImg,0, Convert.ToInt32(this.m_lImageFileLength)); fs.Close(); Complete Load Image Code
<OL> <LI> <H3>Complete Save Image Code</H3> <DIV class=precollapse id=premain10 style="WIDTH: 100%"><IMG id=preimg10 style="CURSOR: hand" height=9 src="http://www.codeproject.com/images/minus.gif" width=9 preid="10"> Collapse</DIV><PRE lang=cs id=pre10 style="MARGIN-TOP: 0px">private void btnSave_Click(object sender, System.EventArgs e) { try { this.sqlConnection1.Open(); if (sqlCommand1.Parameters.Count ==0 ) { this.sqlCommand1.CommandText="INSERT INTO tblImgData(ID," + " Name,Picture) values(@ID,@Name,@Picture)"; this.sqlCommand1.Parameters.Add("@ID", System.Data.SqlDbType.Int,4); this.sqlCommand1.Parameters.Add("@Name", System.Data.SqlDbType.VarChar,50); this.sqlCommand1.Parameters.Add("@Picture", System.Data.SqlDbType.Image); }
this.sqlCommand1.Parameters["@ID"].Value=this.editID.Text; this.sqlCommand1.Parameters["@Name"].Value=this.editName.Text; this.sqlCommand1.Parameters["@Picture"].Value=this.m_barrImg;
int iresult=this.sqlCommand1.ExecuteNonQuery(); MessageBox.Show(Convert.ToString(iresult)); } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { this.sqlConnection1.Close(); } }</PRE></LI></OL> <H2>Retrieving Image</H2> <P>Retrieving images from the database is the exact reverse process of saving images to the database.</P> <OL> <LI>First create command text to retrieve record. <PRE lang=cs>SqlCommand cmdSelect = new SqlCommand("select Picture" + " from tblImgData where ID=@ID", this.sqlConnection1);</PRE> <LI>Create parameter for the query. <PRE lang=cs>cmdSelect.Parameters.Add("@ID",SqlDbType.Int,4);</PRE> <LI>Provide value to the parameter. <PRE lang=cs>cmdSelect.Parameters["@ID"].Value=this.editID.Text;</PRE> <LI>Open database connection and execute “<CODE>ExecuteScalar</CODE>” because we want only “<CODE>IMAGE</CODE>” column data back. <PRE lang=cs>byte[] barrImg=(byte[])cmdSelect.ExecuteScalar();</PRE> <P>As the execute scalar returns data of “<CODE lang=cs>Object</CODE>” data type, we cast it to <CODE lang=cs>byte</CODE> array.</P> <LI>Save this data to a temporary file. <PRE lang=cs>string strfn=Convert.ToString(DateTime.Now.ToFileTime()); FileStream fs=new FileStream(strfn,FileMode.CreateNew,FileAccess.Write); fs.Write(barrImg,0,barrImg.Length); fs.Flush(); fs.Close();</PRE> <LI>And display the image anywhere you want to display. <PRE lang=cs>pictureBox1.Image=Image.FromFile(strfn);</PRE></LI></OL> <H3>Complete Image Retrieving Code</H3> <DIV class=precollapse id=premain17 style="WIDTH: 100%"><IMG id=preimg17 style="CURSOR: hand" height=9 src="http://www.codeproject.com/images/minus.gif" width=9 preid="17"> Collapse</DIV><PRE lang=cs id=pre17 style="MARGIN-TOP: 0px">private void btnLoad_Click(object sender, System.EventArgs e) { try { SqlCommand cmdSelect=new SqlCommand("select Picture" + " from tblImgData where ID=@ID",this.sqlConnection1); cmdSelect.Parameters.Add("@ID",SqlDbType.Int,4); cmdSelect.Parameters["@ID"].Value=this.editID.Text;
this.sqlConnection1.Open(); byte[] barrImg=(byte[])cmdSelect.ExecuteScalar(); string strfn=Convert.ToString(DateTime.Now.ToFileTime()); FileStream fs=new FileStream(strfn, FileMode.CreateNew, FileAccess.Write); fs.Write(barrImg,0,barrImg.Length); fs.Flush(); fs.Close(); pictureBox1.Image=Image.FromFile(strfn); } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { this.sqlConnection1.Close(); } }</PRE>
chandrashekarthota@gmail.com /92 93 95 95 39 FREE SESSIONS ON .NET AND VISUALSTUDIO
|
| Author: Sandeep 12 Jun 2007 | Member Level: Silver | Rating:  Points: 2 |
Hi,
See the following links:
1. http://aspalliance.com/1083 [Without using Stored Pr] 2. http://aspalliance.com/1072 [Using Stored Proc]
Hope this will help you.
Thanks
Sandeep Acharya
|