(一)文件轉化為byte[]
方法一:使用MemoryStream(MemoryStream的數據來自內存中緩沖區)
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); //從內存緩沖區中讀取 byte[] b = m.GetBuffer();
方法二:使用FileStream(FileStream對象的數據來自文件)
FileStream stream = new FileInfo(path).OpenRead(); Byte[] buffer = new Byte[stream.Length]; //從流中讀取字節塊並將該數據寫入給定緩沖區buffer中 stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
(二)將byte[]轉化成文件
WebReference.MyService obj = new WebReference.MyService(); Byte[] b = obj.downWav ("1001");//1001某條記錄的pid,返回這條記錄中的圖片的byte[] MemoryStream m = new MemoryStream(b); string file =string.Format(@"{0}\e.wav", Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)); //將下載下來的文件放在當前目錄下,保存為e.wav;當然如果是圖片,可以保存為a.jpg FileStream fs = new FileStream(file, FileMode.OpenOrCreate); m.WriteTo(fs); m.Close(); fs.Close(); m = null; fs = null;
