SoFunction
Updated on 2025-03-08

gridview example code for displaying pictures

1. Store the image in binary to the database

2. Read binary pictures to display on the page

3. Set the Image control to display binary images read from the database

ImageField displays images in URL

Show read binary pictures

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

1. Store the image in binary to the database

Copy the codeThe code is as follows:

//Save the picture to the database

protected void Button1_Click(object sender, EventArgs e)

{

//Picture path

   string strPath = "~/photo/";

   string strPhotoPath = (strPath);

//Read the picture

   FileStream fs = new (strPhotoPath, , );

   BinaryReader br = new BinaryReader(fs);

   byte[] photo = ((int));

   ();

   ();

//Disclaim

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

   ("@photoBinary", ,);

   ["@photoBinary"].Value = /wycoo/archive/2012/02/07/photo;

   ();

   ();

   ();

}

2. Read binary pictures to display on the page

Copy the codeThe code is as follows:

//Read the picture

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

();

SqlDataReader dr = ();

while (())

{

   byte[] photo = (byte[])dr["personPhoto"];

   (photo);

}

();

();


or
Copy the codeThe code is as follows:

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

();

(myds);

();

byte[] photo = (byte[])[0].Rows[0]["personPhoto"];

(photo);


3. Set the Image control to display binary images read from the database
Copy the codeThe code is as follows:

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

();

(myds);

();

byte[] photo = (byte[])[0].Rows[0]["personPhoto"];

//Picture path

string strPath = "~/photo/";

string strPhotoPath = (strPath);

//Save the picture file

BinaryWriter bw = new BinaryWriter((strPhotoPath,));

(photo);

();


Show pictures

Copy the codeThe code is as follows:

this. = strPath;

 

//ImageField displays images in URL

----------------------------

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">

   <Columns>

<asp:BoundField DataField="personName" HeaderText="Name" />

   <asp:ImageField DataImageUrlField="personPhotoPath"

HeaderText="Picture">

   </asp:ImageField>

   </Columns>

</asp:GridView>


Just bind directly to the background

Show read binary pictures

Copy the codeThe code is as follows:

//Template column
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">

   <Columns>

<asp:BoundField DataField="personName" HeaderText="Name" />

   <asp:ImageField DataImageUrlField="personPhotoPath"

HeaderText="Picture">

   </asp:ImageField>

<asp:TemplateField HeaderText="Picture">

   <ItemTemplate>

   <asp:Image ID="Image1" runat="server" />

   </ItemTemplate>

   </asp:TemplateField>

   </Columns>

</asp:GridView>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

   if ( < 0)

   return;

   //

   string strPersonName = (string)(, "personName");

   Image tmp_Image = (Image)[2].FindControl("Image1");

   if (!((, "personPhoto")))

   {

   //

   byte[] photo = (byte[])(, "personPhoto");

//Picture path

   string strPath = "~/photo/" + () + ".JPG";

   string strPhotoPath = (strPath);

//Save the picture file

   BinaryWriter bw = new BinaryWriter((strPhotoPath, ));

   (photo);

   ();

//Show pictures

   tmp_Image.ImageUrl = strPath;

   }
}