<span style="font-size:18px;color:#cc0000;">
// OpenFileDialog:打开文件对话框。OpenFileDialog dlg = new OpenFileDialog();dlg.Multiselect = true; // true:允许选择多个文件;false:只能选择一个文件dlg.Title = "请选择文件:";dlg.Filter = "(*.jpg;*.bmp;*.gif;*.png)|*.jpg;*.bmp;*.gif;*.png|(*.*)|*.*";if (dlg.ShowDialog() == DialogResult.OK){ for (int i = 0; i < dlg.FileNames.Length; i++) // 多个文件名的集合数组 { using (FileStream fs = new FileStream(dlg.FileNames[i].ToString(), FileMode.Open)) { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, (int)fs.Length); // 将图片转换成字节数组 string strBase64 = Convert.ToBase64String(buffer);//将字节数组转换成base64 byte[] photo = Convert.FromBase64String(strBase64);//将base64转换成字节数组 using (FileStream fs2 = new FileStream("test.jpg", FileMode.Create)) { fs2.Write(photo, 0, photo.Length); // 将字节数据写入照片 } } }}
</span>