Asp.net stores images in the database (I)

Source: Internet
Author: User

Recently, in this project, you need to convert the client image to binary and save it to the database.

Convert the image to binary:

(1) Converting through the image path

Private byte [] redyte (string strpath) {// read the file 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); // locate the file pointer to the start of brmyfile. basestream. seek (0, seekorigin. begin); byte [] bytes = brmyfile. readbytes (convert. toint32 (fsmyfile. length. tostring (); // close each of the above new objects brmyfile. close (); Return bytes ;}

(2) Converting through image objects

 private 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;        }

(3) convert binary data into images

private System.Drawing.Image getImage(byte[] fileData)        {           System.IO.MemoryStream ms = new System.IO.MemoryStream(fileData);           System.Drawing.Image img = System.Drawing.Image.FromStream(ms);             return img;        } 

(4) use image to display

Private void imgshow (workerinfo) {string strpicture = ""; system. drawing. image imgpictureshow = getimage (workerinfo. photo); // define the file name strpicture = "picture" + datetime. now. tostring ("yymmddhhmmss"); imgpictureshow. save (server. mappath ("~ /PIC/") + strpicture +" jpg ", system. drawing. imaging. imageformat. JPEG); // display the file imgpicture. imageurl = "PIC/" + strpicture + ". jpg ";

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.