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 ";