1 保存图片到数据库中
转化为byte数组
OpenFileDialog openfile = new OpenFileDialog();
openfile.Title = "请选择客户端longin的图片";
openfile.Filter = "Login图片(*.jpg;*.bmp;*png)|*.jpeg;*.jpg;*.bmp;*.png|AllFiles(*.*)|*.*";
if (DialogResult.OK == openfile.ShowDialog())
{
try
{
if (openfile.FileName.Trim() != " ")
{
FileInfo fi = new FileInfo( openfile.FileName);
string imgtitle = openfile.FileName;
int imgdatalen = (int)fi.Length;
byte[] imgdata = new byte[imgdatalen];
Stream imgdatastream = fi.OpenRead();
int n = imgdatastream.Read(imgdata, 0, imgdatalen);
SqlConnection conn = new SqlConnection(ConnString);
string mySelectQuery = "INSERT INTO imagetable(image) VALUES ( @imgdata ) ";
//string mySelectQuery= "UPDATE ImageStore set imgtitle=@imgtitle,imgdata=@imgdata " ;
SqlCommand myCommand = new SqlCommand(mySelectQuery, conn);
//SqlParameter paramTitle = new SqlParameter("@imgtitle ", SqlDbType.VarChar, 50);
//paramTitle.Value = imgtitle;
//myCommand.Parameters.Add(paramTitle);
SqlParameter paramData = new SqlParameter("@imgdata ", SqlDbType.Image);
paramData.Value = imgdata;
myCommand.Parameters.Add(paramData);
conn.Open();
int numRowsAffected = myCommand.ExecuteNonQuery();
conn.Close();
}
}
}
2 保存路径到数据库中
1.在你的窗体上放一个pictureBox控件(ListView控件也可以),(如果有多个图片,则可以再放imageList控件和Timer控件定时循环显示,imagelist要和pictureBox关联起来.用FOR循环的话不放imageList控件也可以).
2.你用SQL语句找到的路径(Path)赋值给一个string类型的变量(要是多路径的话就赋值给一个数组).
3.在Form1_Load事件中添如下代码:
void Form1_Load(object sender, EventArgs e)
{
//SQLPath:是你用SQL语句找到的路径
string Path= SQLPath;
//在pictureBox控件中显示你的图片
pictureBox1.Image = Image.FromFile(Path);
}