C#遠(yuǎn)程發(fā)送和接收數(shù)據(jù)流生成圖片的方法
本文實(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)文章
c#代碼自動(dòng)修改解決方案下任意文件實(shí)例
這篇文章主要介紹了c#代碼自動(dòng)修改解決方案下任意文件實(shí)例,有需要的朋友可以參考一下2013-11-11
c#中object、var和dynamic的區(qū)別小結(jié)
這篇文章主要給大家介紹了關(guān)于c#中object、var和dynamic的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Unity UGUI的EventTrigger事件監(jiān)聽器組件介紹使用示例
這篇文章主要為大家介紹了Unity UGUI的EventTrigger事件監(jiān)聽器組件介紹及使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
C#通過創(chuàng)建Windows服務(wù)啟動(dòng)程序的方法詳解
這篇文章主要介紹了C#通過創(chuàng)建Windows服務(wù)啟動(dòng)程序的方法,較為詳細(xì)的分析了C#創(chuàng)建Windows服務(wù)應(yīng)用程序的步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-06-06

