C # Read the image field of the Northwind database

Source: Internet
Author: User

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

}
}
}

Related Article

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.