欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#遠(yuǎn)程發(fā)送和接收數(shù)據(jù)流生成圖片的方法

 更新時(shí)間:2015年07月22日 11:23:05   作者:jwang  
這篇文章主要介紹了C#遠(yuǎn)程發(fā)送和接收數(shù)據(jù)流生成圖片的方法,涉及C#通過數(shù)據(jù)流傳輸圖片的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#遠(yuǎn)程發(fā)送和接收數(shù)據(jù)流生成圖片的方法。分享給大家供大家參考。具體如下:

將圖片轉(zhuǎn)成數(shù)據(jù)流方式發(fā)送到遠(yuǎn)程服務(wù),在通過服務(wù)器后臺(tái)程序來接收數(shù)據(jù)流,再保存成圖片存放在需要的地方。

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

發(fā)送數(shù)據(jù)流方法

/// <summary>
/// PostBinaryData
/// </summary>
/// <param name="url">要發(fā)送的 url 網(wǎng)址</param>
/// <param name="bytes">要發(fā)送的數(shù)據(jù)流</param>
/// <returns></returns>
public string PostBinaryData(string url, byte[] bytes)
{
  //下面是測(cè)試?yán)?
  //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;
}

接收數(shù)據(jù)流方法

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");
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論