File and byte[in C #] interchange issues

Source: Internet
Author: User

How do I convert pictures and sounds to byte[] and transfer them through webservice?
How to transfer the WebService to byte[] and convert it into the file we want?
(i) Conversion of documents to byte[]
Method One: Use MemoryStream (MemoryStream data from in-memory buffers)
System.IO.MemoryStream m = new System.IO.MemoryStream ();
System.Drawing.Bitmap bp = new System.Drawing.Bitmap (pname);
Bp. Save (M, System.Drawing.Imaging.ImageFormat.Jpeg);//saves this image to the specified stream in the specified format.
Byte[] B = m.getbuffer ();//read from memory buffer
Method Two: Use FileStream (FileStream object's data from file)
FileStream stream = new FileInfo (path). OpenRead ();
byte[] buffer = new Byte[stream. Length];
Reads a block of bytes from the stream and writes the data to the given buffer
Stream. Read (buffer, 0, Convert.ToInt32 (stream). Length));
(ii) conversion of byte[] into a document
Webreference.myservice obj = new Webreference.myservice ();
Byte[] B = obj.downwav ("1001"),//1001 the PID of a record, returns the byte[of the picture in this record]
MemoryStream m = new MemoryStream (b);
String file =string. Format (@ "{0}\e.wav",
Path.getdirectoryname (assembly.getexecutingassembly (). GetName (). CodeBase));
Put the downloaded files in the current directory, save as E.wav, of course, if it is a picture, can be saved as a.jpg
FileStream fs = new FileStream (file, FileMode.OpenOrCreate);
M.writeto (FS);
M.close ();
Fs. Close ();
m = null;
FS = null; File and byte[in C #] interchange issues

From:http://www.cnblogs.com/qiuweiguo/archive/2011/08/05/2128690.html

File and byte[in C #] interchange issues

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.