1 //图片的"读"操作 2 //①参数是图片路径:返回Byte[]类型: 3 //参数是图片的路径 4 public byte[] GetPictureData(string imagePath){ 5 FileStream fs=new FileStream(imagePath,FileMode.Open); 6 byte[] byteData=new byte[fs.Length]; 7 fs.Read(byteData,0,byteData.Length); 8 fs.Close(); 9 return byteData(); 10 } 11 //②参数类型是Image对象,返回Byte[]类型 12 //将Image转换成流数据,并保存为byte[] 13 public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto) 14 { 15 MemoryStream mstream = new MemoryStream(); 16 imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp); 17 byte[] byData = new Byte[mstream.Length]; 18 mstream.Position = 0; 19 mstream.Read(byData, 0, byData.Length); mstream.Close(); 20 return byData; 21 } 22 //图片的“写”操作 23 //①参数是Byte[]类型,返回值是Image对象 24 public System.Drawing.Image ReturnPhoto(byte[] streamByte) 25 { 26 System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte); 27 System.Drawing.Image img = System.Drawing.Image.FromStream(ms); 28 return img; 29 } 30 //②参数是Byte[] 类型,没有返回值(ASP.NET输出图片) 31 public void WritePhoto(byte[] streamByte) 32 { 33 // Response.ContentType 的默认值为默认值为“text/html” 34 Response.ContentType = "image/GIF"; 35 //图片输出的类型有: image/GIF image/JPEG 36 Response.BinaryWrite(streamByte); 37 }
开发路程(8):图片和byte[]的互相转换,布布扣,bubuko.com
原文:http://www.cnblogs.com/liubeimeng/p/3820015.html