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

2022-10-10,,,,

//----引入必要的命名空间
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; }

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

下载本文的Word格式文档,以方便收藏与打印。