sql 2005
求教如何将图片转化为二进制流 存到数据库
然后再在网页上显示
各位大侠指导一下
最好有代码 加注解
13 个解决方案
#1
fileStream, 文件读出来就是byte[], sql2005的列类型设置成二进制,保存即可
#2
能给一段代码么
还有 那咋读出来呢
读出来最好也给一段代码
还有 那咋读出来呢
读出来最好也给一段代码
#4
如何图片转化为二进制流?
你把它按照二进制文件去读,读出来的不就是个二进制流嘛
真是的,这都想不明白?
你把它按照二进制文件去读,读出来的不就是个二进制流嘛
真是的,这都想不明白?
#5
#6
if (FileUpLogo.HasFile)
{
//取得上传文件的大小
int FileLen = FileUpLogo.PostedFile.ContentLength;
Byte[] FileData = new Byte[FileLen];
//创建访问客户端上传文件的对象
HttpPostedFile hp = FileUpLogo.PostedFile;
//创建数据流对象
System.IO.Stream sr = hp.InputStream;
//将图片数据放到FileData数组对象实例中,0代表数组指针的起始位置,FileLen代表指针的结束位置
sr.Read(FileData, 0, FileLen);
//将FileData 赋值给实体
brandModel.fld_logo = FileData;
}
#7
首先数据库里面有一字段类型为image,我这里是一个窗体应用程序,也是将图片以二进制的形式保存至数据库的,思路是一样的
byte[] photo;
public byte[] Photo
{
get { return photo; }
set { photo = value; }
}
FileStream fs;
string fileName = string.Empty;
private void btnChooose_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Filter = "*.jpg|*.jpg|*.gif|*.gif|*.bmp|*.bmp";
if (fileDialog.ShowDialog() == DialogResult.OK)
{
fileName = fileDialog.FileName;
fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
pictureBox.Image = Image.FromStream(fs);
fs.Close();
}
}
private void btnSave_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(fileName))
{
fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
byte[] imageByte = new byte[fs.Length];
fs.Read(imageByte, 0, (int)fs.Length);
Photo = imageByte;
fs.Close();
AddImage();
}
else
{
MessageBox.Show("请选择图片");
}
}
private void AddImage()
{
using (SqlConnection connection = new SqlConnection(connString))
{
connection.Open();
SqlCommand command = new SqlCommand("PROC_ADDIMAGE", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@IMAGE", SqlDbType.Image).Value = Photo;
int rows = command.ExecuteNonQuery();
if (rows > 0)
{
MessageBox.Show("添加成功");
}
else
{
MessageBox.Show("添加失败");
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
int index;
/// <summary>
/// 获取图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGetImage_Click(object sender, EventArgs e)
{
/// <summary>
/// 存图片的集合
/// </summary>
IList<Form1> form1List = new List<Form1>();
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
SqlCommand command = new SqlCommand("PROC_GETIMAGE", conn);
command.CommandType = CommandType.StoredProcedure;
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Form1 form1 = new Form1();
form1.Photo = (byte[])reader["Photo"];
form1List.Add(form1);
}
}
}
if (form1List.Count == 1)
{
MemoryStream imageStream = new MemoryStream(form1List[0].Photo);
pictureBox.Image = Image.FromStream(imageStream);
}
else if (form1List.Count > 1)
{
index++;
if (index < form1List.Count)
{
btnGetImage.Text = "下一张";
MemoryStream imageStream = new MemoryStream(form1List[index].Photo);
pictureBox.Image = Image.FromStream(imageStream);
}
else
{
index=0;
MemoryStream imageStream = new MemoryStream(form1List[index].Photo);
pictureBox.Image = Image.FromStream(imageStream);
}
}
else
{
MessageBox.Show("存中还没有图片");
}
}
/// <summary>
/// 清除图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClear_Click(object sender, EventArgs e)
{
pictureBox.Image = null;
}
#8
try
{
string ImgPath = this.FileUpload1.PostedFile.FileName;
string ImgName = ImgPath.Substring(ImgPath.LastIndexOf("\\") + 1);
string ImgExtend = ImgPath.Substring(ImgPath.LastIndexOf(".") + 1);
if (ImgExtend != "bmp" || ImgExtend != "gif" || ImgExtend != "jpg")//验证图片格式
{
this.Label1.Text = "上传图片的格式不正确";
return;
}
int FileLen = this.FileUpload1.PostedFile.ContentLength;
Byte[] FileData = new Byte[FileLen];
HttpPostedFile hp = FileUpload1.PostedFile;
Stream sr = hp.InputStream;
sr.Read(FileData, 0, FileLen);
//这段数据库操作代码建议你 好好封转一下 不要直接 写在前台 我是临时写的 呵呵
SqlConnection con = new SqlConnection("数据库连接字符串");
con.Open();
SqlCommand com = new SqlCommand("insert into table (数据列明) values (@imgdata)", con);
com.Parameters.Add("@imgdata", SqlDbType.Image);
com.Parameters["@imgdata"].Value = FileData;
com.ExecuteNonQuery();
Label1.Text = "保存成功!";
}
catch(Exception err)
{
Label1.Text = "处理失败!原因为:" + err.ToString();
}
{
string ImgPath = this.FileUpload1.PostedFile.FileName;
string ImgName = ImgPath.Substring(ImgPath.LastIndexOf("\\") + 1);
string ImgExtend = ImgPath.Substring(ImgPath.LastIndexOf(".") + 1);
if (ImgExtend != "bmp" || ImgExtend != "gif" || ImgExtend != "jpg")//验证图片格式
{
this.Label1.Text = "上传图片的格式不正确";
return;
}
int FileLen = this.FileUpload1.PostedFile.ContentLength;
Byte[] FileData = new Byte[FileLen];
HttpPostedFile hp = FileUpload1.PostedFile;
Stream sr = hp.InputStream;
sr.Read(FileData, 0, FileLen);
//这段数据库操作代码建议你 好好封转一下 不要直接 写在前台 我是临时写的 呵呵
SqlConnection con = new SqlConnection("数据库连接字符串");
con.Open();
SqlCommand com = new SqlCommand("insert into table (数据列明) values (@imgdata)", con);
com.Parameters.Add("@imgdata", SqlDbType.Image);
com.Parameters["@imgdata"].Value = FileData;
com.ExecuteNonQuery();
Label1.Text = "保存成功!";
}
catch(Exception err)
{
Label1.Text = "处理失败!原因为:" + err.ToString();
}
#9
public class ImageManage
{
public static string GetFileName(FileUpload fuValues)
{
System.Web.UI.Page pe = new Page();
string loginname = pe.Session["UserName"].ToString();
int allowlength = 1024 * 1024 * 10;//限制文件大小
string filename = Path.GetFileName(fuValues.PostedFile.FileName);
if (fuValues.HasFile)
{
string size = fuValues.PostedFile.ContentLength.ToString(); //获取已上传文件夹的大小
string type = Path.GetExtension(fuValues.PostedFile.FileName); //得到文件的后缀
//string fullpath = Server.MapPath("~/UserDocument/" + loginname + "/Picture/");//文件的保存路径
string fullpath = "C:\\CESS\\" + loginname + "\\";
string vsnewname = System.DateTime.Now.ToString("yyyyMMddHHmm_");//声称文件名,防止重复
//string path = "UserDocument/" + loginname + "/Picture/" + vsnewname;
if (fuValues.PostedFile.ContentLength < allowlength)
{
if (type == ".bmp" || type == ".png" || type == ".jpg" || type == ".gif" || type == ".BMP" ||
type == ".PNG" || type == ".JPG" || type == ".GIF")
{
if (!Directory.Exists(fullpath))//判断上传文件夹是否存在,若不存在,则创建
{
//创建文件夹
Directory.CreateDirectory(fullpath);
fuValues.PostedFile.SaveAs(fullpath + vsnewname + filename);
return fullpath + vsnewname + filename;
}
else
{
fuValues.PostedFile.SaveAs(fullpath + vsnewname + filename);
return fullpath + vsnewname + filename;
}
}
else
{
pe.Response.Write("<script>alert('上传文件格式错误!');history.go(-1);</script>");
return "";
}
}
else
{
pe.Response.Write("<script>alert('上传文件太大!');history.go(-1);</script>");
return "";
}
}
else
{
pe.Response.Write("<script>alert('请上传文件!');history.go(-1);</script>");
return "";
}
}
}
#10
擦, 贴错了,,,
#11
private Byte[] EditImg()
{
string strURL = this.fuEditImage.PostedFile.FileName;
//将需要存储的图片读取为数据流
FileStream fs = new FileStream(strURL, FileMode.Open, FileAccess.Read);
Byte[] btye2 = new byte[fs.Length];
fs.Read(btye2, 0, Convert.ToInt32(fs.Length));
fs.Close();
return btye2;
{
string strURL = this.fuEditImage.PostedFile.FileName;
//将需要存储的图片读取为数据流
FileStream fs = new FileStream(strURL, FileMode.Open, FileAccess.Read);
Byte[] btye2 = new byte[fs.Length];
fs.Read(btye2, 0, Convert.ToInt32(fs.Length));
fs.Close();
return btye2;
#12
#5楼弟兄到哪都贴广告。。。
#1
fileStream, 文件读出来就是byte[], sql2005的列类型设置成二进制,保存即可
#2
能给一段代码么
还有 那咋读出来呢
读出来最好也给一段代码
还有 那咋读出来呢
读出来最好也给一段代码
#3
#4
如何图片转化为二进制流?
你把它按照二进制文件去读,读出来的不就是个二进制流嘛
真是的,这都想不明白?
你把它按照二进制文件去读,读出来的不就是个二进制流嘛
真是的,这都想不明白?
#5
#6
if (FileUpLogo.HasFile)
{
//取得上传文件的大小
int FileLen = FileUpLogo.PostedFile.ContentLength;
Byte[] FileData = new Byte[FileLen];
//创建访问客户端上传文件的对象
HttpPostedFile hp = FileUpLogo.PostedFile;
//创建数据流对象
System.IO.Stream sr = hp.InputStream;
//将图片数据放到FileData数组对象实例中,0代表数组指针的起始位置,FileLen代表指针的结束位置
sr.Read(FileData, 0, FileLen);
//将FileData 赋值给实体
brandModel.fld_logo = FileData;
}
#7
首先数据库里面有一字段类型为image,我这里是一个窗体应用程序,也是将图片以二进制的形式保存至数据库的,思路是一样的
byte[] photo;
public byte[] Photo
{
get { return photo; }
set { photo = value; }
}
FileStream fs;
string fileName = string.Empty;
private void btnChooose_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Filter = "*.jpg|*.jpg|*.gif|*.gif|*.bmp|*.bmp";
if (fileDialog.ShowDialog() == DialogResult.OK)
{
fileName = fileDialog.FileName;
fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
pictureBox.Image = Image.FromStream(fs);
fs.Close();
}
}
private void btnSave_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(fileName))
{
fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
byte[] imageByte = new byte[fs.Length];
fs.Read(imageByte, 0, (int)fs.Length);
Photo = imageByte;
fs.Close();
AddImage();
}
else
{
MessageBox.Show("请选择图片");
}
}
private void AddImage()
{
using (SqlConnection connection = new SqlConnection(connString))
{
connection.Open();
SqlCommand command = new SqlCommand("PROC_ADDIMAGE", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@IMAGE", SqlDbType.Image).Value = Photo;
int rows = command.ExecuteNonQuery();
if (rows > 0)
{
MessageBox.Show("添加成功");
}
else
{
MessageBox.Show("添加失败");
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
int index;
/// <summary>
/// 获取图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGetImage_Click(object sender, EventArgs e)
{
/// <summary>
/// 存图片的集合
/// </summary>
IList<Form1> form1List = new List<Form1>();
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
SqlCommand command = new SqlCommand("PROC_GETIMAGE", conn);
command.CommandType = CommandType.StoredProcedure;
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Form1 form1 = new Form1();
form1.Photo = (byte[])reader["Photo"];
form1List.Add(form1);
}
}
}
if (form1List.Count == 1)
{
MemoryStream imageStream = new MemoryStream(form1List[0].Photo);
pictureBox.Image = Image.FromStream(imageStream);
}
else if (form1List.Count > 1)
{
index++;
if (index < form1List.Count)
{
btnGetImage.Text = "下一张";
MemoryStream imageStream = new MemoryStream(form1List[index].Photo);
pictureBox.Image = Image.FromStream(imageStream);
}
else
{
index=0;
MemoryStream imageStream = new MemoryStream(form1List[index].Photo);
pictureBox.Image = Image.FromStream(imageStream);
}
}
else
{
MessageBox.Show("存中还没有图片");
}
}
/// <summary>
/// 清除图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClear_Click(object sender, EventArgs e)
{
pictureBox.Image = null;
}
#8
try
{
string ImgPath = this.FileUpload1.PostedFile.FileName;
string ImgName = ImgPath.Substring(ImgPath.LastIndexOf("\\") + 1);
string ImgExtend = ImgPath.Substring(ImgPath.LastIndexOf(".") + 1);
if (ImgExtend != "bmp" || ImgExtend != "gif" || ImgExtend != "jpg")//验证图片格式
{
this.Label1.Text = "上传图片的格式不正确";
return;
}
int FileLen = this.FileUpload1.PostedFile.ContentLength;
Byte[] FileData = new Byte[FileLen];
HttpPostedFile hp = FileUpload1.PostedFile;
Stream sr = hp.InputStream;
sr.Read(FileData, 0, FileLen);
//这段数据库操作代码建议你 好好封转一下 不要直接 写在前台 我是临时写的 呵呵
SqlConnection con = new SqlConnection("数据库连接字符串");
con.Open();
SqlCommand com = new SqlCommand("insert into table (数据列明) values (@imgdata)", con);
com.Parameters.Add("@imgdata", SqlDbType.Image);
com.Parameters["@imgdata"].Value = FileData;
com.ExecuteNonQuery();
Label1.Text = "保存成功!";
}
catch(Exception err)
{
Label1.Text = "处理失败!原因为:" + err.ToString();
}
{
string ImgPath = this.FileUpload1.PostedFile.FileName;
string ImgName = ImgPath.Substring(ImgPath.LastIndexOf("\\") + 1);
string ImgExtend = ImgPath.Substring(ImgPath.LastIndexOf(".") + 1);
if (ImgExtend != "bmp" || ImgExtend != "gif" || ImgExtend != "jpg")//验证图片格式
{
this.Label1.Text = "上传图片的格式不正确";
return;
}
int FileLen = this.FileUpload1.PostedFile.ContentLength;
Byte[] FileData = new Byte[FileLen];
HttpPostedFile hp = FileUpload1.PostedFile;
Stream sr = hp.InputStream;
sr.Read(FileData, 0, FileLen);
//这段数据库操作代码建议你 好好封转一下 不要直接 写在前台 我是临时写的 呵呵
SqlConnection con = new SqlConnection("数据库连接字符串");
con.Open();
SqlCommand com = new SqlCommand("insert into table (数据列明) values (@imgdata)", con);
com.Parameters.Add("@imgdata", SqlDbType.Image);
com.Parameters["@imgdata"].Value = FileData;
com.ExecuteNonQuery();
Label1.Text = "保存成功!";
}
catch(Exception err)
{
Label1.Text = "处理失败!原因为:" + err.ToString();
}
#9
public class ImageManage
{
public static string GetFileName(FileUpload fuValues)
{
System.Web.UI.Page pe = new Page();
string loginname = pe.Session["UserName"].ToString();
int allowlength = 1024 * 1024 * 10;//限制文件大小
string filename = Path.GetFileName(fuValues.PostedFile.FileName);
if (fuValues.HasFile)
{
string size = fuValues.PostedFile.ContentLength.ToString(); //获取已上传文件夹的大小
string type = Path.GetExtension(fuValues.PostedFile.FileName); //得到文件的后缀
//string fullpath = Server.MapPath("~/UserDocument/" + loginname + "/Picture/");//文件的保存路径
string fullpath = "C:\\CESS\\" + loginname + "\\";
string vsnewname = System.DateTime.Now.ToString("yyyyMMddHHmm_");//声称文件名,防止重复
//string path = "UserDocument/" + loginname + "/Picture/" + vsnewname;
if (fuValues.PostedFile.ContentLength < allowlength)
{
if (type == ".bmp" || type == ".png" || type == ".jpg" || type == ".gif" || type == ".BMP" ||
type == ".PNG" || type == ".JPG" || type == ".GIF")
{
if (!Directory.Exists(fullpath))//判断上传文件夹是否存在,若不存在,则创建
{
//创建文件夹
Directory.CreateDirectory(fullpath);
fuValues.PostedFile.SaveAs(fullpath + vsnewname + filename);
return fullpath + vsnewname + filename;
}
else
{
fuValues.PostedFile.SaveAs(fullpath + vsnewname + filename);
return fullpath + vsnewname + filename;
}
}
else
{
pe.Response.Write("<script>alert('上传文件格式错误!');history.go(-1);</script>");
return "";
}
}
else
{
pe.Response.Write("<script>alert('上传文件太大!');history.go(-1);</script>");
return "";
}
}
else
{
pe.Response.Write("<script>alert('请上传文件!');history.go(-1);</script>");
return "";
}
}
}
#10
擦, 贴错了,,,
#11
private Byte[] EditImg()
{
string strURL = this.fuEditImage.PostedFile.FileName;
//将需要存储的图片读取为数据流
FileStream fs = new FileStream(strURL, FileMode.Open, FileAccess.Read);
Byte[] btye2 = new byte[fs.Length];
fs.Read(btye2, 0, Convert.ToInt32(fs.Length));
fs.Close();
return btye2;
{
string strURL = this.fuEditImage.PostedFile.FileName;
//将需要存储的图片读取为数据流
FileStream fs = new FileStream(strURL, FileMode.Open, FileAccess.Read);
Byte[] btye2 = new byte[fs.Length];
fs.Read(btye2, 0, Convert.ToInt32(fs.Length));
fs.Close();
return btye2;
#12
#5楼弟兄到哪都贴广告。。。