C#实现图片文件到数据流,再到图片文件的转换
程序员文章站
2023-09-28 18:43:34
// 引入必要的命名空间 using System.IO; using System.Drawing.Imaging; // 代码部分 // private byte[] photo;//公用缓冲区 public string SourFilePath;//源图片文件路径 public string ......
//----引入必要的命名空间 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# 截取屏幕
下一篇: Flask-Script