用户登录
用户注册

分享至

C#实现图片文件到数据流,再到图片文件的转换

  • 作者: 爱的旋律88
  • 来源: 51数据库
  • 2021-11-16
//----引入必要的命名空间
using system.io;
using system.drawing.imaging;

//----代码部分----//

private byte[] photo;//公用缓冲区 public string sourfilepath;//源图片文件路径 public string objfilepath;//目标图片路径 public int filetostream()//文件到流的转换 { image img = new bitmap(sourfilepath); memorystream stream = new memorystream(); img.save(stream, imageformat.bmp); binaryreader br = new binaryreader(stream); photo = stream.toarray(); stream.close(); return 0; } public image showpic()//根据流显图 { byte[] bytes = photo; memorystream ms = new memorystream(bytes); ms.position = 0; image img = image.fromstream(ms); ms.close(); return img; } public int streamtofile()//反向转换 { byte[] bytes = photo; filestream fs = new filestream(objfilepath, filemode.create, fileaccess.write); fs.write(bytes, 0, bytes.length); fs.flush(); fs.close(); return 0; }
软件
前端设计
程序设计
Java相关