Click here to Skip to main content
15,898,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
 i want to retrieve image from sql database to picture box using c# coding,
one thing image is saved in database as varchar form.
Posted
Comments
Divya RS 17-Oct-12 1:11am    
You do google this , there lot of info for this in google

C#
MemoryStream ms1 = New MemoryStream;
 ms1.Write(im.Item(0), 0, im.Item(0).Length);
 PictureBox1.Image = Image.FromStream(ms1);



provided the image is stored in binary format in the database !!!!!
 
Share this answer
 
Comments
ravuravu 17-Oct-12 1:37am    
the image is stored in database as system.byte[]
philiplobo 17-Oct-12 1:41am    
ya so it has binary data written in tables cells where the image is stored right...??? this code will work fine for that ...!!!!
ravuravu 17-Oct-12 1:44am    
image is stored in tbEmployee table in the field name as Photo
ravuravu 17-Oct-12 1:45am    
data type is varchar
philiplobo 17-Oct-12 1:49am    
can u copy paste what data is stored in Photo col ...???
C#
byte[] big;
private System.Windows.Forms.OpenFileDialog OpenFD;




private void btnBrowse_Click(object sender, EventArgs e)
{
//this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
//openFileDialog1.ShowDialog();
//txtPhoto.Text = openFileDialog1.FileName;
//picImage.Image = "~/Images"+openFileDialog1.FileName;


this.OpenFD = new System.Windows.Forms.OpenFileDialog();
OpenFD.Title = "Select Files";
OpenFD.Filter = "Jpg|*.jpg|Jpge|*.jpge|Gif|*.gif";
OpenFD.FileName = null;
string fileName;
if (OpenFD.ShowDialog() != DialogResult.Cancel)
{
//querybuilder qu = new querybuilder();
fileName = OpenFD.FileName;
Object refmissing = System.Reflection.Missing.Value;
try
{
// show it to picturebox
picImage.Load(fileName);
// Here get_image is a function and Big is the byte[] type
big = get_image(fileName);
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex.Message.ToString());
}
}

/* OpenFileDialog fileOpen = new OpenFileDialog();
fileOpen.Title = "Open Image file";
fileOpen.Filter = "JPG Files (*.jpg)| *.jpg";

if (fileOpen.ShowDialog() == DialogResult.OK)
{
picImage.Image = Image.FromFile(fileOpen.FileName);
}
fileOpen.Dispose();*/
}

private byte[] get_image(string filePath)
{
FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(stream);
byte[] photo = reader.ReadBytes((int)stream.Length);
reader.Close();
stream.Close();

return photo;
}
 
Share this answer
 
Comments
zhale 28-Jan-13 4:08am    
i want to retrieve many image from sql database to picture boxes using c# coding,
one thing image is saved in database as image form.
VB
MemoryStream ms1 = New MemoryStream();
picturebox1.Image.Save(ms1, System.Drawing.Imaging.ImageFormat.Png)

'and when u insert data into the table
'place a parameter in the command 
'insert into table values(@a)
'assign the parameter value
cmd1.Parameters.AddWithValue("@a", ms1.ToArray);
'done ur image is saved to the database

</pre>
 
Share this answer
 
Comments
philiplobo 17-Oct-12 2:16am    
this code is smaller compared to yours and easy to understand... please try using this okhey !!!! :P :D
ravuravu 17-Oct-12 2:20am    
okhey but how can i retrive the image
ravuravu 17-Oct-12 2:20am    
pls help me in this
philiplobo 17-Oct-12 2:25am    
the first solution has d code for retriving the image from the database if u use my code to store image in database....!!!!! :P :D
ravuravu 17-Oct-12 2:29am    
k,i will try to use ur code,i have any doubt then i ask u

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900