C#语言的Image和byte数组的互相转换

        /// <summary>
        /// 字节数组转换为图片
        /// </summary>
        /// <param name="buffer">字节数组</param>
        /// <returns>图片</returns>
        public Image BytesToImage(Byte[] buffer)
        {
            var ms = new MemoryStream(buffer, 0, buffer.Length);
            var bf = new BinaryFormatter();
            object obj = bf.Deserialize(ms);
            ms.Close();
            return (Image)obj;
        }
        /// <summary>
        /// 图片转换为字节数组
        /// </summary>
        /// <param name="image">图片</param>
        /// <returns>字节数组</returns>
        public byte[] ImageToBytes(Image image)
        {
            var ms = new MemoryStream();
            var bf = new BinaryFormatter();
            bf.Serialize(ms, image);
            ms.Close();
            return ms.ToArray();
        } 

偶然间用到,记下来防止忘记

你可能感兴趣的:(image)