C # reading the Northwind database Image field _ Practical tips

Source: Internet
Author: User
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 ();

}
}
}

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.