asp.net the method of storing pictures in the database and reading pictures

Source: Internet
Author: User
Tags oracleconnection tostring

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:

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.