a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁(yè) > 編程 > C# > 正文

gridview 顯示圖片的實(shí)例代碼

2020-01-24 03:24:44
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

1.將圖片以二進(jìn)制存入數(shù)據(jù)庫(kù)

2.讀取二進(jìn)制圖片在頁(yè)面顯示

3.設(shè)置Image控件顯示從數(shù)據(jù)庫(kù)中讀出的二進(jìn)制圖片

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

5.GridView顯示讀出的二進(jìn)制圖片

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

1.將圖片以二進(jìn)制存入數(shù)據(jù)庫(kù)

復(fù)制代碼 代碼如下:

//保存圖片到數(shù)據(jù)庫(kù)

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.讀取二進(jìn)制圖片在頁(yè)面顯示

復(fù)制代碼 代碼如下:

//讀取圖片

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();



復(fù)制代碼 代碼如下:

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ù)庫(kù)中讀出的二進(jìn)制圖片
復(fù)制代碼 代碼如下:

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();


顯示圖片

復(fù)制代碼 代碼如下:

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>


后臺(tái)直接綁定即可

5.GridView顯示讀出的二進(jìn)制圖片

復(fù)制代碼 代碼如下:

//樣板列
<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;

   }
}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 久在线视频| 太平公主一级艳史播放高清 | 欧美在线播放一区 | h片在线免费观看 | 黄色在线免费看 | 欧美电影一区 | 中文字幕在线第一页 | 国产精品久久免费视频 | 欧美a√ | 性高湖久久久久久久久aaaaa | 色综合久久伊人 | 国产精品久久精品久久 | 一区二区三区精品 | 中文字幕 亚洲一区 | 91视频在线观看 | 一区二区三区回区在观看免费视频 | 免费一区二区三区 | 国内精品久久久久 | 国内精品久久久久久影视8 久久亚洲精品国产一区最新章节 | 日韩av在线一区二区三区 | 欧美视频日韩 | 国产精品久久综合 | 日韩欧美一区二区在线 | 国产精品中文字幕在线 | 狠狠久 | 欧美日韩中文字幕在线 | 成年人免费在线视频 | 欧美精品亚洲 | 免费特级黄毛片 | 日韩精品无码一区二区三区 | 日韩毛片在线观看 | 四虎最新紧急更新地址 | 国产精品欧美一区二区三区不卡 | 99视频这里有精品 | 一区二区日韩在线观看 | www.av在线| 亚洲精品乱码久久久久久蜜桃不卡 | 最近免费中文字幕在线视频2 | 精品日韩一区二区三区 | 亚洲国产精品一区二区久久 | 婷婷桃色网|