Online about asp.net upload pictures to the database is very much information, commonly used as follows:
There are several ways to store picture type data:
1. Convert a picture to a binary array (byte[])
Copy Code code as follows:
byte[] Filedata = this. Fileupload1.filebytes;
2. Convert a file to a 2 binary array based on the path
Copy Code code as follows:
Code
public byte[] Returnbyte (string strpath)
{
//Read files in binary mode
FileStream fsmyfile = new FileStream (strpath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
//Create a binary data stream reader, associated with an open file
BinaryReader brmyfile = new BinaryReader (fsmyfile);
//Reposition the file pointer to the beginning of the file
brMyfile.BaseStream.Seek (0, Seekorigin.begin);
byte[] bytes = brmyfile.readbytes (Convert.ToInt32 (fsMyfile.Length.ToString ()));
//Closes each object of the above new
Brmyfile.close ();
return bytes;
}
3img type gets binary array
Copy Code code 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;
}
Read the image type and display it on the Web page in the following ways:
1. Return image type directly
Copy Code code 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 pictures
Page imageshow.aspx (Page_Load method)
Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
byte[] b_logoimg = (byte[]) dt_channelimg.rows[0]["Logoimage"]; Get byte[] Array, here's 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 ();
}
}
Picture path written as: