C#中二进制数组和图片之间的相互转换

二进制数组和图像相互转换的函数: 

      //图像转换成二进制

       private byte[] ImageToByte(Image Picture)
        {
            MemoryStream ms = new MemoryStream();
            if (Picture == null)
                return new byte[ms.Length];
            Picture.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] BPicture = new byte[ms.Length];
            BPicture = ms.GetBuffer();
            return BPicture;
        }
        //二进制转换为图像
        private Image ByteToImage(byte[] btImage)
        {
            if (btImage.Length == 0)
                return null;
            System.IO.MemoryStream ms = new System.IO.MemoryStream(btImage);
            System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
            return image;
        }

1、定义对象

public class PicClass
{
        public List<byte[]> img { get; set; }    //图片
}

 

//生成图片的方法
PicClass pc = new PicClass();

List<byte[]> imgs = new List<byte[]>();
byte[] img = new byte[0];
img = (byte[])pic.image;       //pic.image是返回的图片的二进制数组
imgs.Add(img);
pc.img = imgs;                    

Image image = ByteToImage(btimage);  //将二进制数组转换成image图片
pictureBox1.Image = image;                   //图片显示在picturebox框里

 

//生成视频的方法

List<byte[]> imgs = new List<byte[]>();
myPic pic = new myPic();                             //服务定义的对象
pic=client.queryVioVideo("", "", "", "");       //调用服务获取视频流
byte[] img = new byte[0];
img = (byte[])pic.video;       //pic.video是接收的视频的二进制数组
PicClass pc = new PicClass();
imgs.Add(img);
pc.img=imgs;
File.WriteAllBytes("D://1.avi", pc.img.First()); //将视频流写入到文件里

 

 将此记录下来以备以后查用

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值