zoukankan      html  css  js  c++  java
  • Stream/Bytes[]/Image对象相互转化

    Stream转Byte数组、Image转Byte数组、文件转Stream等

    streamHelper

    /// <summary>
    /// 将 Stream 转成 byte[]
    /// </summary>
    /// <param name="stream"></param>
    /// <returns></returns>
    public static byte[] StreamToBytes(Stream stream)
    {
        byte[] bytes = new byte[stream.Length];
        stream.Read(bytes, 0, bytes.Length);
        // 设置当前流的位置为流的开始
        stream.Seek(0, SeekOrigin.Begin);
        return bytes;
    }
    
    /// <summary>
    /// 将 byte[] 转成 Stream
    /// </summary>
    /// <param name="bytes"></param>
    /// <returns></returns>
    public static Stream BytesToStream(byte[] bytes)
    {
        Stream stream = new MemoryStream(bytes);
        return stream;
    }
    
    /// <summary> 
    /// 字节流转换成图片 
    /// </summary> 
    /// <param name="byt">要转换的字节流</param> 
    /// <returns>转换得到的Image对象</returns> 
    public static Image BytToImg(byte[] byt)
    {
        try
        {
            MemoryStream ms = new MemoryStream(byt);
            Image img = Image.FromStream(ms);
            return img;
        }
        catch (Exception ex)
        {
            LogHelper.WriteError("StreamHelper.BytToImg 异常", ex);
            return null;
        }
    }
    
    /// <summary>
    ///  图片转换成字节流 
    /// </summary>
    /// <param name="img"></param>
    /// <returns></returns>
    public static byte[] ImageToByteArray(Image img)
    {
        ImageConverter imgconv = new ImageConverter();
        byte[] b = (byte[])imgconv.ConvertTo(img, typeof(byte[]));
        return b;
    }
    
    /// <summary>
    /// 把图片Url转化成Image对象
    /// </summary>
    /// <param name="imageUrl"></param>
    /// <returns></returns>
    public static Image Url2Img(string imageUrl)
    {
        try
        {
            if (string.IsNullOrEmpty(imageUrl))
            {
                return null;
            }
    
            WebRequest webreq = WebRequest.Create(imageUrl);
            WebResponse webres = webreq.GetResponse();
            Stream stream = webres.GetResponseStream();
            Image image;
            image = Image.FromStream(stream);
            stream.Close();
    
            return image;
        }
        catch (Exception ex)
        {
            LogHelper.WriteError("StreamHelper.Url2Img 异常", ex);
        }
    
        return null;
    }
    
    /// <summary>
    /// 把本地图片路径转成Image对象
    /// </summary>
    /// <param name="imagePath"></param>
    /// <returns></returns>
    public static Image ImagePath2Img(string imagePath)
    {
        try
        {
            if (string.IsNullOrEmpty(imagePath))
            {
                return null;
            }
    
            byte[] bytes = Image2ByteWithPath(imagePath);
            Image image = BytToImg(bytes);
            return image;
        }
        catch (Exception ex)
        {
            LogHelper.WriteError("StreamHelper.ImagePath2Img 异常", ex);
            return null;
        }
    }
    
    

    详细代码看这里

  • 相关阅读:
    设计模式 : Template method 模板方法模式 -- 行为型
    设计模式:Strategy 策略模式 -- 行为型
    没有评审的技术都是扯淡
    TCP/IP 协议大致的概念
    HTTP 2.0 与 tomcat
    相对URL拼接为绝对URL的过程
    URI与URL区别
    《淘宝首页性能优化实践》文章阅读
    第一章 Linux内核简介
    vim中执行shell命令小结
  • 原文地址:https://www.cnblogs.com/fanyong/p/3938017.html
Copyright © 2011-2022 走看看