国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#遠程發送和接收數據流生成圖片的方法

2019-10-29 21:40:11
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了C#遠程發送和接收數據流生成圖片的方法,涉及C#通過數據流傳輸圖片的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了C#遠程發送和接收數據流生成圖片的方法。分享給大家供大家參考。具體如下:

將圖片轉成數據流方式發送到遠程服務,在通過服務器后臺程序來接收數據流,再保存成圖片存放在需要的地方。

這種方式就類似上傳圖片功能一樣,希望能給一些大家另一種上傳圖片功能的方法。

發送數據流方法

 

 
  1. /// <summary> 
  2. /// PostBinaryData 
  3. /// </summary> 
  4. /// <param name="url">要發送的 url 網址</param> 
  5. /// <param name="bytes">要發送的數據流</param> 
  6. /// <returns></returns> 
  7. public string PostBinaryData(string url, byte[] bytes) 
  8. //下面是測試例子 
  9. //string url = "http://www.test.com/test.ashx"; 
  10. //string img = HttpContext.Current.Server.MapPath("../images/test.jpg"); 
  11. //byte[] bytes = File.ReadAllBytes(img); 
  12. HttpWebRequest wRequest = (HttpWebRequest)WebRequest.Create(url); 
  13. wRequest.ContentType = "multipart/form-data"
  14. wRequest.ContentLength = bytes.Length; 
  15. wRequest.Method = "POST"
  16. Stream stream = wRequest.GetRequestStream(); 
  17. stream.Write(bytes, 0, bytes.Length); 
  18. stream.Close(); 
  19. HttpWebResponse wResponse = (HttpWebResponse)wRequest.GetResponse(); 
  20. StreamReader sReader = new StreamReader(wResponse.GetResponseStream(), System.Text.Encoding.UTF8); 
  21. string str = sReader.ReadToEnd(); 
  22. sReader.Close(); 
  23. wResponse.Close(); 
  24. return str; 

接收數據流方法

 

 
  1. public void GetBinaryData() 
  2. string imgFile = DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg"
  3. string filePath = HttpContext.Current.Server.MapPath(imgFile); 
  4. //方法一 
  5. int lang = HttpContext.Current.Request.TotalBytes; 
  6. byte[] bytes = HttpContext.Current.Request.BinaryRead(lang); 
  7. string content = System.Text.Encoding.UTF8.GetString(bytes); 
  8. FileStream fStream = new FileStream(filePath, FileMode.Create, FileAccess.Write); 
  9. BinaryWriter bw = new BinaryWriter(fStream); 
  10. bw.Write(bytes); 
  11. bw.Close(); 
  12. fStream.Close();  
  13. //方法二 
  14. Bitmap img = new Bitmap(HttpContext.Current.Request.InputStream); 
  15. img.Save(filePath); 
  16. HttpContext.Current.Response.Write("ok"); 

希望本文所述對大家的C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汾阳市| 镇安县| 广元市| 安宁市| 抚宁县| 隆子县| 绥德县| 新干县| 科技| 鄄城县| 永德县| 姜堰市| 静宁县| 塘沽区| 江孜县| 息烽县| 大厂| 高雄市| 海南省| 黄大仙区| 丰宁| 合山市| 五常市| 景宁| 邻水| 安福县| 翁源县| 九台市| 莫力| 时尚| 西安市| 宁陕县| 锦屏县| 开江县| 嫩江县| 博爱县| 武清区| 大丰市| 冷水江市| 开封市| 宣威市|