读取流字符串、字符串格式转成json格式、base64转图片

一、读取流字符串

Stream resStream = verifyResponse.GetResponseStream();
// 方法一:
StreamReader sr = new StreamReader(resStream, Encoding.ASCII);
                string readerStr = sr.ReadToEnd();
                sr.Close();
                resStream.Close();
                verifyResponse.Close();
                return readerStr;
//方法二:
MemoryStream ms = new MemoryStream();
                byte[] buffer = new byte[4096];
                while (true)
                {
                    int sz = resStream.Read(buffer, 0, 4096);
                    if (sz == 0) break;
                    ms.Write(buffer, 0, sz);
                }
                string content = Encoding.UTF8.GetString(ms.ToArray());
                return content;
//方法四:
                int count = 4096;
                int offset = 0;
                var buf = new byte[count];
                while (count > 0)
                {
                    int n = resStream.Read(buf, offset, count);
                    if (n == 0) break;
                    count -= n;
                    offset += n;
                }
                string content = Encoding.Default.GetString(buf, 0, buf.Length);
                return content;   
//方法五:
					//开辟内存区域 1024 * 1024 bytes
                    byte[] readBytes = new byte[1024 * 1024 * 5];
                    //开始读数据
                    int count = resStream.Read(readBytes, 0, readBytes.Length);
                    //byte[] 转字符串
                    string readStr = Encoding.UTF8.GetString(readBytes, 0, count);
                    //关闭文件流
                    resStream.Close(); 
//---------------------------------------------------------------------------------------------------------                    
//方法六: 读取指定路径下文件的字符串内容
					string strContent = string.Empty;
                    FileStream fs = new FileStream(postset, FileMode.Open);
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    strContent = Encoding.Default.GetString(buffer);
                    fs.Close(); 
//方法七:读取指定路径下文件的字符串
					string content = "";
                    using (StreamReader sr = new StreamReader(postset))
                    {
                        content = sr.ReadToEnd();
                    }
                    return content;                                                               
//将指定的字符串保存到指定路径文件下
				using (DAL.MyDbContext db = new DAL.MyDbContext())
                {
                var system = db.System.FirstOrDefault();
                StreamReader sr = new StreamReader(res, Encoding.ASCII);
                string html = sr.ReadToEnd();//字符串
                System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath(@"~\Image\Template\A.txt"), System.IO.FileMode.Create, System.IO.FileAccess.Write);
                //利用新传来的路径实例化一个FileStream对像  
                System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs);
                //实例化一个用于写的BinaryWriter  
                bw.Write(html);
                bw.Close();
                fs.Close();
                sr.Close();
                res.Close();
                verifyResponse.Close();
                return html;
                }

二、字符串格式转成json格式

string postset="xxxxxxxxxxxxxxxxxxxxxxxxxxx";
//方法一:
			JObject jo = JObject.Parse(postset);
                    JToken confidence = null;
                    if (jo.TryGetValue("request_id", out confidence))
                    {
                        int persent = Int32.Parse(confidence.ToString());
                    }
//方法二:(字符串格式)
       JObject read = JsonConvert.DeserializeObject<JObject>(postset);
                    string resul = read["result"].ToString();
                    string id = read["request_id"].ToString();  
//方法三:(对象形式)(尝试未实现,但有此写法)
//需要创建一个类,存储反馈的成员属性
	public class MergeFace
    {
        /// <summary>
        /// 请求的唯一的字符串
        /// </summary>
        public string request_id { get; set; }
        /// <summary>
        /// 融合后的图片(base64 编码的二进制图片)
        /// </summary>
        public string result { get; set; }
        /// <summary>
        /// 整个请求所花费的时间(单位为毫秒)
        /// </summary>
        public string time_used { get; set; }
        /// <summary>
        /// 当发生错误时才返回
        /// </summary>
        public string error_message { get; set; }
    }
    /// <summary>
    /// 字符串转json格式
    /// </summary>
    /// <param name="jsonStr">需要转的字符串</param>
    /// <returns></returns>
    public MergeFace GetData(string jsonStr)
    {
       return (MergeFace)Newtonsoft.Json.JsonConvert.DeserializeObject(jsonStr, typeof(/*MergeFace*/));
    }
   MergeFace merface = GetData(readStr);
   var read = JsonConvert.DeserializeObject<MergeFace>(readStr);
   string err=read.error_message;
   string result=read.result;

三、base64字符串转图片

string base64="xxxxxxxxxxxxx";
//方法一:
                byte[] bytes = Convert.FromBase64String(base64);
                MemoryStream ms = new MemoryStream();
                ms.Write(bytes, 0, bytes.Length);
                Bitmap bmp = new Bitmap(ms);
                return bmp;
//方法二:
                byte[] bytes = Convert.FromBase64String(base64);
                MemoryStream memStream = new MemoryStream(bytes);
                BinaryFormatter binFormatter = new BinaryFormatter();
                Image img = (Image)binFormatter.Deserialize(memStream);
                return img;
//方法三:
				byte[] bt = Convert.FromBase64String(base64);
                System.IO.MemoryStream stream = new System.IO.MemoryStream(bt);
                Bitmap bitmap = new Bitmap(stream);
                return bitmap;                           
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:C马雯娟 返回首页