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

首頁 > 編程 > C# > 正文

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

2020-01-24 01:35:49
字體:
來源:轉載
供稿:網友

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

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

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

發送數據流方法

/// <summary>/// PostBinaryData/// </summary>/// <param name="url">要發送的 url 網址</param>/// <param name="bytes">要發送的數據流</param>/// <returns></returns>public string PostBinaryData(string url, byte[] bytes){  //下面是測試例子  //string url = "http://www.test.com/test.ashx";  //string img = HttpContext.Current.Server.MapPath("../images/test.jpg");  //byte[] bytes = File.ReadAllBytes(img);  HttpWebRequest wRequest = (HttpWebRequest)WebRequest.Create(url);  wRequest.ContentType = "multipart/form-data";  wRequest.ContentLength = bytes.Length;  wRequest.Method = "POST";  Stream stream = wRequest.GetRequestStream();  stream.Write(bytes, 0, bytes.Length);  stream.Close();  HttpWebResponse wResponse = (HttpWebResponse)wRequest.GetResponse();  StreamReader sReader = new StreamReader(wResponse.GetResponseStream(), System.Text.Encoding.UTF8);  string str = sReader.ReadToEnd();  sReader.Close();  wResponse.Close();  return str;}

接收數據流方法

public void GetBinaryData(){  string imgFile = DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";  string filePath = HttpContext.Current.Server.MapPath(imgFile);  //方法一  int lang = HttpContext.Current.Request.TotalBytes;  byte[] bytes = HttpContext.Current.Request.BinaryRead(lang);  string content = System.Text.Encoding.UTF8.GetString(bytes);  FileStream fStream = new FileStream(filePath, FileMode.Create, FileAccess.Write);  BinaryWriter bw = new BinaryWriter(fStream);  bw.Write(bytes);  bw.Close();  fStream.Close();      //方法二  Bitmap img = new Bitmap(HttpContext.Current.Request.InputStream);  img.Save(filePath);  HttpContext.Current.Response.Write("ok");}

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 固安县| 祁连县| 闵行区| 扶余县| 峨山| 丹阳市| 乌兰察布市| 萨嘎县| 增城市| 阳西县| 百色市| 轮台县| 成安县| 云和县| 乐安县| 松滋市| 镇巴县| 天门市| 通渭县| 托克逊县| 安塞县| 平顶山市| 三门县| 屯留县| 凤阳县| 商丘市| 古田县| 阿拉善盟| 余姚市| 晋城| 石棉县| 和平县| 淳化县| 剑阁县| 梁河县| 山阳县| 井研县| 泊头市| 崇阳县| 卫辉市| 荣成市|