将Image对象转化成二进制流方法
Demo:
===============================================================================
///<summary>
///将Image对象转化成二进制流///
///</summary>
///<paramname="image"></param>
///<returns></returns>
public byte[] ImageToByteArray(Image image)
{
//实例化流
System.IO.MemoryStream imageStream = new System.IO.MemoryStream();
//将图片的实例保存到流中
image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
//保存流的二进制数组
byte[] imageContent = new Byte[imageStream.Length];
imageStream.Position = 0;
//将流泻如数组中
imageStream.Read(imageContent, 0, (int)imageStream.Length);
return imageStream.ToArray();
}
Demo:
===============================================================================
private void button1_Click(object sender, EventArgs e)
{
//读取文件路径
openFileDialog1.ShowDialog();
textBox1.Text = openFileDialog1.FileName;
}
private void button2_Click(object sender, EventArgs e)
{
//通过路径打开文件 用文件流的方式读取内容
FileStream fs = new FileStream(textBox1.Text,FileMode.Open,FileAccess.Read);
//把读取的流存到数字pic中
fs.Read(pic, 0, 60000);
//读取文件流 pic
MemoryStream ms = new MemoryStream(pic);
//从流中穿件image
Image image = Image.FromStream(ms, true);
//将image显示到pictureBox中
pictureBox1.Image = image;
}