C#程序中将图片转换为byte数组,并将byte数组转换为图片

/// <summary>
/// 将图片以二进制流
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public byte[] SaveImage(String path)
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); //将图片以文件流的形式进行保存
BinaryReader br = new BinaryReader(fs);
byte[] imgBytesIn = br.ReadBytes((int)fs.Length); //将流读入到字节数组中
return imgBytesIn;
}

/// <summary>
/// 现实二进制流代表的图片
/// </summary>
/// <param name="imgBytesIn"></param>
public void ShowImgByByte(byte[] imgBytesIn)
{
string NewImageName = "AAAA";//ImageName(CenterId);//获得图片的名字
string ImagePath = @"F:/AQPXImageURL/" + NewImageName.ToString() + ".jpg";

MemoryStream ms = new MemoryStream(imgBytesIn);

Bitmap bmp = new Bitmap(ms);
bmp.Save(ImagePath, ImageFormat.Bmp);
ms.Close();
//return NewImageName;

//pictureBox1.Image = Image.FromStream(ms);
}

/// <summary>
/// 这是用于测试
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btn_jituan_Click(object sender, EventArgs e)
{
byte[] bys = SaveImage("E:/LYX/SafeTrainAll_AQPX/SafeTrainAll_AQPX/DefaultModule/Exam/ExamImage/2015-08-10#2/00000052x1.jpg");
ShowImgByByte(bys);
}

上一篇:java泛型学习(1)


下一篇:百度地图之UI控制