注册 登录
编程论坛 ASP.NET技术论坛

[求助]如何从数据库中提取图片

yuzhou 发布于 2007-03-13 10:16, 1122 次点击
各位高手请指教:

如何从数据库中提取图片(image),把它显示在控件中。
8 回复
#2
zhzh2007-03-13 10:38

你的数据库中存储的是图片的名字还是图片?
image 的src绑定路径和数据库中的名字就可以显示了啊
怎么都是问这个问题?下面应该有解答,仔细找找看吧

#3
yuzhou2007-03-13 21:31

谢谢zhzh大侠,不是你的那个意思,是二进制数据image类型,把它显示在image控件中.

#4
zhzh2007-03-13 22:42
这个到没做过,一般都是存储图片的名字!同盼指点
#5
parklee2007-03-13 23:49

protected void Page_Load(object sender, EventArgs e)
{
string photoid;
photoid = Request.Params["id"];
Image aa = (Image)FormView1.FindControl("Image1");
aa.ImageUrl = GetPicPath(photoid);
}
private string GetPicPath(string addictNo)
{
OracleConnection conn = new OracleConnection("");
OracleCommand Oraclecomm = new OracleCommand();
Oraclecomm.CommandText = "SELECT pic FROM pp WHERE rybh ='" + addictNo + "'";
Oraclecomm.Connection = conn;
OracleDataAdapter da = new OracleDataAdapter(Oraclecomm);
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds, "pp");
conn.Close();
Response.Clear();

//将图片数据存成本地文件
string tempfile = "" + addictNo + ".jpg";
string fpath = Server.MapPath(Request.ApplicationPath) + "\\temp\\" + tempfile;
FileStream fs = new FileStream(fpath, FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write((byte[])ds.Tables["pp"].Rows[0][0]);
bw.Close();
return "temp/" + tempfile;
}

从数据库中读取图片信息在IMAGE中显示

这是别人写的 不是我写的

#6
漯河2007-03-14 09:32
有的人存储的是路径
有的人存储的是图片

写出来的代码就不一样了
#7
yuzhou2007-03-15 13:41
谢谢大家呀!,你们好热情呀

上面也是将图片存在项目中,
图片在数据库中以二进制形式保存,
将它抓出来显示在image控件中 我只学过在整个页面显示,但没有在image控件

private void Page_Load(object sender, System.EventArgs e)
{
//获得前页面传递过来的图片id
int ImgID = Convert.ToInt32(Request.QueryString["mid"]);

string strCon = "server=.;database=DBImage;uid=sa;pwd=";
SqlConnection Con = new SqlConnection(strCon);
String SqlCmd = "select * from imgs where mid = @ImageID";
SqlCommand cmd = new SqlCommand(SqlCmd, Con);
cmd.Parameters.Add("@ImageID",ImgID);
Con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
Response.ContentType = (string)sdr["contenttype"];//设定输出文件类型
//输出图象文件二进制数制,sdr["myimage"]为数据储存体,0为数据指针位置、sdr["imgsize"]为数据长度
Response.OutputStream.Write((byte[])sdr["myimage"], 0, (int)sdr["imgsize"]);

#8
chelsea2007-03-17 09:46
先把图片以二进制流保存在数据库中,在连接数据库,把图片在image中显示。
#9
yuzhou2007-03-17 20:23
楼顶:
怎么显示呀?显示代码怎么写啊
1