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

转https://www.cnblogs.com/baofengxue00/p/4720852.html

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

///


/// 将图片以二进制流
///

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


///


/// 现实二进制流代表的图片
///

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

///


/// 这是用于测试
///

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

你可能感兴趣的:(C#)