(C#)WPF保存图片,将图片提交到服务端进行保存

来源:互联网 发布:马来西亚华人知乎 编辑:程序博客网 时间:2024/04/30 01:10
1、服务端代码
 private string DocumentFlowSwitchPath(byte[][] byteTuPian)
    {
        try
        {
            //按照年月日时分秒组成一个字符串
            string strFilePrefix = DateTime.Now.Year.ToString() +
                                  DateTime.Now.Month.ToString() +
                                  DateTime.Now.Day.ToString() + 
                                  DateTime.Now.Hour.ToString() +
                                  DateTime.Now.Minute.ToString() + 
                                  DateTime.Now.Second.ToString() +
                                  DateTime.Now.Millisecond.ToString();
            //图片名称变量
            string strFileName = "";
            for (int i = 0; i < byteTuPian.Length; i++)//遍历二进制的数组的数组
            {
                string strRiQiWenJian = strFilePrefix + i.ToString() + ".png";
                //获取根目录磁盘路径
                string strBaoCunLuJing = System.AppDomain.CurrentDomain.BaseDirectory;
                strBaoCunLuJing = strBaoCunLuJing + "image\\" + strRiQiWenJian;
                FileInfo fi = new System.IO.FileInfo(strBaoCunLuJing);
                FileStream fs;
                fs = fi.OpenWrite();
                fs.Write(byteTuPian[i], 0, byteTuPian[i].Length);
                fs.Close();
                strFileName += strRiQiWenJian;
            }
            return strFileName;
        }
        catch
        {
            return null;
        }
    }  
2、页面设计
 
 
3、。cs界面代码
 //声明一个byte[]类型的集合
  List<byte[]> lstBytes1 = new List<byte[]>();

 //点击保存进行员工信息的保存
    private void btnUpdate_Click(object sender, RoutedEventArgs e)
    {
            byte[][] HeadPhoto = new byte[lstBytes1.Count][];
            for (int i = 0; i < lstBytes1.Count; i++)
            {
                HeadPhoto[i] = lstBytes1[i];
            }               
            DataTable dt = bll_Emloyee.DocumentFlowSwitchPath(HeadPhoto).Tables[0];        
       
    }  


    //点击选择头像按钮事件,单开文件
    private void btn_openFile_Click(object sender, RoutedEventArgs e)
    {
        Stream checkStream = null;
        Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
        openFileDialog.Multiselect = false;
        openFileDialog.Filter = "All Image Files | *.*";
        if ((bool)openFileDialog.ShowDialog())
        {
            lstBytes1.Clear();
            if ((checkStream = openFileDialog.OpenFile()) != null)
            {
                int Length = (int)checkStream.Length;
                byte[] bytes = new byte[Length];  //二进制文件存放的二进制数组
                checkStream.Read(bytes, 0, Length);
                lstBytes1.Add(bytes);
                scrolls.Source = new BitmapImage(new Uri(openFileDialog.FileName, UriKind.Absolute)).Clone();                    }
        }
    }  
原创粉丝点击