Stream/Bytes []/Image object conversion, streambytes
Stream/Bytes []/Image object Conversion
Stream to Byte array, Image to Byte array, file to Stream, etc.
/// <Summary> /// convert Stream to byte [] /// </summary> /// <param name = "stream"> </param> /// <returns> </returns> public static byte [] StreamToBytes (Stream stream) {byte [] bytes = new byte [stream. length]; stream. read (bytes, 0, bytes. length); // set the current stream position to the starting stream of the stream. seek (0, SeekOrigin. begin); return bytes ;}Convert Stream to byte [] /// <summary> // convert byte [] to Stream /// </summary> /// <param name = "bytes"> </param> // <returns> </returns> public static Stream BytesToStream (byte [] bytes) {Stream stream = new MemoryStream (bytes); return stream ;}Byte [] to Stream // <summary> // change byte Stream to image // </summary> // <param name = "byt"> the byte Stream to be converted </param> // <returns> the converted Image object </returns> public static Image BytToImg (byte [] byt) {try {MemoryStream MS = new MemoryStream (byt); Image img = Image. fromStream (MS); return img;} catch (Exception ex) {LogHelper. writeError ("StreamHelper. bytToImg exception ", ex); return null ;}}Byte Transfer to image /// <summary> /// convert the image into a word throttling // </summary> /// <param name = "img"> </param>/ // <returns> </returns> public static byte [] ImageToByteArray (Image img) {ImageConverter imgconv = new ImageConverter (); byte [] B = (byte []) imgconv. convertize (img, typeof (byte []); return B ;}Converting an Image to a word-based throttling /// <summary> /// convert the Image Url to an Image object /// </summary> /// <param name = "imageUrl"> </ param> // <returns> </returns> public static Image Url2Img (string imageUrl) {try {if (string. isNullOrEmpty (imageUrl) {return null;} WebRequest webreq = WebRequest. create (imageUrl); WebResponse webres = webreq. getResponse (); Stream stream = webres. getResponseStream (); Image image; Image = Image. fromStream (stream); stream. close (); return image;} catch (Exception ex) {LogHelper. writeError ("StreamHelper. url2Img exception ", ex);} return null ;}Convert the Image Url to an Image object /// <summary> /// convert the local Image path to an Image object /// </summary> /// <param name = "imagePath"> </param> /// <returns> </returns> public static Image ImagePath2Img (string imagePath) {try {if (string. isNullOrEmpty (imagePath) {return null;} byte [] bytes = Image2ByteWithPath (imagePath); Image image = BytToImg (bytes); return image;} catch (Exception ex) {LogHelper. writeError ("StreamHelper. imagePath2Img exception ", ex); return null ;}}Convert an Image Url into an Image object