Store and display the image control of the gridview control and the binary database of the image

Source: Internet
Author: User

1. store images in binary format to the database.
2. Read Binary Images and display them on the page
3. Set the image control to display the binary image read from the database.
4. The imagefield in the gridview displays the image in URL mode.
5. display the Read Binary Image in the gridview
================================

1. store images in binary format to the database.
--------------------------
// Save the image to the database
Protected void button#click (Object sender, eventargs E)
{
// Image path
String strpath = "~ /Photo/03.jpg ";
String strphotopath = server. mappath (strpath );
// Read the image
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 ();
// Save
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 ();
}

2. Read Binary Images and display them on the page
--------------------------
// Read the image
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 ();

Or

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. set the image control to display the binary image read from the database
-----------------------------------------
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 = 'hangzhou' ", myconn );
dataset myds = new dataset ();
myconn. open ();
myda. Fill (myds);
myconn. close ();
//
byte [] photo = (byte []) myds. tables [0]. rows [0] ["personphoto"];
// image path
string strpath = "~ /Photo/wangwu. jpg ";
string strphotopath = server. mappath (strpath);
// Save the image file
binarywriter BW = new binarywriter (file. open (strphotopath, filemode. openorcreate);
BW. write (photo);
BW. close ();
// display the image
This. image1.imageurl = strpath;

4. The imagefield in the gridview displays the image in URL mode.
----------------------------
<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false">
<Columns>
<Asp: boundfield datafield = "personname" headertext = "name"/>
<Asp: imagefield dataimageurlfield = "personphotopath"
Headertext = "image">
</ASP: imagefield>
</Columns>
</ASP: gridview>
Bind directly to the background

5. display the Read Binary Image in the gridview
------------------------------
// Sample column
<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false" onrowdatabound = "gridview1_rowdatabound">
<Columns>
<Asp: boundfield datafield = "personname" headertext = "name"/>
<Asp: imagefield dataimageurlfield = "personphotopath"
Headertext = "image">
</ASP: imagefield>
<Asp: templatefield headertext = "image">
<Itemtemplate>
<Asp: Image id = "image1" runat = "server"/>
</Itemtemplate>
</ASP: templatefield>
</Columns>
</ASP: gridview>

// Bind
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 ");
// Image path
String strpath = "~ /Photo/"+ strpersonname. Trim () +". jpg ";
String strphotopath = server. mappath (strpath );
// Save the image file
Binarywriter BW = new binarywriter (file. Open (strphotopath, filemode. openorcreate ));
Bw. Write (photo );
Bw. Close ();
// Display the image
Tmp_image.imageurl = strpath;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.