国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

gridview 顯示圖片的實例代碼

2020-01-24 03:24:44
字體:
來源:轉載
供稿:網友

1.將圖片以二進制存入數據庫

2.讀取二進制圖片在頁面顯示

3.設置Image控件顯示從數據庫中讀出的二進制圖片

4.GridView中ImageField以URL方式顯示圖片

5.GridView顯示讀出的二進制圖片

====================

1.將圖片以二進制存入數據庫

復制代碼 代碼如下:

//保存圖片到數據庫

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.設置Image控件顯示從數據庫中讀出的二進制圖片
復制代碼 代碼如下:

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;

   }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 治县。| 元氏县| 鹤峰县| 敖汉旗| 湘潭市| 称多县| 广安市| 靖西县| 江孜县| 承德县| 正镶白旗| 大新县| 皋兰县| 邯郸市| 富平县| 定安县| 永吉县| 新化县| 桐城市| 宁阳县| 高尔夫| 宝应县| 万山特区| 项城市| 轮台县| 邳州市| 株洲县| 安顺市| 新建县| 湘潭县| 漳州市| 闸北区| 安图县| 鄂托克前旗| 佛坪县| 大厂| 静乐县| 博兴县| 大姚县| 龙胜| 甘肃省|