gridview 顯示圖片的實例代碼
1.將圖片以二進制存入數(shù)據(jù)庫
2.讀取二進制圖片在頁面顯示
3.設(shè)置Image控件顯示從數(shù)據(jù)庫中讀出的二進制圖片
4.GridView中ImageField以URL方式顯示圖片
5.GridView顯示讀出的二進制圖片
====================
1.將圖片以二進制存入數(shù)據(jù)庫
//保存圖片到數(shù)據(jù)庫
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 = http://www.cnblogs.com/wycoo/archive/2012/02/07/photo;
myConn.Open();
myComm.ExecuteNonQuery();
myConn.Close();
}
2.讀取二進制圖片在頁面顯示
//讀取圖片
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);
3.設(shè)置Image控件顯示從數(shù)據(jù)庫中讀出的二進制圖片
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;
//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>
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;
}
}
相關(guān)文章
C#使用System.Threading.Timer實現(xiàn)計時器的示例詳解
以往一般都是用 System.Timers.Timer 來做計時器,其實 System.Threading.Timer 也可以實現(xiàn)計時器功能,下面就跟隨小編一起來學(xué)習(xí)一下如何使用System.Threading.Timer實現(xiàn)計時器功能吧2024-01-01C#使用IronPython調(diào)用Python的實現(xiàn)
本文主要介紹了C#使用IronPython調(diào)用Python的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02C#使用HttpPost請求調(diào)用WebService的方法
這篇文章主要為大家詳細介紹了C#使用HttpPost請求調(diào)用WebService的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08詳解C#編程中異常的創(chuàng)建和引發(fā)以及異常處理
這篇文章主要介紹了C#編程中異常的創(chuàng)建和引發(fā)以及異常處理,文中介紹了Catch塊和Finally塊等基本的異常處理要點,需要的朋友可以參考下2016-02-02C# 實現(xiàn)簡易的串口監(jiān)視上位機功能附源碼下載
這篇文章主要介紹了C# 實現(xiàn)簡易的串口監(jiān)視上位機功能,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11C#/VB.NET實現(xiàn)將Html轉(zhuǎn)為Word的示例詳解
本文分享以C#程序代碼為例,實現(xiàn)將Html文件轉(zhuǎn)換Word文檔的方法(附VB.NET代碼)。在實際轉(zhuǎn)換場景中可參考本文的方法,感興趣的可以了解一下2022-07-07C#窗體編程不顯示最小化、最大化、關(guān)閉按鈕的方法
這篇文章主要介紹了C#窗體編程不顯示最小化、最大化、關(guān)閉按鈕的方法,即windows forms編程中取消最小化、最大化、關(guān)閉按鈕,需要的朋友可以參考下2014-08-08c#只讀字段和常量的區(qū)別,以及靜態(tài)構(gòu)造函數(shù)的使用實例
這篇文章主要介紹了c#只讀字段和常量的區(qū)別,以及靜態(tài)構(gòu)造函數(shù)的使用實例,有需要的朋友可以參考一下2013-12-12