C#将相片转换成二进制存储在数据库中,再从数据库中显示出来
2011-04-18 21:58
435 查看
#region 用于在PictureBox控件中显示选择的图片
/// <summary>
/// 用于在PictureBox控件中显示选择的图片
/// </summary>
/// <param name="openF">图像名</param>
/// <param name="MyImage">pictureBox控件ID</param>
public void Read_Image(OpenFileDialog openF, PictureBox MyImage)//显示选择的图片
{
//指定OpenFileDialog控件打开的文件格式
openF.Filter = "*.jpg|*.jpg|*.bmp|*.bmp";
if (openF.ShowDialog()==DialogResult.OK)
{
try
{
//将图片文件存入到PictureBox控件中
MyImage.Image = System.Drawing.Image.FromFile(openF.FileName);
}
catch (Exception)
{
//弹出错误信息
MessageBox.Show("您选择的图片不能被读取或文件类型不对!","错误",MessageBoxButtons.OK,MessageBoxIcon.Warning);
throw;
}
}
}
#endregion
#region 用于将图片以二进制形式存入数据库中
/// <summary>
/// 用于将图片以二进制形式存入数据库中
/// </summary>
/// <param name="FilmID">影片ID</param>
/// <param name="openF"></param>
public void SaveImage(string FilmID, OpenFileDialog openF)//将图片以二进制存入数据库中
{
string strimg = openF.FileName.ToString(); //记录图片的所在路径
FileStream fs = new FileStream(strimg, FileMode.Open, FileAccess.Read); //将图片以文件流的形式进行保存
BinaryReader br = new BinaryReader(fs);
byte[] imgBytesIn = br.ReadBytes((int)fs.Length);//将流读入到字节数组中
SqlConnection conn = sqlhelper.getcon();
conn.Open();
StringBuilder strSql = new StringBuilder();
strSql.Append("update T_Film Set F_FPhoto=@Photo where F_FId=" + FilmID);
SqlCommand cmd = new SqlCommand(strSql.ToString(), conn);
cmd.Parameters.Add("@Photo", SqlDbType.Binary).Value = imgBytesIn;
cmd.ExecuteNonQuery();
conn.Close();
}
#endregion
#region 用于将图片从数据库中取出并显示在PictureBox控件中
/// <summary>
/// 用于将图片从数据库中取出并显示在PictureBox控件中
/// </summary>
/// <param name="FilmID">影片ID</param>
/// <param name="pb">PictureBox控件ID</param>
public void Get_Image(string FilmID, PictureBox pb)//将图片从数据库中取出
{
byte[] imagebytes = null;
SqlConnection conn = sqlhelper.getcon();
conn.Open();
SqlCommand com = new SqlCommand("select * from T_Film where F_FId='" + FilmID + "'", conn);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
imagebytes = (byte[])dr.GetValue(10);
}
dr.Close();
conn.Close();
MemoryStream ms = new MemoryStream(imagebytes);
Bitmap bmpt = new Bitmap(ms);
pb.Image = bmpt;
}
#endregion
上面三个分别为公共类,第一个为打开对话框,第二个为将图像以二进制存入数据库,第三个为从数据库中读取出来。
在窗体代码中, Mymenu.Get_Image(FilmID, picboxPhoto);直接用就可以了(Mymenu)为公共类空间名称
/// <summary>
/// 用于在PictureBox控件中显示选择的图片
/// </summary>
/// <param name="openF">图像名</param>
/// <param name="MyImage">pictureBox控件ID</param>
public void Read_Image(OpenFileDialog openF, PictureBox MyImage)//显示选择的图片
{
//指定OpenFileDialog控件打开的文件格式
openF.Filter = "*.jpg|*.jpg|*.bmp|*.bmp";
if (openF.ShowDialog()==DialogResult.OK)
{
try
{
//将图片文件存入到PictureBox控件中
MyImage.Image = System.Drawing.Image.FromFile(openF.FileName);
}
catch (Exception)
{
//弹出错误信息
MessageBox.Show("您选择的图片不能被读取或文件类型不对!","错误",MessageBoxButtons.OK,MessageBoxIcon.Warning);
throw;
}
}
}
#endregion
#region 用于将图片以二进制形式存入数据库中
/// <summary>
/// 用于将图片以二进制形式存入数据库中
/// </summary>
/// <param name="FilmID">影片ID</param>
/// <param name="openF"></param>
public void SaveImage(string FilmID, OpenFileDialog openF)//将图片以二进制存入数据库中
{
string strimg = openF.FileName.ToString(); //记录图片的所在路径
FileStream fs = new FileStream(strimg, FileMode.Open, FileAccess.Read); //将图片以文件流的形式进行保存
BinaryReader br = new BinaryReader(fs);
byte[] imgBytesIn = br.ReadBytes((int)fs.Length);//将流读入到字节数组中
SqlConnection conn = sqlhelper.getcon();
conn.Open();
StringBuilder strSql = new StringBuilder();
strSql.Append("update T_Film Set F_FPhoto=@Photo where F_FId=" + FilmID);
SqlCommand cmd = new SqlCommand(strSql.ToString(), conn);
cmd.Parameters.Add("@Photo", SqlDbType.Binary).Value = imgBytesIn;
cmd.ExecuteNonQuery();
conn.Close();
}
#endregion
#region 用于将图片从数据库中取出并显示在PictureBox控件中
/// <summary>
/// 用于将图片从数据库中取出并显示在PictureBox控件中
/// </summary>
/// <param name="FilmID">影片ID</param>
/// <param name="pb">PictureBox控件ID</param>
public void Get_Image(string FilmID, PictureBox pb)//将图片从数据库中取出
{
byte[] imagebytes = null;
SqlConnection conn = sqlhelper.getcon();
conn.Open();
SqlCommand com = new SqlCommand("select * from T_Film where F_FId='" + FilmID + "'", conn);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
imagebytes = (byte[])dr.GetValue(10);
}
dr.Close();
conn.Close();
MemoryStream ms = new MemoryStream(imagebytes);
Bitmap bmpt = new Bitmap(ms);
pb.Image = bmpt;
}
#endregion
上面三个分别为公共类,第一个为打开对话框,第二个为将图像以二进制存入数据库,第三个为从数据库中读取出来。
在窗体代码中, Mymenu.Get_Image(FilmID, picboxPhoto);直接用就可以了(Mymenu)为公共类空间名称
相关文章推荐
- C#将相片转换成二进制存储在数据库中,再从数据库中显示出来
- C#将相片转换成二进制存储在数据库中,再从数据库中显示出来
- Asp.net(c#)将数据库中以二进制存的图片显示出来
- Asp.net(c#)将数据库中以二进制存的图片显示出来
- C# 用二进制将图片保存在数据库里再读出来显示在pictureBox中
- C#把图片转换字节存入数据库在读取显示出来。
- C# extjs 图片、附件以二进制形式存储至数据库,图片显示,附件下载
- Asp.net 2.0(C#)图片存储到数据库和从数据库读取显示
- .net 图片的二进制数据库存储和显示
- 与图片的二进制数据库存储和显示
- C#中将图片文件转化为二进制数组-用于数据库存储
- .net 图片的二进制数据库存储和显示
- 与图片的二进制数据库存储和显示
- ASP.net(c#)将图片以二进制格式存储到数据库中
- c# winform下图片二进制存储-改变大小-显示
- 图片的二进制数据库存储和显示
- PB 如果把PDF 转换成二进制数据保存到数据库,并读取显示
- C# asp中,如何把二进制图片流转换成图片,并显示在网页上的一些注意事项
- 用C#实现图片数据库存储与显示
- Asp.net 2.0(C#)图片存储到数据库和从数据库读取显示