C# Winform上传图片
//选择照片
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = false;//该值确定是否可以选择多个文件
dialog.Title = "请选择文件夹"; //窗体标题
dialog.Filter = "图片文件(*.jpg,*.png)|*.jpg;*.png"; //文件筛选
//默认路径设置为我的电脑文件夹
dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
file= dialog.FileName;//文件夹路径
path = file.Substring(file.LastIndexOf("\\") + 1); //格式化处理,提取文件名
//File.Copy(file, Application.StartupPath + "\\cai\\" + lujin, false); //复制到的目录切记要加文件名!!!
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; //使图像拉伸或收缩,以适合PictureBox
this.PictureBox1.ImageLocation = file;
//图片转为base64编码的字符串
Bitmap bmp = new Bitmap(file);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int) ms.Length);
ms.Close();
string base64string = Convert.ToBase64String(arr);
MessageBox.Show(base64string);
}