It is worth mentioning that, unlike the winForm control, the web Control image can display images by reading binary streams and assigning values to the image attribute. It can be implemented through work und. The popular practice is to create a page dedicated to displaying images. Here the code is directly used by Chapter E of Mencius (with small modifications, remove 78 byte streams to display pictures of the northwind database ):
ReadImage. aspx. cs
Copy codeThe Code is as follows:
Using System;
Using System. Collections;
Using System. Configuration;
Using System. Data;
Using System. Linq;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Xml. Linq;
Using System. Data. SqlClient;
Namespace WebApplication2
{
Public partial class ReadImage: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
String strImageID = Request. QueryString ["id"];
SqlConnection myConnection = new SqlConnection ("Data Source =.; Initial Catalog = northwind; User Id = sa; Password = 123456 ;");
SqlCommand myCommand = new SqlCommand ("Select Picture from Categories Where CategoryID ="
+ StrImageID, myConnection );
Try
{
MyConnection. Open ();
SqlDataReader myDataReader;
MyDataReader = myCommand. ExecuteReader (CommandBehavior. CloseConnection );
If (myDataReader. Read ())
{
Response. Clear ();
Response. ContentType = "image/jpeg ";
Byte [] B = (byte []) myDataReader ["Picture"];
// The following method is used to display the image normally.
Byte [] temp = new byte [B. Length-78];
Array. Copy (B, 78, temp, 0, B. Length-78 );
Response. BinaryWrite (temp );
}
MyConnection. Close ();
}
Catch (SqlException SQLexc)
{
Response. Write (SQLexc. ToString ());
}
Response. End ();
}
}
}
You can use the following method to call the Source Page, such as Default. aspx. cs.
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
Image1.ImageUrl = FormatURL ("1 ");
}
Protected string FormatURL (string strArgument)
{
Return "ReadImage. aspx? Id = "+ strArgument;
}
If you do not want to create a page to carry images, you can also use the following method: (Note: The following classes are custom and you can understand this method)
Copy codeThe Code is as follows:
Using System;
Using System. Collections;
Using System. Configuration;
Using System. Data;
Using System. Linq;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Xml. Linq;
Using System. Data. SqlClient;
Namespace WebApplication2
{
Public partial class ReadImage: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
String strImageID = Request. QueryString ["id"];
SqlConnection myConnection = new SqlConnection ("Data Source =.; Initial Catalog = northwind; User Id = sa; Password = 123456 ;");
SqlCommand myCommand = new SqlCommand ("Select Picture from Categories Where CategoryID ="
+ StrImageID, myConnection );
Try
{
MyConnection. Open ();
SqlDataReader myDataReader;
MyDataReader = myCommand. ExecuteReader (CommandBehavior. CloseConnection );
If (myDataReader. Read ())
{
Response. Clear ();
Response. ContentType = "image/jpeg ";
Byte [] B = (byte []) myDataReader ["Picture"];
Byte [] temp = new byte [B. Length-78];
Array. Copy (B, 78, temp, 0, B. Length-78 );
Response. BinaryWrite (temp );
}
MyConnection. Close ();
}
Catch (SqlException SQLexc)
{
Response. Write (SQLexc. ToString ());
}
Response. End ();
}
}
}