Example #1
0
        private void BtnUploadAvatar_Click(object sender, EventArgs e)
        {
            if (TxtID.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请先输入账户名", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (OpenAvatar.ShowDialog() == DialogResult.OK)
            {
                PicAvatar.Image = Image.FromFile(OpenAvatar.FileName);
                string ext = Pic.GetExtension(PicAvatar.Image);

                Directory.CreateDirectory(Environment.CurrentDirectory + "\\UsersAvatars");

                //将保存的图片的位置暂存, 在下面保存到User里面
                strAvatarFileName = Environment.CurrentDirectory + "\\UsersAvatars\\" +
                                    TxtID.Text.Trim() + ext;

                PicAvatar.Image.Save(strAvatarFileName);
            }
        }
        private void BtnLogin_Click(object sender, EventArgs e)
        {
            bool       isRet  = false;
            string     strID  = string.Empty;
            string     strPwd = string.Empty;
            DataSet    ds     = new DataSet();
            SQLExecute excute = new SQLExecute();

            strID  = TxtID.Text.Trim();
            strPwd = TxtPwd.Text.Trim();

            ds = excute.UserLogin(strID);

            isRet = PasswordStorage.VerifyPassword(strPwd, ds.Tables[0].Rows[0][0].ToString());

            if (isRet)
            {
                MessageBox.Show("登录成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //FormMain FrmM = new FormMain();
                //FrmM.LblShowUserName.Text = "欢迎" + TxtID.Text.Trim();
                LblChange.ChangeLbl("欢迎您: " + TxtID.Text.Trim());
                //发现这边要是用show方法的话, main窗体show不出来, 就尝试在另一个线程里运行
                //new System.Threading.Thread(() =>
                //    {
                //        Application.Run(FrmM);
                //    }).Start();

                ds.Clear();
                ds = excute.GetUserAvatarFileName(TxtID.Text.Trim());
                Image image = null;

                //从保存用户头像的文件夹加载头像, 如果没有就从数据库拉取, 并将其以用户名+格式的形式保存
                try
                {
                    image = Image.FromFile(ds.Tables[0].Rows[0][0].ToString());
                }
                catch (Exception)
                {
                    byte[] bytes = null;

                    ds    = excute.GetUserAvatar(TxtID.Text.Trim());
                    bytes = (byte[])ds.Tables["temp"].Rows[0][0];

                    MemoryStream ms = new MemoryStream(bytes);
                    image = Image.FromStream(ms);

                    string ext = Pic.GetExtension(image);
                    //如果没有此目录的话, 就创建
                    Directory.CreateDirectory(Environment.CurrentDirectory + "\\UsersAvatars");
                    string strFileName = Environment.CurrentDirectory + "\\UsersAvatars\\" + TxtID.Text.Trim() + ext;
                    image.Save(strFileName);
                    //并将此目录更新到数据库, 以便下次从本地加载头像
                    excute.UpdateAvatarFileName(TxtID.Text.Trim(), strFileName);
                }

                ImageChange.ImageChange(image);

                Close();
            }
            else
            {
                MessageBox.Show("登录失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }