zoukankan      html  css  js  c++  java
  • 把图片 保存成字符流 到数据库里

    后台代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using DM;
    using BLL;

    namespace UI
    {
        public partial class AddUserImageInfo : Form
        {
            UserImageInfoBLL bll = new UserImageInfoBLL();
            public AddUserImageInfo()
            {
                InitializeComponent();
            }

            private void btnAdd_Click(object sender, EventArgs e)
            {
                UserImageInfoDM imageinfodm = new UserImageInfoDM();
                imageinfodm.UserName = this.txtUserName.Text;
                imageinfodm.Password = this.txtPassword.Text;
                imageinfodm.Age = Convert.ToInt32(this.txtAge.Text);
                imageinfodm.Sex = rdoMan.Checked ? '男' : '女';
                imageinfodm.Email = this.txtEmail.Text;
                //创建文件流
                FileStream fs = new FileStream(this.pictureBox1.ImageLocation, FileMode.Open);
                //声明byte数组
                byte[] bytes = new byte[fs.Length];
                //读取数组
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();
                imageinfodm.Image = bytes;
                if (bll.AddUserImageInfo(imageinfodm)==1)
                {
                    MessageBox.Show("添加成功");
                    this.txtUserName.Text = "";
                    this.txtPassword.Text = "";
                    this.txtEmail.Text = "";
                    this.txtBeSure.Text = "";
                    this.txtAge.Text = "";
                    this.pictureBox1.Image = null;
                }
                else
                {
                    MessageBox.Show("添加失败");
                }
            }

            private void pictureBox1_Click(object sender, EventArgs e)
            {
                DialogResult re = this.openFileDialog1.ShowDialog();
                if (re==DialogResult.OK)
                {
                    this.pictureBox1.ImageLocation = this.openFileDialog1.FileName;//把路径赋给PictureBox
                }
            }

            private void btnShow_Click(object sender, EventArgs e)
            {
                //取出的数据时byte[]
                byte[] bytes = bll.SelectImageByUserName(this.txtUserName.Text);
                MemoryStream ms = new MemoryStream(bytes); //从内存流中读取数据
                this.pictureBox1.Image = Image.FromStream(ms);
            }
        }
    }

  • 相关阅读:
    Ex 6_20 最优二叉搜索树..._第六次作业
    Ex 6_12 凸多边形的最优三角剖分..._第六次作业
    Ex 6_9 某个字符串处理语言提供了一个将字符串一分为二的基本操作..._第六次作业
    Ex 6_4 判断序列是否由合法单词组成..._第六次作业
    maven配置阿里云镜像时(私服设置~JEECG)
    node、npm、webpack、vue-cli傻傻分不清?
    设计模式~观察者模式和发布订阅模式的比较:
    前端~定位属性position(relative、absolute、fixed)的分析
    debounce防抖函数减少函数调用的逻辑分析(包裹上时间的外衣,在时间还没来时,kill)
    js原生滚动与使用插件better-scroll不起作用原因
  • 原文地址:https://www.cnblogs.com/chenghu/p/2526516.html
Copyright © 2011-2022 走看看