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