/// <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