从数据库读出图片

lin5619sun 2008-07-08 09:33:09
我现在向从数据库里面读出一张图片,以后可能要加上水印,我现在不知道用什么控件比较好,具体的读出过程也不是很清楚,麻烦各位给指点下,本人初学,谢谢
asp.net+sql2005 最好是C#代码,源代码不是很熟悉
...全文
119 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
lin5619sun 2008-07-10
  • 打赏
  • 举报
回复
谢谢各位的帮助,问题已经解决,谢谢~
utpcb 2008-07-08
  • 打赏
  • 举报
回复
kankna
aaajedll 2008-07-08
  • 打赏
  • 举报
回复
用哪種方法不重要,只要能把功能做出來就可以了,個人觀點!
snrnui 2008-07-08
  • 打赏
  • 举报
回复
先在这里为我的无知向各位道歉~~~
再学习下
lchh0917 2008-07-08
  • 打赏
  • 举报
回复
顶下6楼,用二进制流存进数据库比较快而便
格拉 2008-07-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 donghai83 的回复:]
2楼的,你不懂,不要乱说好不好,误人子弟.
数据库不仅可以存储图片路径(字符串型),
还是可以将图片转换成二进制流存进数据库的(sql中的image型).
好好研究去.
[/Quote]顶下!
donghai83 2008-07-08
  • 打赏
  • 举报
回复
http://hi.baidu.com/simadi/blog/item/e98354fb22f698156c22eb64.html
donghai83 2008-07-08
  • 打赏
  • 举报
回复
2楼的,你不懂,不要乱说好不好,误人子弟.
数据库不仅可以存储图片路径(字符串型),
还是可以将图片转换成二进制流存进数据库的(sql中的image型).
好好研究去.
lchh0917 2008-07-08
  • 打赏
  • 举报
回复
我们也正在学,刚做过的,可以用listbox显示图像列表,用picturebox显示每个所选列的图像,在加上按钮“浏览图片、删除图片、保存图片、关闭程序”。另外要自己新建个数据库,存储字段改为图片格式:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace datapicture
{
public partial class Form1 : Form
{
DataSet Mydataset;
SqlConnection myconn;
SqlDataAdapter mydr;
public Form1()
{
InitializeComponent();
}

private void ShowDbImage()
{
byte[] bytes=(byte[]) Mydataset.Tables[0].Rows[this.listBox1.SelectedIndex][1];
MemoryStream memStream = new MemoryStream(bytes);
try
{
Bitmap MyImage = new Bitmap(memStream);
this.pictureBox1.Image = MyImage;
}
catch
{
MessageBox.Show(this, "读取数据库中的图像信息失败!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.pictureBox1.Image = null;

}

}

private void Form1_Load(object sender, EventArgs e)
{
this.myconn = new SqlConnection("server=localhost;database=pubs;Uid=sa;Pwd=sa");
this.mydr = new SqlDataAdapter("select * from pub_info", "server=localhost;database=pubs;Uid=sa;Pwd=sa");
SqlCommandBuilder mybu = new SqlCommandBuilder(this.mydr);
this.mydr.UpdateCommand = mybu.GetUpdateCommand();
this.Mydataset = new DataSet();
this.mydr.Fill(this.Mydataset,"pub_info");
this.textBox1.DataBindings.Add(new Binding("text", this.Mydataset, "pub_info.pr_info"));
for (int i = 0; i < this.Mydataset.Tables[0].Rows.Count; i++)
{
this.listBox1.Items.Add(this.Mydataset.Tables[0].Rows[i][0]);

}
this.listBox1.SetSelected(0,true);

}

private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog myFileDialog = new OpenFileDialog();
myFileDialog.ShowDialog();
if (myFileDialog.FileName.Trim() != "")
{
Stream mystream = myFileDialog.OpenFile();
int length = (int)mystream.Length;
byte[] bytes = new byte[length];
mystream.Read(bytes,0,length);
mystream.Close();
this.Mydataset.Tables[0].Rows[this.listBox1.SelectedIndex][1] = bytes;
ShowDbImage();
}
}

private void button2_Click(object sender, EventArgs e)
{
try
{
byte[] bytes = System.Text.Encoding.Unicode.GetBytes("");
int iIndex = this.listBox1.SelectedIndex;
this.Mydataset.Tables[0].Rows[iIndex][1] = bytes;
this.listBox1.SetSelected(iIndex + 1, true);
ShowDbImage();
}
catch
{
MessageBox.Show("删除完毕!","Info",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}

private void button3_Click(object sender, EventArgs e)
{
this.mydr.Update(this.Mydataset,"pub_info");
MessageBox.Show(this, "保存数据库中的图像信息成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

private void button4_Click(object sender, EventArgs e)
{
this.Close();
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ShowDbImage();
this.BindingContext[this.Mydataset, "pub_info"].Position = this.listBox1.SelectedIndex;
}
}
}
nagexiatiank 2008-07-08
  • 打赏
  • 举报
回复
用Image控件吧,
stning 2008-07-08
  • 打赏
  • 举报
回复
http://blog.163.com/xing_aixin/blog/static/37235505200831563113909/
楼主看看这个。
snrnui 2008-07-08
  • 打赏
  • 举报
回复
在ASP.net的设计中,数据库里一般只是图片的路径,你可以把这个路径从数据库里取出来,然后赋值给Image控件的,ImageUrl属性就可以把图片显示出来了。
donghai83 2008-07-08
  • 打赏
  • 举报
回复
二进制流

110,043

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