/// <summary>
        /// 将图片Image转换成Byte[]
        /// </summary>
        /// <param name="Image">image对象</param>
        /// <param name="imageFormat">后缀名</param>
        /// <returns></returns>
        public static byte[] ImageToBytes(Image Image, System.Drawing.Imaging.ImageFormat imageFormat)
        {

if (Image == null) { return null; }

byte[] data = null;

using (MemoryStream ms= new MemoryStream())
            {

using (Bitmap Bitmap = new Bitmap(Image))
                {

Bitmap.Save(ms, imageFormat);

ms.Position = 0;

data = new byte[ms.Length];

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

ms.Flush();

}

}

return data;

}

/// <summary>
            /// byte[]转换成Image
            /// </summary>
            /// <param name="byteArrayIn">二进制图片流</param>
            /// <returns>Image</returns>
            public static System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
            {
                if (byteArrayIn == null)
                    return null;
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArrayIn))
                {
                    System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
                    ms.Flush();
                    return returnImage;
                }
            }

//Image转换Bitmap

1. Bitmap img = new Bitmap(imgSelect.Image);

2. Bitmap bmp = (Bitmap)pictureBox1.Image;

//Bitmap转换成Image

using System.IO;

private static System.Windows.Controls.Image Bitmap2Image(System.Drawing.Bitmap Bi)
        {           
            MemoryStream ms = new MemoryStream();
            Bi.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            BitmapImage bImage = new BitmapImage();
            bImage.BeginInit();
            bImage.StreamSource = new MemoryStream(ms.ToArray());
            bImage.EndInit();
            ms.Dispose();
            Bi.Dispose();
            System.Windows.Controls.Image i = new System.Windows.Controls.Image();
            i.Source = bImage;
            return i ;
        }

//byte[] 转换 Bitmap
 public static Bitmap BytesToBitmap(byte[] Bytes) 
        { 
            MemoryStream stream = null; 
            try 
            { 
                stream = new MemoryStream(Bytes); 
                return new Bitmap((Image)new Bitmap(stream)); 
            } 
            catch (ArgumentNullException ex) 
            { 
                throw ex; 
            } 
            catch (ArgumentException ex) 
            { 
                throw ex; 
            } 
            finally 
            { 
                stream.Close(); 
            } 
        }  
 
//Bitmap转byte[]  
        public static byte[] BitmapToBytes(Bitmap Bitmap) 
        { 
            MemoryStream ms = null; 
            try 
            { 
                ms = new MemoryStream(); 
                Bitmap.Save(ms, Bitmap.RawFormat); 
                byte[] byteImage = new Byte[ms.Length]; 
                byteImage = ms.ToArray(); 
                return byteImage; 
            } 
            catch (ArgumentNullException ex) 
            { 
                throw ex; 
            } 
            finally 
            { 
                ms.Close(); 
            } 
        } 
    }

转载于:https://www.cnblogs.com/amylis_chen/p/3441294.html

Byte[]、Image、Bitmap 之间的相互转换相关推荐

  1. C# Image 、 byte[] 、Bitmap之间的转化

    一.Byte[] 转 System.Drawing.Bitmap public static Bitmap CreateBitmap(byte[] originalImageData, int ori ...

  2. String byte[] stream File之间的相互转换

    2019独角兽企业重金招聘Python工程师标准>>> //String 转 Stream public static InputStream stringTOInputStream ...

  3. File、byte[]、Bitmap、ImageView、资源中的图片 之间相互转换

    以下是安卓当中常用的 File.byte[].Bitmap.ImageView上的图片.资源图片 之间的转换 所用的代码 ImageView --> Bitmap --> File: 核心 ...

  4. php byte字节数组转stream,IStream与TStream之间的相互转换

    IStream与TStream之间的相互转换 更新时间:2008年08月01日 00:48:51   作者: IStream与TStream之间的相互转换的代码 procedure TForm1.Bu ...

  5. C#之windows桌面软件第八课:汉字(GB2312)与编码(UTF-8)之间的相互转换

    C#之windows桌面软件第八课:汉字(GB2312)与编码(UTF-8)之间的相互转换 using System; using System.Collections.Generic; using ...

  6. 有关WriteableBitmap和BitmapImage之间的相互转换

    对于WP7中图形处理有关WriteableBitmap和BitmapImage之间的相互转换,给大家几个简单实用的方法. 一.WriteableBitmap转为BitmapImage对象 var bi ...

  7. 字符编码之间的相互转换 UTF8与GBK

    原文地址为: 字符编码之间的相互转换 UTF8与GBK UTF8与GBK字符编码之间的相互转换 C++ UTF8编码转换 CChineseCode 一 预备知识 1,字符:字符是抽象的最小文本单位.它 ...

  8. UTF8与GBK字符编码之间的相互转换

    UTF8与GBK字符编码之间的相互转换   C++ UTF8编码转换 CChineseCode 一 预备知识 1,字符:字符是抽象的最小文本单位.它没有固定的形状(可能是一个字形),而且没有值.&qu ...

  9. 数组和list之间的相互转换

    数组和list之间的相互转换 数组转list 遍历 Arrays.asList()方法(有局限) Collections.addAll() java8 stream流 list转数组 toArray( ...

最新文章

  1. Keil错误Unable to automatically place AT section malloc.o(.ARM.__AT_0x20000000) with required base add
  2. Android C2DM学习 - 云端推送
  3. [转帖]linux下的X server:linux图形界面原理
  4. python安装caffe_Caffe安装笔记二:Caffe安装过程
  5. OpenGL于MFC使用汇总(三)——离屏渲染
  6. c语言 文件游程统计,游程 码表 如何形成
  7. 事务并发、事务隔离级别
  8. buildroot 编译问题
  9. 怎么用editplus写asm文件_本周分享 | 用Typora写markdown文件
  10. 机器学习之overfiting
  11. 单片机矩阵键盘扫描程序c语言,51单片机矩阵键盘扫描程序
  12. 图片查看器-Python-tkinter
  13. VUE仿知乎网站(四)登录注册页面开发+表单验证
  14. word只在第一页插入页眉
  15. (转)一些个人感悟(2008)
  16. 一次阿里巴巴的电话面试
  17. 双鱼林 android 服务器,双鱼林安卓Android代码生成器下载_双鱼林安卓Android代码生成器官方下载-太平洋下载中心...
  18. week6 day4 并发编程之多线程 理论
  19. Element 表单只能输入数字校验
  20. OFFICE 制作邀请函

热门文章

  1. 多路复用器_超详细的I/O多路复用概念、常用I/O模型、系统调用等介绍
  2. 中的live_2016知乎Live精选汇编——简历类
  3. CCNP精粹系列之十七--路由映射实战,博主推荐
  4. 欢迎界面java_Linux命令行欢迎界面美化
  5. JavaWeb——Mybatis逆向工程
  6. ArcGIS水文分析实战教程(14)提取水库湖泊等面状水系
  7. 使用ZeroBrane Studio调试OpenResty Lua脚本
  8. Windows下配置nginx+php(wnmp)
  9. 【java学习之路】(java SE篇)(练习)一些关于面向对象的题目与思考
  10. 特斯拉电池检测_特斯拉风格的割草机,也是采用电池供电