在学习期间,把开发过程经常用到的一些代码段做个备份,下边代码内容是关于java中如何把图片转换成二进制流的代码,应该能对各朋友也有用处。
public byte[] SetImageToByteArray(string fileName)
{ FileStream fs = new FileStream(fileName, FileMode.Open);
int streamLength = (int)fs.Length; byte[] image = new byte[streamLength];
fs.Read(image, 0, streamLength);
fs.Close();
return image; }
public byte[]
SetImageToByteArray(FileUpload FileUpload1)
{ Stream stream = FileUpload1.PostedFile.InputStream;
byte[] photo = new byte[FileUpload1.PostedFile.ContentLength];
stream.Read(photo, 0, FileUpload1.PostedFile.ContentLength);
stream.Close();
return photo;
}
2.从SQLServer数据库读取Image类型的数据,并转换成bytes[]或Image图像文件
{ Image image; MemoryStream mymemorystream = new MemoryStream(mybyte,0, mybyte.Length);
image = Image.FromStream(mymemorystream);
return image;
}