C#長途發送和吸收數據流生成圖片的辦法。本站提示廣大學習愛好者:(C#長途發送和吸收數據流生成圖片的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#長途發送和吸收數據流生成圖片的辦法正文
本文實例講述了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#法式設計有所贊助。