当前位置: 移动技术网 > IT编程>开发语言>.net > 图片二进制转换与存入数据库相关

图片二进制转换与存入数据库相关

2019年01月23日  | 移动技术网IT编程  | 我要评论

sleepfx,大道修真录,山东结婚风俗

关于转换问题,刚开始我需要从数据库读出一个二进制数据流,并将其转换成一个image格式。

在不涉及数据库的情况下,我先将一个图片转换成一个二进制数组显示出来,再写一个方法将其转换成图片image格式。

一、 先不涉及数据库,将图片转换成二进制,在将二进制转换成图片。

1.protected void button1_click(object sender, eventargs e)
  {
  string str = null;
  picturetobinary ptb = new picturetobinary();
  // str = convert.tobase64string( ptb.data("e:/workspace/asp/test/pictureturntobinary/pictureturntobinary/img/tulips.jpg"));

  image newimage = image.fromfile(server.mappath("tulips.jpg"));
  str =convert.tobase64string( ptb.photoimageinsert(newimage));
  label1.text = str;
//label可以用response代替(response.write(str);)

 

  }

 

 

第一步,我将图片转换成二进制数组,并转换成string格式,用一个label展现(也可以用response直接输出到页面显示)。产生的数组并不是二进制构成的数据流而是一串字节,如下:

所以建议先将图片压缩一下,不然会很大。

第二步,将得到的二进制字节码转换为图片格式。

2. protected void button2_click(object sender, eventargs e)
   {
     picturetobinary ptb = new picturetobinary();
     image newimage = image.fromfile(server.mappath("tulips.jpg"));
     writephoto(ptb.photoimageinsert(newimage));
   }
//图片输出到页面
 public void writephoto(byte[] streambyte)
   {
     response.contenttype = "image/jpeg";
     response.binarywrite(streambyte);
  }
 public class picturetobinary
  {

    public picturetobinary()
    {
      //
      // todo: 在此处添加构造函数逻辑
      //

   }

public byte[] photoimageinsert(system.drawing.image imgphoto)
  {
  //将image转换成二进制流数据

  memorystream mstream = new memorystream();
  imgphoto.save(mstream, system.drawing.imaging.imageformat.bmp);
  byte[] mydata = new byte[mstream.length];
    mstream.position = 0;
  mstream.read(mydata, 0, mydata.length);
    mstream.close();
  return mydata;
  }
}

 


 

结果如下:

 

 二、将得到的二进制数据保存到数据库,再将其读出。

 需要用到这么几个控件:一个fileload控件,两个按钮,分别是存入和读取。

存入图片到数据库:

protected void saveintodatabase_click(object sender, eventargs e)
{
  //将图片保存到数据库
  //加载文件,并以字节数组格式存入数据库
  httppostedfile loadphoto = fileupload1.postedfile;
  //获取记载图片内容长度
  int photolength = loadphoto.contentlength;
  //存为字节数组
  byte[] photoarray = new byte[photolength];
  stream photostream = loadphoto.inputstream;
  photostream.read(photoarray, 0, photolength);
  //连接数据库读取文件
  sqlconnection conn = new sqlconnection();
  conn.connectionstring = "data source=localhost;database=test;user id=sa;pwd=123456789";
  string sql = "insert into test_picture values(@image,3)";
  sqlcommand cmd = new sqlcommand(sql,conn);
  cmd.commandtype = system.data.commandtype.text;
  cmd.parameters.add("@image",sqldbtype.image);
  cmd.parameters["@image"].value = photoarray;
  conn.open();
  //执行sql,成功执行返回1,否则返回0(insert,update,delete),其他命令返回-1
  int row = cmd.executenonquery();
  response.write(row);
  conn.close();
}

 

读取文件:

protected void photoshowinwebsite_click(object sender, eventargs e)
{
  //读取数据库图片文件,并保存到本地。
  sqlconnection conn = new sqlconnection();
  conn.connectionstring = "data source=localhost;database=test;user id=sa;pwd=123456789";

  //选择你需要的字段值,这边就直接赋值了
  string sql = "select picture from test_picture where number = 3";
  sqlcommand cmd = new sqlcommand(sql, conn);
  byte[] mydata = new byte[0]; 
  try
  {
    conn.open();
    sqldatareader mysqldatareader;
    mysqldatareader = cmd.executereader(commandbehavior.closeconnection);
    if (mysqldatareader.read())
    {
      response.clear();

      response.contenttype = "image/jpeg";
      response.binarywrite((byte[])mysqldatareader["picture"]);

      /*将图片写入到本地d盘      

      //图片字节流

      mydata = (byte[])mysqldatareader["picture"];

      int arraysize = mydata.getupperbound(0);
      filestream fs = new filestream(@"d:\02.jpg", filemode.openorcreate, fileaccess.write);
      fs.write(mydata, 0, arraysize);
      fs.close();

      */

    }
    
  } catch (sqlexception sqlexc)
  {
    response.write(sqlexc.tostring());
  }

    conn.close();
}

 

 

以上为整理的为图片二进制转换与存取数据库相关的内容。

 

新人报道,请多指教!

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网