Stream/Bytes []/Image object conversion, streambytes

Source: Internet
Author: User

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

 

Related Article

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.