public List<byte> ImageToByte(string titleStr)
公共函数,
返回值list数组,
参数 字符型,图片标题.
OpenFileDialog ofd = new OpenFileDialog();
定义文件打开窗体ofd;
此类可用于检查文件是否存在并打开它,
ofd.Title = titleStr;
将标题赋值给ofd
ofd.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP|*.PNG|*.PNG";
文件过滤器设置
List<byte> lsB = new List<byte>();
新建返回值序列
if (ofd.ShowDialog() == DialogResult.OK)
判断是否成功打开
string fullpath = ofd.FileName;//文件路径
获取文件路径
FileStream fs = new FileStream(fullpath,FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
创建文件流读取缓存,
byte[] imagebytes = new byte[fs.Length];
创建byte数组存储数据
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
lsB.AddRange(imagebytes);
加入返回值.
文件读取流程
文件名----OpenFileDialog----选择文件获得文件路径
----FileStream文件流读取----BinaryReader读取字节数据.