数据库读取二进制图片显示到PictureBox中

1、已知路径,加载本地图片到Image中

  Image img = Image.FromFile("路径");

2、数据库中读取二进制图片

  string strSql = "Select Top 1 ImageContent From TT_ImageFileSave)";

  Byte[] byteImage = new Byte[0];

  byteImage = (Byte[])(DbHelperSQL.GetSingle(strSql));

  MemoryStream stmBLOBData = new MemoryStream(byteImage);

  picCarInfo.Image = Image.FromStream(stmBLOBData);

3、将已知路径图片转换为二进制

  string strImagePath = AppDomain.CurrentDomain.BaseDirectory + "//Tickets//Ticket.jpg";

  FileStream fileStream = new FileStream(strImagePath, FileMode.Open, FileAccess.Read);

  BinaryReader binaryReader = new BinaryReader(fileStream);

  byte[] byteImage = binaryReader.ReadBytes((int)fileStream.Length);

  binaryReader.Close(); fileStream.Close(); File.Delete(strImagePath);

  return byteImage;

4、将现有Image图片转化为二进制

    MemoryStream mStream = new MemoryStream();
  byte[] bytImagedata = null;
  imgSource.Save(mStream, System.Drawing.Imaging.ImageFormat.Jpeg);  

  bytImagedata = mStream.GetBuffer();     return bytImagedata;

 


 

posted on 2010-06-01 17:24  一路前行  阅读(3612)  评论(4编辑  收藏  举报