C# Winform上传图片

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);
   }
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值