图片和字节流之间的转换

 

将图片保存成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;

        }

上一篇:C#之DateTime日期格式解析


下一篇:php文件缓存方法总结