zoukankan      html  css  js  c++  java
  • 读取图片数据流转换成图片

      string imgUrl = "http://www.scapex.cn/upload/201601/28/201601280915393586.jpg";
                if (!string.IsNullOrEmpty(imgUrl))
                {
                    WebRequest webRequest = WebRequest.Create(new Uri(imgUrl));
                    WebResponse webResponse = webRequest.GetResponse();
                    Stream stream = webResponse.GetResponseStream();
                    byte[] bytes = new byte[stream.Length];
                    stream.Read(bytes, 0, bytes.Length);
                    stream.Seek(0, SeekOrigin.Begin);
                    string str = new System.Text.UnicodeEncoding().GetString(bytes);
                    
                    stream.Close();
                    webResponse.Close();
    
    
                    Bitmap myImage = new Bitmap(webResponse.GetResponseStream());
    
                    MemoryStream ms = new MemoryStream();
                    myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    Response.ClearContent();
                    Response.ContentType = "image/Jpeg";
                    Response.BinaryWrite(ms.ToArray());
    }
    读取文件转换成BaseCode  

    if (!File.Exists(filePath)) return baseCode; using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); baseCode = Convert.ToBase64String(bytes); }

    BaseCode保存文件
      if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    byte[] bytes = Convert.FromBase64String(model.BaseCode);
                    using (Stream fs = new FileStream(model.FilePath, FileMode.Create))
                    {
                        fs.Write(bytes, 0, bytes.Length);
                    }
    
    
    
     
     public static string GetBaseCode(string filePath)
            {
                string base64 = string.Empty;
                if (string.IsNullOrEmpty(filePath)) return base64;
    
                HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(filePath);
                HttpWebResponse response = (HttpWebResponse)webrequest.GetResponse();
                try
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (BinaryReader reader = new BinaryReader(stream))
                        {
                            byte[] arr = reader.ReadBytes(1024 * 500);
                            base64 = Convert.ToBase64String(arr);
                        }
                    }
                    response.Close();
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    response.Dispose();
                }
                return base64;
            }
    
    
    
    
    
  • 相关阅读:
    Python的运算符
    RabbitMQ 的配置文件
    安装新版本的rabbitmq
    Ubuntu 16.04 安装rabbitmq
    Python Web 版本tailf, grep
    解决pycharm问题:module 'pip' has no attribute 'main'
    Python argparse
    Ansible 并行和异步
    cef相关
    浏览器透明设置例子,qt5.6才支持
  • 原文地址:https://www.cnblogs.com/sunxuchu/p/5381415.html
Copyright © 2011-2022 走看看