zoukankan      html  css  js  c++  java
  • 如何把图片转换成二进制存入数据库

    public static byte[] imgBytesIn;//用来存储图片的二进制
    Stream ms;
    byte[] picbyte;
    //在创建数据库链接,测试链接成功后,在高级里可自动生成链接数据库字符串,copy出来即可
    string str = "Data Source=PC-20180AIHL;Initial Catalog=Image;User ID=sa";
    OpenFileDialog openF = new OpenFileDialog();

    //获取用户打开的路径然转换成二进制存入数据库
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";

    if (ofd.ShowDialog() == DialogResult.OK)
    {
    string filePath = ofd.FileName;//图片路径
    FileStream fs = new FileStream(filePath, FileMode.Open);
    byte[] imageBytes = new byte[fs.Length];
    BinaryReader br = new BinaryReader(fs);
    imageBytes = br.ReadBytes(Convert.ToInt32(fs.Length));//图片转换成二进制流

    string strSql = string.Format("insert into image(tupian) values('" + imageBytes + "')");
    int count = Write(strSql, imageBytes);

    if (count > 0)
    {
    MessageBox.Show("成功!");
    }
    else
    {
    MessageBox.Show("失败!");
    }
    }

    读出:

    byte[] imagebytes = null;

    //打开数据库

    SqlConnection con = new SqlConnection("server=PC-2018AIHL;uid=sa;pwd=123456;database=Image");

    con.Open();

    SqlCommand com = new SqlCommand("select * from image where ID=1", con);

    SqlDataReader dr = com.ExecuteReader();

    byte[] dd = null;
    string id = null;
    while (dr.Read())
    {
    dd = (byte[])dr["tupian"];
    id = dr["ID"].ToString();
    imagebytes = (byte[])dr.GetValue(1);

    }

    dr.Close();

    com.Clone();

    con.Close();

    MemoryStream ms = new MemoryStream(imagebytes);

    Bitmap bmpt = new Bitmap(ms);

    pictureBox1.Image = bmpt;

  • 相关阅读:
    CSS浮动(float、clear)通俗讲解
    JAVA 类的加载
    数据库操作 delete和truncate的区别
    正则表达式 匹配相同数字
    Oracle EBS OM 取消订单
    Oracle EBS OM 取消订单行
    Oracle EBS OM 已存在的OM订单增加物料
    Oracle EBS OM 创建订单
    Oracle EBS INV 创建物料搬运单头
    Oracle EBS INV 创建物料搬运单
  • 原文地址:https://www.cnblogs.com/jasonch123/p/8598095.html
Copyright © 2011-2022 走看看