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


    三类资源:流对象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对象相互转化

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

  • 相关阅读:
    ◆◆0[问题解决]REUSE_ALV_FIELDCATALOG_MERGE丢失catalog字段
    不同类型ALV的catalog数据转换[LVC_TRANSFER_TO_SLIS][LVC_TRANSFER_FROM_SLIS]
    ◆◆0ABAP ALV分类以及对应的函数,类小结
    [代码]如何在ALV头中显示Logo图片-[REUSE_ALV_GRID_DISPLAY]
    ◆◆0SALV-如何刷新显示
    ◆◆0[REUSE_ALV_GRID_DISPLAY_LVC]-显示单选按钮(radio button)
    ◆◆0[REUSE_ALV_GRID_DISPLAY_LVC]ALV中字段显示前导零(leading zero)
    ◆◆0[REUSE_ALV_GRID_DISPLAY_LVC]-显示布局按钮(layout button)
    [问题解决]ALV标准过滤功能失效
    ◆◆0SALV-设置SALV选择模式
  • 原文地址:https://www.cnblogs.com/wangqiideal/p/11338010.html
Copyright © 2020-2023  润新知