其他分享
首页 > 其他分享> > 图片和字节流之间的转换

图片和字节流之间的转换

作者:互联网

 

将图片保存成byte数组保存到数据库,将byte数组转为图片

      /// <summary>

        /// 把一幅图片转换成字节数组

        /// </summary>

        /// <param name="imageData"></param>

        /// <returns></returns>

        private byte[] ToByte(Image imageData)

        {

            MemoryStream ms = new MemoryStream();

            imageData.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);//把图像数据序列化到内存

            byte[] imgByte = new byte[ms.Length];

            ms.Position = 0;

            ms.Read(imgByte, 0, Convert.ToInt32(ms.Length));

            ms.Close();

            return imgByte;

        }

        /// <summary>

        /// 把获得数据流转为一副图片

        /// </summary>

        /// <param name="imageDataDetails"></param>

        /// <returns></returns>

        private Bitmap AddHeader(byte[] imageDataDetails)

        {

            if (imageDataDetails == null)

            {

                return null;

            }

            MemoryStream stream = new MemoryStream(imageDataDetails);

            System.Drawing.Bitmap result = new Bitmap(stream);

            return result;

        }

标签:转换,MemoryStream,imgByte,return,ms,new,byte,图片,字节
来源: https://blog.51cto.com/u_15194128/2761746