Asp.net stores images in the database and uses file streams to read and display images.

Source: Internet
Author: User

I. There are several ways to store images in databases:
1. Replace the image with a binary value (byte [])
Byte [] fileData = this. FileUpload1.FileBytes;
2. Replace the file with a binary array based on the path
Private byte [] returnbyte (string strpath)
{
// Read the file in binary mode
FileStream fsMyfile = new FileStream (strpath, FileMode. OpenOrCreate, FileAccess. ReadWrite );
// Create a binary data stream reader and associate it with the opened file
BinaryReader brMyfile = new BinaryReader (fsMyfile );
// Relocates the file pointer to the beginning of the file
BrMyfile. BaseStream. Seek (0, SeekOrigin. Begin );
Byte [] bytes = brMyfile. ReadBytes (Convert. ToInt32 (fsMyfile. Length. ToString ()));
// Close all objects of the new object above
BrMyfile. Close ();
Return bytes;
}
3. Obtain a binary array of the Image type.
Public static byte [] Getbyte (Image img)
{
MemoryStream stream = new MemoryStream ();
Img. Save (stream, ImageFormat. Jpeg); // Image. FromFile (Path). Save (stream, ImageFormat. Jpeg );;
Byte [] mydata = new byte [stream. Length];
Mydata = stream. ToArray ();
Stream. Close ();
Return mydata;
}
 
Ii. Image Display
The method for reading image data and displaying it on a webpage is as follows:
1. directly return the image type
Private System. Drawing. Image GetImageDataFromDB ()
{
String SQL = "select Picture from TPicture where ID = 1 ";
String strconn = System. Configuration. ConfigurationManager. ConnectionStrings ["Conn"]. ToString ();
SqlConnection conn = new SqlConnection (strconn );
SqlCommand cmd = new SqlCommand (SQL, conn );
Conn. Open ();
Byte [] filedata = (byte []) cmd. ExecuteScalar ();
Conn. Close ();
System. IO. MemoryStream MS = new MemoryStream (filedata );
System. Drawing. Image img = System. Drawing. Image. FromStream (MS );
Return img;
}
2. Use Page output to display images
Page ShowImage. aspx (Page_Load) Method
Protected void Page_Load (object sender, EventArgs e)
{
String SQL = "select Picture from TPicture ";
String strconn = System. Configuration. ConfigurationManager. ConnectionStrings ["Conn"]. ToString ();
SqlConnection conn = new SqlConnection (strconn );
SqlDataAdapter da = new SqlDataAdapter (SQL, conn );
DataTable dt = new DataTable ();
Da. Fill (dt );
Byte [] logoimg = (byte []) dt. Rows [0] ["Picture"];
If (logoimg. Length> 0)
{
System. Drawing. Image img;
MemoryStream MS = new MemoryStream (logoimg );
Response. Clear ();
Response. ContentType = "image/gif ";
Response. OutputStream. Write (logoimg, 0, logoimg. Length );
Response. End ();
}
}
Image path:
 
Set the image type in the database to image.
Example:
SqlConnection conn = new SqlConnection (strconn );
Conn. Open ();
String SQL = "insert into TPicture (Picture) values (@ Picture )";
SqlCommand cmd = new SqlCommand (SQL, conn );
SqlParameter para = new SqlParameter ("@ Picture", SqlDbType. Image );
// Para. Value = FileUpload1.FileBytes;
Para. Value = Getbyte (null); // byte array
Cmd. Parameters. Add (para );
Cmd. ExecuteNonQuery ();

Author "Explorer"

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.