如何在数据库中保存图像并从SQL Server数据库中获取图片框中的图片 [英] How to save image in database and get back picture in picturebox from SQL Server database
问题描述
IM工作在Tiers应用程序我保存的图片可能是正确的方式或错误的方式但是在Sql server中保存图像后数据库工作n_tiers c#windows应用程序我无法回来图片PLZ一些人帮我这里是我的一些保存的代码,但我没有重新检索代码检查我的代码我错了,并且plz发给我一些代码来检索代码。
我在DataAccessLayer上写的这个函数代码。
Hi, I M WORKING ON Tiers application i saved picture may be its right way or wrong way but after saving image in Sql server Database workthroug n_tiers c# windows application i can't get back Image plz some one help me here is my some code for saving but i don't have retriving code check my code where i m wrong and plz send me some one code for retrieving code too.
this code of function i wrote on my DataAccessLayer.
public int insertdata(BO.Person person)
{
SqlConnection con=new SqlConnection(connectionstring);
try
{
con.Open();
command = new SqlCommand("sp_Insertdatavalues", con);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@ID", person.ID);
command.Parameters.AddWithValue("@name",person.Name);
command.Parameters.AddWithValue("@dates", person.UserDate);
command.Parameters.AddWithValue("@address", person.Address);
command.Parameters.AddWithValue("@age", person.Age);
command.Parameters.AddWithValue("@gender", person.Gender);
command.Parameters.AddWithValue("@dateofbirth", person.Dateofbirth);
command.Parameters.AddWithValue("@img", person.Image);
return command.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
command.Dispose();
con.Close();
con.Dispose();
}
}
===================== ================================================== ==============
我在btnbrowpic上写的代码拍照
=====================================================================================
this code i wrote on btnbrowpic to take picture
private void btnbrowspic_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "JPG Files(*.jpg)|*.jpg|GIF Files(*gif)|*gif|ALL Files(*.*)|*.*";
dlg.Title = "SELECT PICTURE";
if (dlg.ShowDialog() == DialogResult.OK)
{
imgloc = dlg.FileName.ToString();
pictureBox1.ImageLocation = imgloc;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
================= ================================================== ==========================
和我在btnsave_click活动上写的这段代码
private void button1_Click(object sender,EventArgs e)
{
byte [] imagebt = null;
FileStream fs = new FileStream(imgloc,FileMode.Open,FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
imagebt = br.ReadBytes((int)fs.Length);
BO.Person person = new BO.Person();
person.ID = Convert.ToInt32(textBox1.Text);
person.Name = textBox2.Text;
person.UserDate = dateTimePicker1 .Value.Date;
person.Address = textBox3.Text;
person.Age = Convert.ToInt32(textBo x4.Text);
person.Dateofbirth = dateTimePicker2.Value.Date;
person.Gender =性别;
int i = BitConverter。 ToInt32(imagebt,0);
person.Image = BitConverter.GetBytes(i);
BAL.BALPerson pBAL = new BAL.BALPerson();
pBAL.insertdata(person);
}
plz一些支票并给我图像翻新代码我也将是Gratfull
=============================================================================================
and this code i wrote on btnsave_click event
private void button1_Click(object sender, EventArgs e)
{
byte[] imagebt = null;
FileStream fs = new FileStream(imgloc, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
imagebt = br.ReadBytes((int)fs.Length);
BO.Person person = new BO.Person();
person.ID = Convert.ToInt32(textBox1.Text);
person.Name = textBox2.Text;
person.UserDate = dateTimePicker1.Value.Date;
person.Address = textBox3.Text;
person.Age = Convert.ToInt32(textBox4.Text);
person.Dateofbirth = dateTimePicker2.Value.Date;
person.Gender = Gender;
int i = BitConverter.ToInt32(imagebt, 0);
person.Image = BitConverter.GetBytes(i);
BAL.BALPerson pBAL = new BAL.BALPerson();
pBAL.insertdata(person);
}
plz some one check and give me image retriving code too i will be Gratfull
推荐答案
尝试以这种方式保存图像,这更简单,
不需要使用位转换器。
Hi,
Try Saving image in this way, this is simpler,
do not need to use bit converter.
MemoryStream stream = new MemoryStream();
pictureBox1.Image.Save(stream, ImageFormat.Jpeg);
Byte[] imageArray = stream.ToArray();
BO.Person person = new BO.Person();
person.ID = Convert.ToInt32(textBox1.Text);
person.Name = textBox2.Text;
person.UserDate = dateTimePicker1.Value.Date;
person.Address = textBox3.Text;
person.Age = Convert.ToInt32(textBox4.Text);
person.Dateofbirth = dateTimePicker2.Value.Date;
person.Gender = Gender;
person.Image = imageArray; //Image Array.
BAL.BALPerson pBAL = new BAL.BALPerson();
pBAL.insertdata(person);
从数据库获取数据(图片)时
And when you get data(Image) from database
Byte[] imageArray = person.Image; // If your image is not in byte array format try converting it to Byte[]
MemoryStream stream = new MemoryStream(imageArray);
pictureBox1.Image = Image.FromStream(Stream);
希望这会有所帮助!
Hope this helps!
这篇关于如何在数据库中保存图像并从SQL Server数据库中获取图片框中的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!