C#远程发送和接收数据流生成图片的方法_C#教程-查字典教程网
C#远程发送和接收数据流生成图片的方法
C#远程发送和接收数据流生成图片的方法
发布时间:2016-12-28 来源:查字典编辑
摘要:本文实例讲述了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#程序设计有所帮助。

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新C#教程学习
    热门C#教程学习
    编程开发子分类