`
caozuiba
  • 浏览: 911740 次
文章分类
社区版块
存档分类
最新评论

与图片的二进制数据库存储和显示

 
阅读更多

1.将图片以二进制存入数据库
2.读取二进制图片在页面显示
3.设置Image控件显示从数据库中读出的二进制图片
4.GridView中ImageField以URL方式显示图片
5.GridView显示读出的二进制图片
====================

1.将图片以二进制存入数据库

C#
--------------------------
//保存图片到数据库
protected void Button1_Click(object sender, EventArgs e)
{
//图片路径
string strPath = "~/photo/03.JPG";
string strPhotoPath = Server.MapPath(strPath);
//读取图片
FileStream fs = new System.IO.FileStream(strPhotoPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
//存入
SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
string strComm = " INSERT INTO personPhoto(personName, personPhotoPath, personPhoto) ";
strComm += " VALUES('wangwu', '" + strPath + "', @photoBinary )";
SqlCommand myComm = new SqlCommand(strComm, myConn);
myComm.Parameters.Add("@photoBinary", SqlDbType.Binary,photo.Length);
myComm.Parameters["@photoBinary"].Value = photo;
myConn.Open();
myComm.ExecuteNonQuery();
myConn.Close();
}

VB.NET

'保存图片到数据库

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
'图片路径
Dim strPath As String = "~/photo/03.JPG"
Dim strPhotoPath As String = Server.MapPath(strPath)
'读取图片
Dim fs As FileStream = New System.IO.FileStream(strPhotoPath, FileMode.Open, FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fs)
Dim photo() As Byte = br.ReadBytes(CType(fs.Length,Integer))
br.Close
fs.Close
'存入
Dim myConn As SqlConnection = New SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa")
Dim strComm As String = " INSERT INTO personPhoto(personName, personPhotoPath, personPhoto) "
strComm = (strComm + (" VALUES('wangwu', '" _
+ (strPath + "', @photoBinary )")))
Dim myComm As SqlCommand = New SqlCommand(strComm, myConn)
myComm.Parameters.Add("@photoBinary", SqlDbType.Binary, photo.Length)
myComm.Parameters("@photoBinary").Value = photo
myConn.Open
myComm.ExecuteNonQuery
myConn.Close
End Sub

2.读取二进制图片在页面显示

C#
--------------------------
//读取图片
SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
string strComm = " SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ";
SqlCommand myComm = new SqlCommand(strComm, myConn);
myConn.Open();
SqlDataReader dr = myComm.ExecuteReader();
while (dr.Read())
{
byte[] photo = (byte[])dr["personPhoto"];
this.Response.BinaryWrite(photo);
}
dr.Close();
myConn.Close();

SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ", myConn);
DataSet myds = new DataSet();
myConn.Open();
myda.Fill(myds);
myConn.Close();
//
byte[] photo = (byte[])myds.Tables[0].Rows[0]["personPhoto"];
this.Response.BinaryWrite(photo);

VB.NET

Dim myConn As SqlConnection = New SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa")
Dim strComm As String = " SELECT personPhoto FROM personPhoto WHERE personName='wangwu' "
Dim myComm As SqlCommand = New SqlCommand(strComm, myConn)
myConn.Open
Dim dr As SqlDataReader = myComm.ExecuteReader

While dr.Read
Dim photo() As Byte = CType(dr("personPhoto"),Byte())
Me.Response.BinaryWrite(photo)

End While
dr.Close
myConn.Close


Dim myConn As SqlConnection = New SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa")
Dim myda As SqlDataAdapter = New SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ", myConn)
Dim myds As DataSet = New DataSet
myConn.Open
myda.Fill(myds)
myConn.Close
Dim photo() As Byte = CType(myds.Tables(0).Rows(0)("personPhoto"),Byte())
Me.Response.BinaryWrite(photo)


3.设置Image控件显示从数据库中读出的二进制图片

C#
---------------------------------------------
SqlConnection myConn = new SqlConnection("Data Source=192.168.0.36;Initial Catalog=TestDB;User ID=sa;Password=sa");
SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ", myConn);
DataSet myds = new DataSet();
myConn.Open();
myda.Fill(myds);
myConn.Close();
//
byte[] photo = (byte[])myds.Tables[0].Rows[0]["personPhoto"];
//图片路径
string strPath = "~/photo/wangwu.JPG";
string strPhotoPath = Server.MapPath(strPath);
//保存图片文件
BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));
bw.Write(photo);
bw.Close();
//显示图片
this.Image1.ImageUrl = strPath;

VB.NET

Dim myConn As SqlConnection = New SqlConnection("Data Source=192.168.0.36;Initial Catalog=TestDB;User ID=sa;Password=sa")
Dim myda As SqlDataAdapter = New SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ", myConn)
Dim myds As DataSet = New DataSet
myConn.Open
myda.Fill(myds)
myConn.Close
Dim photo() As Byte = CType(myds.Tables(0).Rows(0)("personPhoto"),Byte())
Dim strPath As String = "~/photo/wangwu.JPG"
Dim strPhotoPath As String = Server.MapPath(strPath)
Dim bw As BinaryWriter = New BinaryWriter(File.Open(strPhotoPath, FileMode.OpenOrCreate))
bw.Write(photo)
bw.Close
'显示图片
Me.Image1.ImageUrl = strPath

4.GridView中ImageField以URL方式显示图片
----------------------------
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="personName" HeaderText="姓名" />
<asp:ImageField DataImageUrlField="personPhotoPath"
HeaderText="图片">
</asp:ImageField>
</Columns>
</asp:GridView>
后台直接绑定即可

5.GridView显示读出的二进制图片
------------------------------
//样板列
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="personName" HeaderText="姓名" />
<asp:ImageField DataImageUrlField="personPhotoPath"
HeaderText="图片">
</asp:ImageField>
<asp:TemplateField HeaderText="图片">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

//绑定

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex < 0)
return;
// System.ComponentModel.Container
string strPersonName = (string)DataBinder.Eval(e.Row.DataItem, "personName");
Image tmp_Image = (Image)e.Row.Cells[2].FindControl("Image1");
if (!System.Convert.IsDBNull(DataBinder.Eval(e.Row.DataItem, "personPhoto")))
{
//
byte[] photo = (byte[])DataBinder.Eval(e.Row.DataItem, "personPhoto");
//图片路径
string strPath = "~/photo/" + strPersonName.Trim() + ".JPG";
string strPhotoPath = Server.MapPath(strPath);
//保存图片文件
BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath, FileMode.OpenOrCreate));
bw.Write(photo);
bw.Close();
//显示图片
tmp_Image.ImageUrl = strPath;
}
}
VB.NET

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If (e.Row.RowIndex < 0) Then
Return
End If
' System.ComponentModel.Container
Dim strPersonName As String = CType(DataBinder.Eval(e.Row.DataItem, "personName"),String)
Dim tmp_Image As Image = CType(e.Row.Cells(2).FindControl("Image1"),Image)
If Not System.Convert.IsDBNull(DataBinder.Eval(e.Row.DataItem, "personPhoto")) Then
'
Dim photo() As Byte = CType(DataBinder.Eval(e.Row.DataItem, "personPhoto"),Byte())
'G
Dim strPath As String = ("~/photo/" _
+ (strPersonName.Trim + ".JPG"))
Dim strPhotoPath As String = Server.MapPath(strPath)
'XG
Dim bw As BinaryWriter = New BinaryWriter(File.Open(strPhotoPath, FileMode.OpenOrCreate))
bw.Write(photo)
bw.Close
'>:G
tmp_Image.ImageUrl = strPath
End If
End Sub

分享到:
评论

相关推荐

    .net 图片的二进制数据库存储和显示

    更快的解决图片二进制数据存储的问题,了解二进制流的强大威力。

    将图片二进制流存储到数据库中

    读取图片的二进制流,存到数据库中读取图片的二进制流,存到数据库中读取图片的二进制流,存到数据库中读取图片的二进制流,存到数据库中

    图片存储到数据库保存二进制文件,并在DATAGRIDVIEW中显示出来

    在C#编程中,将图片存储到数据库并以二进制数据的形式保存,以及在DataGridView控件中显示这些图片,是一项常见的任务。这种操作在处理大量图像数据时尤其有用,例如在开发一个需要展示产品图片的电子商务应用或者...

    非常好用的MDB二进制数据库读取解密软件

    非常好用的MDB二进制数据库读取解密软件

    图片以二进制存取数据库

    在关系型数据库如MySQL、PostgreSQL、Oracle和SQL Server中,BLOB是一个特殊的数据类型,用于存储大量的二进制数据,例如图片、音频或视频文件。在SQL Server中,这种类型被称为`VARBINARY(MAX)`,它允许存储最多2^...

    c++链接数据库用二进制存储图像,并能显示图像

    在C++编程中,将图像数据存储到...综上所述,这个例子展示了如何在C++中通过ODBC接口与Access数据库交互,以二进制形式存储和读取图像数据。实践中,可能还需要考虑数据安全、并发访问以及数据库设计的合理性等因素。

    二进制图片上传到数据库

    总结起来,二进制图片上传涉及的关键技术点包括图片二进制转换、数据库操作(特别是二进制数据类型的使用)以及数据的恢复与读取。在实际开发中,需要根据项目需求和资源状况选择合适的存储方案。

    二进制流形式上传图片,然后前台显示图片示例(vs2005+sql2005)

    创建一个包含图片ID、图片名称和图片二进制数据的表,用于存储上传的图片信息。 四、图片存储 当图片被上传并转换为二进制流后,需要将其存储到数据库中。通过ADO.NET,你可以使用SqlCommand对象执行INSERT语句,...

    通过二进制数据流的方式,读写图片,把图片存入数据库

    总结,通过二进制数据流的方式处理图片,可以有效地在数据库中存储和检索图片,这对于构建支持多媒体内容的应用至关重要。这种方式不仅节省了存储空间,还提高了数据传输的效率。在实际应用中,还需要考虑性能优化、...

    ASP.NET DataList 绑定数据库二进制图片

    本文将详细介绍如何将存储在数据库中的二进制图片数据绑定到DataList,以便在网页上进行展示。我们将使用Visual Studio 2010作为开发工具,SQL Server 2008作为数据库管理系统。 首先,我们需要了解如何在数据库中...

    图片以二进制的形式存储到数据库中和显示

    图片以二进制的形式存储到数据库中和显示 图片以二进制的形式存储到数据库中和显示

    用C#写的二进制图片在数据库中存储

    在IT行业中,数据库存储是数据管理的关键环节,而图片作为一种非结构化数据,有时需要以二进制形式存储在数据库中。本项目聚焦于使用C#编程语言处理图像的二进制存储,并实现从数据库中读取这些二进制数据以进行显示...

    C#将图片变成二进制保存到数据库

    6. **恢复图片**: 当需要从数据库中读取并显示图片时,可以反向操作,从二进制数据创建Image对象: ```csharp using (MemoryStream stream = new MemoryStream(imageData)) { Image restoredImage = Image.From...

    C#实现把图片转换成二进制以及把二进制转换成图片的方法示例

    C#实现图片与二进制相互转换及数据库存储 本文主要介绍了C#实现把图片转换成二进制以及把二进制转换成图片的方法,并结合具体实例形式分析了基于C#的图片与二进制相互转换以及图片保存到数据库的相关操作技巧。 一...

    C# ##从数据库中取二进制图片文件存入硬盘.rar

    当涉及到图像数据时,一个常见的做法是将图片以二进制形式存储在数据库中。这个场景中,"C# 从数据库中取二进制图片文件存入硬盘"的主题涉及到如何使用C#编程语言从数据库中检索这些二进制数据,并将它们写入到本地...

    添加二进制图片到数据库

    本文将深入探讨如何将二进制图片添加到SQL Server 2005数据库,并通过DataGridview控件进行显示。这一过程涉及了数据库操作、图像处理以及前端展示等多个环节。 首先,我们需要了解在SQL Server 2005中存储二进制...

    图片以二进制流的形式存储到数据库中的源代码

    在IT行业中,数据库存储是关键任务之一,尤其是在处理多媒体数据如图片时。本文将深入探讨如何将图片以二进制流的形式存储到SQL Server 2005数据库中,并通过.NET编程实现上传和查看功能。我们将主要关注以下几个...

    WPF SQLite存储与读取二进制图片

    WPF SQLite存储与读取二进制图片

    数据库读取二进制图片显示

    在IT领域,数据库存储和读取二进制数据,如图片,是一项常见的任务。这篇文章将深入探讨如何在数据库中处理二进制图片,并提供一种显示它们的方法,供学习者参考。 首先,我们要理解图片本质上是二进制数据,通常以...

Global site tag (gtag.js) - Google Analytics