当前位置: 首页 > news >正文

深圳自助网站建设费用/青岛新闻最新今日头条

深圳自助网站建设费用,青岛新闻最新今日头条,交友网站如果建设,百度网站引流怎么做实现功能如下图: 注明:此文使用的是DevExpress控件,winform 原生控件也是一样使用方法。 1.点击选择图片按钮,功能为通过对话框选择要上传的文件,并将该文件在下面的PictureEdit中显示出来。具体代码如下:…

实现功能如下图:
实现功能
注明:此文使用的是DevExpress控件,winform 原生控件也是一样使用方法。

1.点击选择图片按钮,功能为通过对话框选择要上传的文件,并将该文件在下面的PictureEdit中显示出来。具体代码如下:

private void btnChoosePic_Click(object sender, EventArgs e){ShowPic(pictureEdit1);}/// <summary> /// 选择图片 /// </summary> /// <param name="picEdit"></param> public static void ShowPic(PictureEdit picEdit) { OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = @"C:\"; ofd.Filter = "Image Files(*.JPG;*.PNG;*.jpeg;*.GIF;*.BMP)|*.JPG;*.PNG;*.GIF;*.BMP;*.jpeg|All files(*.*)|*.*"; ofd.RestoreDirectory = true; if (ofd.ShowDialog() == DialogResult.OK) { PicAddress = ofd.FileName; Image imge = Image.FromFile(PicAddress); Bitmap bm = new Bitmap(imge, picEdit.Width, picEdit.Height); picEdit.Image = bm; } }

ShowPic()方法为静态方法,可以直接调用,其中的PicAddress变量为静态全局变量,用于记录要上传文件的文件地址。PictureEdit显示图片的方式,是通过PictureEdit的image属性设定的,将图片转成Bitmap格式,位图文件是最简单的图片格式。

2.上传图片,该按钮的功能是将选定的图片上传到数据库中,具体的实现代码如下:

/// <summary>/// 上传图片 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnUploadPic_Click(object sender, EventArgs e) { if (PicAddress != null) { if (PicType.Equals("教师")) { var sqlSearch = $@"select count(*) from studentmanager.picture where PicTypeId = '{TeacherId}' and PicType='{PicType}'"; var dsSearch = _db.GetResult(sqlSearch); if (dsSearch.Tables[0].Rows[0][0].ToString().Equals("0")) //没有重复的,则进行新增插入操作 { byte[] pic = CommonFunction.GetContent(PicAddress); var result = _db.SavePictureToDB(pic, PicAddress, PicType, TeacherId); if (result > 0) { CommonFunction.MessageShow("头像添加成功", "提示", "OK", "Information"); DialogResult = DialogResult.OK; } else { CommonFunction.MessageShow("头像添加失败"); } } else { //更新头像 if (PicAddress.Equals(String.Empty)) { CommonFunction.MessageShow("没有重新选择图片进行更新"); return; } byte[] pic = CommonFunction.GetContent(PicAddress); var result = _db.UpdatePictureToDb(pic, PicAddress, PicType, TeacherId); if (result > 0) { CommonFunction.MessageShow("头像更新成功", "提示", "OK", "Information"); DialogResult = DialogResult.OK; } else { CommonFunction.MessageShow("头像更新失败"); } } } else if(PicType.Equals("学生")) { var sqlSearch = $@"select count(*) from studentmanager.picture where PicTypeId = '{StudentId}' and PicType='{PicType}'"; var dsSearch = _db.GetResult(sqlSearch); if (dsSearch.Tables[0].Rows[0][0].ToString().Equals("0")) //没有重复的,则进行新增插入操作 { byte[] pic = CommonFunction.GetContent(PicAddress); var result = _db.SavePictureToDB(pic, PicAddress, PicType, StudentId); if (result > 0) { CommonFunction.MessageShow("头像添加成功", "提示", "OK", "Information"); DialogResult = DialogResult.OK; } else { CommonFunction.MessageShow("头像添加失败"); } } else { //更新头像 if (PicAddress.Equals(String.Empty)) { CommonFunction.MessageShow("没有重新选择图片进行更新"); return; } byte[] pic = CommonFunction.GetContent(PicAddress); var result = _db.UpdatePictureToDb(pic, PicAddress, PicType, StudentId); if (result > 0) { CommonFunction.MessageShow("头像更新成功", "提示", "OK", "Information"); DialogResult = DialogResult.OK; } else { CommonFunction.MessageShow("头像更新失败"); } } } } else { CommonFunction.MessageShow("请先选择图片!", "提示", "OK", "Error"); } }

上传的过程大概就是:根据文件地址将对应文件转换成数据流二进制格式–>编写对应的SQL语句–>执行该SQL语句,将图片添加到数据库中。
上面代码中SavePictureToDB方法代码如下:

