Asp.net: how to store images in a database and read images

Source: Internet
Author: User
Tags oracleconnection

There are a lot of documents on uploading images to the database via ASP. NET on the Internet, which are commonly used as follows:
You can store image data in the following ways:
1. Convert the image to a binary array (byte [])
Copy codeThe Code is as follows:
Byte [] fileData = this. FileUpload1.FileBytes;

2. Convert the file to a binary array based on the path
Copy codeThe Code is as follows:
Code
Public 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;
}

3img-type binary array
Copy codeThe Code is as follows:
Public static byte [] Getbyte (Image img)
{
MemoryStream stream = new MemoryStream ();
Img. Save (stream, ImageFormat. Jpeg );
Byte [] mydata = new byte [stream. Length];
Mydata = stream. ToArray ();
Stream. Close ();
Return mydata;
}

The method for reading image data and displaying it on a webpage is as follows:
1. Returns the image type directly.
Copy codeThe Code is as follows:
Private System. Drawing. Image getImageDataFromOracle ()
{
String SQL = "select IMGDATA from t_img where imgID = 100 ";
String strconn = System. Configuration. ConfigurationManager. ConnectionStrings ["ConnectionStringForOracle"]. ToString ();
OracleConnection oraConn = new OracleConnection (strconn );
OracleCommand oraComm = new OracleCommand (SQL, oraConn );
OraConn. Open ();
Byte [] fileData = (byte []) oraComm. ExecuteScalar ();
OraConn. Close ();
System. IO. MemoryStream MS = new System. IO. MemoryStream (fileData );
System. Drawing. Image img = System. Drawing. Image. FromStream (MS );
Return img;
}

2. Use Page input to display images
Page ImageShow. aspx (Page_Load method)
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
Byte [] B _logoImg = (byte []) dt_channelImg.Rows [0] ["LogoImage"]; // obtain the byte [] array. Here is just an example.
If (B _logoImg.Length> 0)
{
System. Drawing. Image logoImg;
MemoryStream MS = new MemoryStream (B _logoImg );
Response. Clear ();
Response. ContentType = "image/gif ";
Response. OutputStream. Write (B _logoImg, 0, B _logoImg.Length );
Response. End ();
}
}

Write the image path to

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.