您的位置:首页 > 编程语言 > C#

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

2010-01-30 13:36 288 查看
二进制数组和图像相互转换的函数:

//图像转换成二进制

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()); //将视频流写入到文件里

将此记录下来以备以后查用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: