三类资源:流对象Stream,字节数组byte[],图片Image

关系:Stream<=>byte[],byte[]<=>Image    

Stream 与Image相互转化的媒介是byte[]

代码:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sunshine.application.common
{
    //三类资源:流对象Stream,字节数组byte[],图片Image
    //Stream/Bytes[]/Image对象相互转化,http://www.cnblogs.com/fanyong/p/3938017.html
    //关系:Stream<=>byte[],byte[]<=>Image    
    public class ImageToByteHelper
    {
        /*-------------------------------------------Stream 与Image相互转化的媒介是byte[],开始-------------------------------------------------*/
        /// <summary>
        /// 将 Stream 转成 byte[]
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Seek(0, SeekOrigin.Begin);
            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="img">要转换的Image对象</param>
        /// <returns>转换后返回的字节流</returns>
        public static byte[] ImgToByt(Image img)
        {
            MemoryStream ms = new MemoryStream();
            byte[] imagedata = null;
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            imagedata = ms.GetBuffer();
            return imagedata;
        }
       
        /// <summary>
        /// 字节流转换成图片
        /// </summary>
        /// <param name="byt">要转换的字节流</param>
        /// <returns>转换得到的Image对象</returns>
        public static Image BytToImg(byte[] byt)
        {
            MemoryStream ms = new MemoryStream(byt);
            Image img = Image.FromStream(ms);
            return img;
        }

        /*-------------------------------------------Stream 与Image相互转化的媒介是byte[],结束-------------------------------------------------*/

        /// <summary>
        /// 图片转换成字节流
        /// </summary>
        /// <param name="img">要转换的Image对象</param>
        /// <returns>转换后返回的字节流</returns>
        public static Stream ImgToStream(Image img)
        {
            byte[] imgByte = ImgToByt(img);
            //img.Save(ms, img.RawFormat);//System.Drawing.Imaging.ImageFormat.Jpeg
            Stream stream = new MemoryStream(imgByte);
            return stream;
        }
        //
        /// <summary>
        /// 根据图片路径返回图片的字节流byte[]
        /// </summary>
        /// <param name="imagePath">图片路径</param>
        /// <returns>返回的字节流</returns>
        private static byte[] getImageFielByte(string imagePath)
        {
            FileStream files = new FileStream(imagePath, FileMode.Open);
            byte[] imgByte = new byte[files.Length];
            files.Read(imgByte, 0, imgByte.Length);
            files.Close();
            return imgByte;
        }
    }
}

 

参考:

Stream/Bytes[]/Image对象相互转化

感谢参考的文章的作者的总结与付出!

 

posted on 2019-08-12 09:52  踏歌&而行  阅读(1121)  评论(0编辑  收藏  举报