winfrom手动选择文件完整路径
电脑版发表于:2021/12/23 11:58
前言
笔记
具体代码
// 选择路径 private string SelectPath() { string path = string.Empty; OpenFileDialog openFileDialo = new OpenFileDialog(); openFileDialo.Filter = "所有文件(*.*)|*.*"; if (openFileDialo.ShowDialog() == System.Windows.Forms.DialogResult.OK) { path = openFileDialo.FileName; } return path; } //图片转成二进制 public byte[] GetPictureData(string imagepath) { /**/ FileStream FileStream = new FileStream(imagepath, FileMode.Open); byte[] byData = new byte[FileStream.Length]; FileStream.Read(byData, 0, byData.Length); FileStream.Close(); return byData; } //二进制转图片 public Image ReturnPhoto(string imagepath) { List<byte> listbit = new List<byte>(); listbit.AddRange(GetPictureData(imagepath)); byte[] bit = listbit.ToArray(); System.IO.MemoryStream ms = new System.IO.MemoryStream(bit); System.Drawing.Image img = System.Drawing.Image.FromStream(ms); return img; }
调用就行了
string fliepath = SelectPath(); Image image = ReturnPhoto(fliepath);