In project development, using SQL Server to store data, the data type image can save the picture. However, it is necessary to convert the picture into a binary array before storing it in the form of a value assignment.
Convert a picture file to a binary array
/// <summary> ///Convert a picture file to a binary array/// </summary> /// <param name= "Picpath" >Picture Path</param> /// <returns>Binary Arrays</returns> Private byte[] Getbinarydata (String picpath)//Convert a picture file to byte[]{ //read-only mode to open a picture to a streamFileStream FS=NewFileStream (Picpath, FileMode.Open, FileAccess.Read); //Initializes a binary array of exactly the same length imagebytesbyte[] Imagebytes=New byte[FS. Length]; //reads the full text from the stream and writes to the binary array imagebytesFS. Read (Imagebytes,0, Convert.ToInt32 (fs. Length)); //Remember to release the streamFS. Flush (); Fs. Close (); //back to Imagebytes returnimagebytes; }
Convert a picture file into a binary array in C #-for database storage