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)文章
VB.NET中TextBox的智能感知應(yīng)用實(shí)例
這篇文章主要介紹了VB.NET中TextBox的智能感知應(yīng)用實(shí)例,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08C# OpenCvSharp利用白平衡技術(shù)實(shí)現(xiàn)圖像修復(fù)功能
這篇文章主要為大家詳細(xì)介紹了C# OpenCvSharp如何利用白平衡技術(shù)實(shí)現(xiàn)圖像修復(fù)功能,文中的示例代碼講解詳細(xì),希望對(duì)大家有一定的幫助2024-02-02Unity學(xué)習(xí)之FSM有限狀態(tài)機(jī)
這篇文章主要介紹了Unity學(xué)習(xí)之FSM有限狀態(tài)機(jī),通過詳細(xì)的代碼案例來進(jìn)行解析說明,希望這篇文章對(duì)你有所幫助2021-06-06非常實(shí)用的C#字符串操作處理類StringHelper.cs
這篇文章主要為大家詳細(xì)介紹了非常實(shí)用的C#字符串操作處理類StringHelper.cs,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07C#通過oledb訪問access數(shù)據(jù)庫的方法
這篇文章主要介紹了C#通過oledb訪問access數(shù)據(jù)庫的方法,實(shí)例分析了C#操作access數(shù)據(jù)庫的相關(guān)技巧,需要的朋友可以參考下2015-06-06C#使用委托實(shí)現(xiàn)的快速排序算法實(shí)例
這篇文章主要介紹了C#使用委托實(shí)現(xiàn)的快速排序算法,實(shí)例分析了C#委托機(jī)制與快速排序算法的實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-07-07