Sream bytes[] img Convert each other

Source: Internet
Author: User

/// <summary>///turn the Stream into 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); //sets the position of the current stream as the start of the streamStream. Seek (0, Seekorigin.begin); returnbytes;}/// <summary>///turn byte[] into a Stream/// </summary>/// <param name= "bytes" ></param>/// <returns></returns> Public StaticStream Bytestostream (byte[] bytes) {Stream Stream=NewMemoryStream (bytes); returnstream;}/// <summary> ///convert bytes into pictures/// </summary> /// <param name= "Byt" >the stream of bytes to convert</param> /// <returns>the image object to be converted</returns>  Public StaticImage byttoimg (byte[] byt) {    Try{MemoryStream ms=NewMemoryStream (byt); Image img=Image.fromstream (MS); returnimg; }    Catch(Exception ex) {Loghelper.writeerror ("Streamhelper.byttoimg Exception", ex); return NULL; }}/// <summary>///convert a picture into a byte stream/// </summary>/// <param name= "img" ></param>/// <returns></returns> Public Static byte[] ImageToByteArray (Image img) {imageconverter Imgconv=NewImageConverter (); byte[] B = (byte[]) Imgconv. ConvertTo (IMG,typeof(byte[])); returnb;}/// <summary>///Convert image URL to image object/// </summary>/// <param name= "ImageUrl" ></param>/// <returns></returns> Public StaticImage url2img (stringimageUrl) {    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 (); returnimage; }    Catch(Exception ex) {Loghelper.writeerror ("Streamhelper.url2img Exception", ex); }    return NULL;}/// <summary>///Convert local image path to image object/// </summary>/// <param name= "ImagePath" ></param>/// <returns></returns> Public StaticImage imagepath2img (stringImagePath) {    Try    {        if(string. IsNullOrEmpty (ImagePath)) {return NULL; }        byte[] bytes =Image2bytewithpath (ImagePath); Image Image=byttoimg (bytes); returnimage; }    Catch(Exception ex) {Loghelper.writeerror ("Streamhelper.imagepath2img Exception", ex); return NULL; }}

Sream bytes[] img Convert each other

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.