It is worth mentioning here that Web control image does not look like a WinForm control to display an image by reading the binary stream assignment to the Image property. Can be achieved through a flexible approach, the popular practice is to create a new page specifically used to display images, where the code directly with the Mencius E-Chapter predecessors (made a small change, the main is to eliminate 78 byte stream to normal display of the Northwind database pictures):
ReadImage.aspx.cs
Copy Code code 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=.;i Nitial 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 make the picture appear correctly
byte[] Temp=new byte [b.length-78];
Array.copy (b, b.length-78, temp, 0,);
Response.BinaryWrite (temp);
}
Myconnection.close ();
}
catch (SqlException Sqlexc)
{
Response.Write (Sqlexc.tostring ());
}
Response.End ();
}
}
}
On the source page such as Default.aspx.cs can be called by the following method
Copy Code code 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 new page to host the image, you can also use the following method: (Note: The following class is custom, you can understand this method is OK)
Copy Code code 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=.;i Nitial 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, b.length-78, temp, 0,);
Response.BinaryWrite (temp);
}
Myconnection.close ();
}
catch (SqlException Sqlexc)
{
Response.Write (Sqlexc.tostring ());
}
Response.End ();
}
}
}