有VB.NET写的,参考一下便可用 以MS自带的数据库Northwnd为例,其中有个表是Categories,有四个四段,其中有一个是Image类型的Picture字段.我们首先添加一张bmp图片到最后一行的Picture中,然后在读出来显示到Image控件中. 添加一个SqlDataAdapter1,用向导设置联接数据库为Northwnd,SQL语句为SELECT [Category ID], [Category Name], Description, Picture FROM Categories.生成一个数据集为dataset1. 然后添加两个按钮分别表示写图片到数据库和读数据库,还有一个Image控件用于显示图片.添加以下代码 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load SqlDataAdapter1.Fill(DataSet11) End Sub'从数据库读取图片暂时存储为monkey.bmp,然后加载到image控件里面. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadPicFromDb.Click Try Dim data As Byte() = DataSet11.Tables(0).Rows(7).Item(3) Dim myfilestream As New System.IO.FileStream(Application.StartupPath & "\monkey.bmp", IO.FileMode.Create) myfilestream.Write(data, 0, data.Length) myfilestream.Close() PictureBox1.Image = New Bitmap(Application.StartupPath & "\monkey.bmp") Catch End Try End Sub '把C:\6.bmp写入库中,你可以改为自己的图片. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InsertPicToDb.Click Dim myfilestream As New System.IO.FileStream("c:\6.bmp", IO.FileMode.Open) Dim data() As Byte ReDim data(myfilestream.Length - 1) myfilestream.Read(data, 0, myfilestream.Length) myfilestream.Close() DataSet11.Tables(0).Rows(7).Item(3) = data SqlDataAdapter1.Update(DataSet11.GetChanges()) End Sub
给你一小段代码,供参考: private void GetPicture(object id) { if(id.ToString()=="") { return; } try { string sql="select Photo from SalFund..Sal_HumanInfo_Photograph where Num = '"+id.ToString()+"'"; SqlConnection conn=new SqlConnection(QueryAndStatistic.CommonClass.sConnectionString); SqlDataAdapter da=new SqlDataAdapter(sql,conn); DataSet dss=new DataSet(); da.Fill(dss,"Sal_HumanInfo_Photograph"); if(dss.Tables[0]==null || dss.Tables[0].Rows.Count==0)//表示没有查出图片 { this.photo.Visible=false; this.noPhoto.Visible=true; //this.noPhoto.ToolTip="not exist!"; return; } byte[] MyData = null; MyData = (byte[])dss.Tables[0].Rows[0]["Photo"]; int ArraySize = new int(); ArraySize = MyData.GetUpperBound(0); FileStream fs = new FileStream(Server.MapPath("temp//tmp.bmp"), FileMode.OpenOrCreate, FileAccess.Write); fs.Write(MyData, 0,ArraySize+1); // don't ask me why, but I had to add 1 here for this to work fs.Close(); //Bitmap image = new Bitmap(Server.MapPath("temp//tmp.bmp")); //this.photo.Src=Server.MapPath("temp//tmp.bmp");//.Image = new Bitmap("tmp.gif"); this.photo.Src="temp//tmp.bmp"; this.noPhoto.Visible=false; this.photo.Visible=true; } catch//(Exception err) { return; } }
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Image1.ImageUrl = "default.aspx?number=" + TextBox1.Text End SubPrivate Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '在此处放置初始化页的用户代码 Dim strimage As String = Request.QueryString("number") If Not strimage Is Nothing Then Response.Clear() Dim image_widht As Integer = 50 Dim image_height As Integer = 20 Dim image_size As New Size image_size = New Size(image_widht, image_height) Dim bit As Bitmap = New Bitmap(image_size.Width, image_size.Height, PixelFormat.Format24bppRgb) Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bit) g.SmoothingMode = SmoothingMode.AntiAlias g.Clear(Color.FromArgb(255, 255, 255)) '背景颜色! g.DrawRectangle(Pens.WhiteSmoke, 0, 0, image_size.Width, image_size.Height) g.DrawString(strimage, New Font("Arial", 10), New SolidBrush(Color.FromArgb(0, 125, 0)), New PointF(0, 0)) '显示数字,颜色为0.125.0 bit.Save(Response.OutputStream, ImageFormat.Gif) g.Dispose() bit.Dispose() Response.End() End If End Sub
以MS自带的数据库Northwnd为例,其中有个表是Categories,有四个四段,其中有一个是Image类型的Picture字段.我们首先添加一张bmp图片到最后一行的Picture中,然后在读出来显示到Image控件中. 添加一个SqlDataAdapter1,用向导设置联接数据库为Northwnd,SQL语句为SELECT [Category ID], [Category Name], Description, Picture FROM Categories.生成一个数据集为dataset1. 然后添加两个按钮分别表示写图片到数据库和读数据库,还有一个Image控件用于显示图片.添加以下代码 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SqlDataAdapter1.Fill(DataSet11)
End Sub'从数据库读取图片暂时存储为monkey.bmp,然后加载到image控件里面.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadPicFromDb.Click
Try
Dim data As Byte() = DataSet11.Tables(0).Rows(7).Item(3)
Dim myfilestream As New System.IO.FileStream(Application.StartupPath & "\monkey.bmp", IO.FileMode.Create)
myfilestream.Write(data, 0, data.Length)
myfilestream.Close()
PictureBox1.Image = New Bitmap(Application.StartupPath & "\monkey.bmp")
Catch
End Try
End Sub
'把C:\6.bmp写入库中,你可以改为自己的图片.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InsertPicToDb.Click
Dim myfilestream As New System.IO.FileStream("c:\6.bmp", IO.FileMode.Open)
Dim data() As Byte
ReDim data(myfilestream.Length - 1)
myfilestream.Read(data, 0, myfilestream.Length)
myfilestream.Close()
DataSet11.Tables(0).Rows(7).Item(3) = data
SqlDataAdapter1.Update(DataSet11.GetChanges())
End Sub
private void GetPicture(object id)
{
if(id.ToString()=="")
{
return;
}
try
{
string sql="select Photo from SalFund..Sal_HumanInfo_Photograph where Num = '"+id.ToString()+"'";
SqlConnection conn=new SqlConnection(QueryAndStatistic.CommonClass.sConnectionString);
SqlDataAdapter da=new SqlDataAdapter(sql,conn);
DataSet dss=new DataSet();
da.Fill(dss,"Sal_HumanInfo_Photograph");
if(dss.Tables[0]==null || dss.Tables[0].Rows.Count==0)//表示没有查出图片
{
this.photo.Visible=false;
this.noPhoto.Visible=true;
//this.noPhoto.ToolTip="not exist!";
return;
}
byte[] MyData = null;
MyData = (byte[])dss.Tables[0].Rows[0]["Photo"]; int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0); FileStream fs = new FileStream(Server.MapPath("temp//tmp.bmp"), FileMode.OpenOrCreate, FileAccess.Write); fs.Write(MyData, 0,ArraySize+1); // don't ask me why, but I had to add 1 here for this to work
fs.Close();
//Bitmap image = new Bitmap(Server.MapPath("temp//tmp.bmp"));
//this.photo.Src=Server.MapPath("temp//tmp.bmp");//.Image = new Bitmap("tmp.gif");
this.photo.Src="temp//tmp.bmp";
this.noPhoto.Visible=false;
this.photo.Visible=true;
}
catch//(Exception err)
{
return;
}
}
Image1.ImageUrl = "default.aspx?number=" + TextBox1.Text
End SubPrivate Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
Dim strimage As String = Request.QueryString("number")
If Not strimage Is Nothing Then
Response.Clear()
Dim image_widht As Integer = 50
Dim image_height As Integer = 20
Dim image_size As New Size
image_size = New Size(image_widht, image_height)
Dim bit As Bitmap = New Bitmap(image_size.Width, image_size.Height, PixelFormat.Format24bppRgb)
Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bit)
g.SmoothingMode = SmoothingMode.AntiAlias
g.Clear(Color.FromArgb(255, 255, 255)) '背景颜色!
g.DrawRectangle(Pens.WhiteSmoke, 0, 0, image_size.Width, image_size.Height)
g.DrawString(strimage, New Font("Arial", 10), New SolidBrush(Color.FromArgb(0, 125, 0)), New PointF(0, 0)) '显示数字,颜色为0.125.0
bit.Save(Response.OutputStream, ImageFormat.Gif)
g.Dispose()
bit.Dispose()
Response.End()
End If
End Sub
http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=ECD9AE16-8FF0-4A1C-9B9F-5E8B641CB1B1