原文:Byte[]、Image、Bitmap 之间的相互转换

/// <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(); 
            } 
        } 
    }

Byte[]、Image、Bitmap 之间的相互转换的更多相关文章

  1. C# Byte[]、Image、Bitmap 之间的相互转换

    //byte[] 转图片 public static Bitmap BytesToBitmap(byte[] Bytes) { MemoryStream stream = null; try { st ...

  2. android开发之Bitmap 、byte[] 、 Drawable之间的相互转换

    一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...

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

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

  4. byte[],bitmap,drawable之间的相互转换

    Byte[]转Bitmap BitmapFactory.decodeByteArray(data, 0, data.length); Bitmap转Byte[] ByteArrayOutputStre ...

  5. Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】

    package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; ...

  6. Byte[]、Image、Bitmap_之间的相互转换

    1.将图片Image转换成Byte[] /// <summary>        /// 将图片Image转换成Byte[]        /// </summary>     ...

  7. Android中Bitmap对象和字节流之间的相互转换

    android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte   import java.io.B ...

  8. Android中Bitmap对象和字节流之间的相互转换(转)

    android 将图片内容解析成字节数组:将字节数组转换为ImageView可调用的Bitmap对象:图片缩放:把字节数组保存为一个文件:把Bitmap转Byte import java.io.Buf ...

  9. Python网络编程——主机字节序和网络字节序之间的相互转换

    If you ever need to write a low-level network application, it may be necessary to handle the low-lev ...

随机推荐

  1. Redis相关命令及Jedis的demo(转)

    org.springframework.data.redis.core.RedisTemplate在List操作时的一个注意事项:BoundListOperations boundListOperat ...

  2. WinDbg调试.NET

    WinDbg调试.NET程序入门 俗话说:万事开头难! 自从来到新公司遇到性能问题后,需要想办法解决这个问题,但是一直没有合适的性能分析工具,然后找到StevenChennet 大神帮忙,他用WinD ...

  3. 章节2:SQL之多表连接

    原文:章节2:SQL之多表连接 Sql的多表连接关系有:内连接.外连接和交叉连接. 先建立两个用于演示的表: TB_Characters: Id Character 1 内向 2 外向 3 中性性格 ...

  4. Rational Rose 2007使用小结

    1.Rose怎样隐藏类的属性和操作? 右击类,选Options->Suppress Attributes/Suppress Operations 2.Rose怎样表示类的约束? 在工具箱中选AB ...

  5. unity 编辑器和插件生产(四.2)

    上次 我们告诉编辑器制作,如何将图像加载到现场,如今 我们要告诉下.怎么样 制造UIButton以及UIimage交换. 阿土. 进入专题. 首先,我们要明白 unity机制.button属性等. 首 ...

  6. poj2431 Expedition

    直接代码... #include<string.h> #include<stdio.h> #include<queue> #include<iostream& ...

  7. 于ios7在遇到一些发展deprecated问题

    cell.textLabel.textAlignment = UITextAlignmentCenter; 现在我想写cell.textLabel.textAlignment =NSTextAlign ...

  8. ABP入门教程

    ABP入门教程 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应 ...

  9. MVC推荐教程和文章列表

    着手Getting Started Getting Started with ASP.NET MVC 5 (共11部分) Pluralsight ASP.NET MVC 5 Fundamentals( ...

  10. 基于 Android 的 3D 视频样本代码

    作者:Mark Liu 下载样本代码 简单介绍 在Android 中,创建一个可以播放视频剪辑的应用很easy:创建一个採用 3D 图形平面的游戏应用也很easy.可是,创建一个可以在 3D 图形对象 ...