/// <summary>/// 保存图片到数据库 /// </summary> /// <param name="imageByte"></param> /// <param name="Tablename"></param> /// <param name="FieldPicturename"></param> /// <param name="FieldIdxname"></param> /// <param name="FieldIdxvalue"></param> /// <returns></returns> public int SavePictureToDB(byte[] imageByte, string Picturename, string PicType, int PicTypeId) { var result = 0; try { if (imageByte != null && imageByte.Length != 0) { using (var conn = new MySqlConnection()) { conn.ConnectionString = ConnectionString; conn.Open(); var insertStr = @"INSERT INTO studentmanager.picture ( Picturename, PicType, PicTypeId, imageByte ) VALUES ( @Picturename, @PicType, @PicTypeId, @imageByte );"; var comm = new MySqlCommand(); comm.Connection = conn; comm.CommandText = insertStr; comm.CommandType = CommandType.Text; //设置数据库字段类型MediumBlob的值为图片字节数组imageByte comm.Parameters.Add(new MySqlParameter("@imageByte", MySqlDbType.MediumBlob)).Value = imageByte; comm.Parameters.Add(new MySqlParameter("@Picturename", MySqlDbType.VarChar)).Value = Picturename; comm.Parameters.Add(new MySqlParameter("@PicType", MySqlDbType.VarChar)).Value = PicType; comm.Parameters.Add(new MySqlParameter("@PicTypeId", MySqlDbType.Int32)).Value = PicTypeId; //execute sql result = comm.ExecuteNonQuery(); comm.Dispose(); conn.Close(); conn.Dispose(); } } } catch (Exception) { // throw ex; } return result; }

3.加载图片显示到PictureEdit;

/// <summary>/// 窗口加载 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void HeadManager_Load(object sender, EventArgs e) { LoadImage(PicType, PicType.Equals("教师") ? TeacherId : StudentId); } /// <summary> /// 获取图片 /// </summary> /// <param name="picType"></param> /// <param name="picTypeid"></param> private void LoadImage(string picType, int picTypeid) { try { var imageBytes = _db.GetImage(picType, picTypeid); var image = CommonFunction.GetImageByBytes(imageBytes); Bitmap bm = new Bitmap(image, pictureEdit1.Width, pictureEdit1.Height); pictureEdit1.Image = bm; } catch (Exception) { pictureEdit1.Image = Resource.DefaultUser; } }

4.用到的公共方法:

/// <summary>/// 转换为Byte[] /// </summary> /// <param name="filepath"></param> /// <returns></returns> public static byte[] GetContent(string filepath)//将指定路径下的文件转换成二进制代码,用于传输到数据库 { FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read); byte[] byData = new byte[fs.Length];//新建用于保存文件流的字节数组 fs.Read(byData, 0, byData.Length);//读取文件流 fs.Close(); return byData; } /// <summary> /// 读取byte[]并转化为图片 /// </summary> /// <param name="bytes">byte[]</param> /// <returns>Image</returns> public static Image GetImageByBytes(byte[] bytes) { Image photo; using (MemoryStream ms = new MemoryStream(bytes)) { ms.Write(bytes, 0, bytes.Length); photo = Image.FromStream(ms, true); ms.Dispose(); ms.Close(); } return photo; }
http://www.jmfq.cn/news/4957327.html

相关文章:

  • 什么网站做前端练手好/重庆白云seo整站优化
  • 郑州网站建设工作/网络营销事件
  • 温州网站搭建公司/绍兴seo公司
  • 烟台网站设计制作公司电话/网站建设黄页免费观看
  • 苏州网站建设建网站/百度地图优化排名方法
  • 求个网站这么难吗2021年/实时疫情最新消息数据
  • 云南网站建设找天软/seo咨询师招聘
  • 建设工程主管部门网站/关键词排名查询网站
  • 做外围网站代理违法吗/有效果的网站排名
  • 网站建设运营工作业绩/标题优化方法
  • 独立做网站搭建平台/搜索引擎付费推广
  • 腾讯的网站是谁做的/百度网盘搜索引擎
  • 大华建设项目管理有限公司网站/首页排名关键词优化
  • jsp开发的网站/qq群排名优化软件
  • 企业网站建设 优化/seo基础培训机构
  • 网站建设案例渠道/世界杯积分榜排名
  • 农业大学网站建设特点/福建省人民政府
  • 怎样用word做网站/网页推广怎么做
  • 大方县住房城乡建设局网站/深圳网络营销软件
  • 企业网站整合/网络舆情监测中心
  • 汕头网站建设技术托管/4001688688人工服务
  • 二手书网站策划书/腾讯3大外包公司
  • 惠州市住房和城乡规划建设局官方网站/百度问答首页
  • 谷歌 网站开发/网上在线看视频为什么卡
  • 做网站都能用什么做/西安网站seo工作室
  • 石河子网站建设/网页怎么做
  • 学做网站的基本/seo在线教学
  • 网站建设栏目分级/百度app登录
  • 帮人做网站推选的公司/长沙seo网络优化
  • 网站推广外包公司哪家好/佛山网站seo