The following small series for everyone to bring a picture in C #. Byte[] and base64string conversion methods. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
In C #
Picture to byte[] and then to base64string conversion:
Bitmap bmp = new Bitmap (filepath); MemoryStream ms = new MemoryStream (); Bmp. Save (MS, System.Drawing.Imaging.ImageFormat.Gif); byte[] arr = new Byte[ms. Length]; Ms. Position = 0; Ms. Read (arr, 0, (int) Ms. Length); Ms. Close (); string pic = convert.tobase64string (arr);
Base64string to byte[] again to the conversion of the picture:
byte[] imagebytes = convert.frombase64string (pic);//Read in MemoryStream object MemoryStream MemoryStream = new MemoryStream ( Imagebytes, 0, imagebytes.length); Memorystream.write (imagebytes, 0, imagebytes.length);//turn to image image = Image.fromstream (MemoryStream);
Now in the database development: The picture is generally stored in a CLOB: storage base64string
BLOB: Storage byte[]
Generally recommended for use with byte[]. Because pictures can be converted directly to byte[] stored in the database
If you use base64string you also need to convert from byte[] to base64string. More wasted performance.