怎么用二进制读取图片

解决方案 »

  1.   

    一般是將圖片轉化為二進制存入數據庫
    讀取的時候再將二進制的轉化為圖片。
    如果是如此 再給你model
      

  2.   

    Image I, C; MemoryStream MS; byte[] B;
    I.Save(MS, I.RawFormat);
    B = MS.ToArray();
    C = Image.FromStream(new MemoryStream(B));一些控件有CreateGraphics()方法,使用其DrawImage方法可把Image在控件中显示出来。 
      

  3.   

     //保存事件private void btnSave_Click(object sender, System.EventArgs e)     {      string filename = this.txtPicture.Text.ToString();      System.Drawing.Imaging.ImageFormat iformat = null;      switch(filename.Substring(filename.IndexOf('.')+1).ToLower())      {       case "bmp":        iformat=System.Drawing.Imaging.ImageFormat.Bmp;        break;       case "gif":        iformat=System.Drawing.Imaging.ImageFormat.Gif;        break;       case "jpg":        iformat=System.Drawing.Imaging.ImageFormat.Jpeg;        break;      }      MemoryStream ms = new MemoryStream();      Image imgae=Image.FromFile(filename);      imgae.Save(ms,iformat);      string parm = Path.GetFileNameWithoutExtension(filename);      this.dbaccess.InsertImage(parm,ms);     }    //打开图片事件   private void btnOpen_Click(object sender, System.EventArgs e)     {      string strsql = this.txtSeach.Text.ToString();      DataSet ds = this.dbaccess.GetPhotosDataSet(strsql);      if ((ds.Tables[0].Rows.Count-1)<0)       MessageBox.Show("数据表内没有图片存在!");      else      {       this.picbImage.Image =        this.dbaccess.StreamToImage((byte[])ds.Tables[0].Rows[0][2]);      }     }   //以上代码均为www.it560.com原创首发.//插入图片  public int InsertImage(string filename,MemoryStream ms)     {      try      {       ConnectionDB();                     this.comm = new SqlCommand("sp_ImageTable",this.conn);       this.comm.CommandType = CommandType.StoredProcedure;       this.comm.Parameters.Add(new SqlParameter("@name", SqlDbType.VarChar));       this.comm.Parameters.Add(new SqlParameter("@image", SqlDbType.Image));       this.comm.Parameters[0].Value = filename;       this.comm.Parameters[1].Value = ms.ToArray();       this.comm.ExecuteNonQuery();       System.Windows.Forms.MessageBox.Show("图片 " + filename + " 保存成功");      }      catch(Exception ce)      {       System.Windows.Forms.MessageBox.Show(ce.Message);      }      finally      {       conn.Close();      }      return 1;     }   public Image StreamToImage(byte[] b)     {      MemoryStream ms = new MemoryStream(b);      return Image.FromStream(ms);     }    
    文章来自: IT560编程资讯(www.it560.com) 详文参考:http://www.it560.com/html/program/.net/20100204/2613.html
      

  4.   

    如何从ACCESS数据库中读取images 
    1。ACCESS和FoxPro数据库中的图形格式 
    当浏览器遇到一个<IMG>标志时,它会根据你设定的src属性来下载文件。 
    这可能是一个图形文件或则是一个ASP页面。 
    如果是一个返回gif二进制的ASP页面 
    浏览器需要知道返回的是什么格式的图形文件 
    因为这个原因,就需要指定content type,为image/gif,image/bmp 
    image/jpeg或则其他什么的。 
    Response.contentType = "image/gif" 
    但这会导致另外一个问题,那就是我们只能够显示gif格式的图象, 
    或则说保存在数据库中的东西只能够是gif格式的了。 
    但是一些数据库是使用gif格式保存的,但是另外一些则是使用 
    jpeg格式保存的,甚至其他是采用OLE方式来保存图形的。 
    所以我们必须根据图形的格式来设置response的content type. 
    注意的是你也可以从一个文件中新建一个位图对象,但使用这样 
    的方式保存在数据库中的图形格式是浏览器不能够识别的。 
    当你往数据库中保存图象时,你应该知道你需要使用什么格式来保存 
    你可以把文件中的每一个字节保存下来,或则通过ACCESS/Foxpro的把图形保存 
    为一个OLE格式。 
    你使用什么格式保存图象决定了你在ASP中用什么格式来读出图形来。 
    具体来说,如果你在ACCESS/FoxPro中将图形保存为bmp,gif,jpeg( 
    这个必须要使用到ACCESS/FoxPro的OLE对象,即使用ACCESS的插入对象 
    对话框来完成),这是当你使用 
    image/bmp时浏览器是不能够解释的。 
    现在假设在数据库中保存的是你所想要的图形格式 
    (GIF, JPEG, BMP, TIFF, 等)现在来看看要怎么把它们从 
    数据库中读出来。 
    在ACCESS中使用了两个关键的技术来保存图形 
    1。使用了bmp格式 
    2。78个字节的文件头 
    <% 
    response.Expires = 0 
    response.Buffer = True 
    response.Clear 
    response.contentType = "image/bmp" 
    %> 
    接着你要干的就是去掉那78个字节的OLE对象的文件头。 
    <% 
    Const OLEHEADERSIZE = 78 
    nFieldSize = rs("photo").ActualSize 
    oleHeader = rs("photo").GetChunk(OLEHEADERSIZE) 
    imageBytes = rs("photo").GetChunk(nFieldSize - OLEHEADERSIZE) 
    Response.BinaryWrite imageBytes 
    %> 
    现在举一个例子: 
    如果你要得到一个职工的信息,这段信息包括一个介绍和他的图象。 
    并且要同时显示文字和图形。 
    代码如下:(其中的theImg是一个代理页面) 
    theImg.asp 
    <% 
    response.Expires = 0 
    response.Buffer = True 
    response.Clear 
    response.contentType = Session("ImageType") 
    response.BinaryWrite Session("ImageBytes") 
    Session("ImageType") = "" 
    Session("ImageBytes") = "" 
    response.End 
    %> 
    Function SetImageForDisplay(field, contentType) 
    OLEHEADERSIZE = 78 
    contentType = LCase(contentType) 
    select case contentType 
    case "gif", "jpeg", "bmp" 
    contentType = "image/" & contentType 
    bytes = field.value 
    case "ole" 
    contentType = "image/bmp" 
    nFieldSize = field.ActualSize 
    oleHeader = field.GetChunk(OLEHEADERSIZE) 
    bytes = field.GetChunk(nFieldSize - OLEHEADERSIZE) 
    end select 
    Session("imageBytes") = bytes 
    Session("imageType") = contentType 
    End Function 
    '注意的是,程序中只使用了4中格式:gif, jpeg, bmp , ole . 
    <% 
    sql = "select * from Employees" 
    Set oRS = Server.CreateObject("ADODB.Recordset") 
    oRS.CursorLocation = 3 
    oRS.Open sql, "DSN=NW" 
    SetImageForDisplay oRS("photo"), "ole" 
    Set oRS.ActiveConnection = Nothing 
    %> 
    要显示图象的话,只需要在另外一个asp中,假设为getEmpInfo.asp中 
    <img src="theImg.asp"</img> 
    但这还有一个问题,因为对每个职工的图形都使用了同一个"theImg.asp" 
    文件,应该再小小修改一下: 
    <img src="theImg.asp?temp=<%= Request.Form("empLastName")%>"</img> 
    最后再说一点,如何显示多幅图象呢? 
    也就是说如果数据库中有多个字段都保存了图形,怎么办? 
    其实解决办法很简单,只要给SetImageForDisplay多加一个参数 
    就是用来保存图形的一个session变量。 
    例如: 
    SetImageForDisplay oRS1("photo"), "ole", "empPhoto" 
    SetImageForDisplay oRS2("logo"), "gif", "compLogo" 
    <img src="theImg2.asp?varName=empPhoto&temp=<%= Request.Form("empLastName")%>"> 
    <img src="theImg2.asp?varName=compLogo&temp=<%= Request.Form("imgCode")%>"> 
    使用这个方法能够完成下面的功能: 
    1。能够从数据库中取出图形字段。(你唯一需要知道的是数据库中的图形是什么格式 
    bmp?gif?jpeg?ole?) 
    2.采用session变量 来保存图形的字节数和content type 
    asp需要这些信息来联结到<IMG>中的属性 
    3。只要把theImg放到你想显示图形的地方,就能够显示图象了