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"