向数据库中添加图片,显示图片

在窗体上显示从数据库中读出的图片:

b=(byte[])ds.Tables[0].Rows[position-1]["CustomerPhoto"];

MemoryStream stream = new MemoryStream(b, true);
    stream.Write(b, 0, b.Length);
    System.Drawing.Bitmap bitmap=new Bitmap(stream);
    this.pictureBox1.Image=bitmap; 

将图片读入数据库:

openFileDialog1.Filter="*.jpg;*.bmp;*.gif|*.jpg;*.bmp;*.gif";
   if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
   {
    this.pictureBox1.Image=new Bitmap(this.openFileDialog1.FileName);
    
    FileStream fs=File.OpenRead(this.openFileDialog1.FileName);
    content=new byte[fs.Length];
    fs.Read(content, 0,content.Length);
    fs.Close();
   }
   else
   {
   
    FileStream fs=File.OpenRead("images//no.gif");
    content=new byte[fs.Length];
    fs.Read(content, 0,content.Length);
    fs.Close();
   }

将content读入数据库即可。

asp.net 上传照片:

在image.aspx的Page_Load中添加如下代码:

if(FileUpload1.HasFile==false)
        {
            Jscript.MessageBox(this.Page,"请选择要上传得图片!");
            return;
        }
        if (FileUpload1.PostedFile.ContentType != "image/pjpeg" && FileUpload1.PostedFile.ContentType != "image/gif")
        {
            Jscript.MessageBox(this.Page, "请上传gif|jpg类型的图片文件!");
            return;
        }
        int len = this.FileUpload1.PostedFile.ContentLength;
        //len = 1000000;
        byte[] buf = new byte[len];
        Stream i = this.FileUpload1.PostedFile.InputStream;
        i.Read(buf, 0, buf.Length);
       
        SqlConnection Con = new SqlConnection(objbase.connectionstring);
        String SqlCmd = "update form_my set zp=@Image,zpflag='1' where id='"+Request.QueryString["id"].Trim()+"'";
       
        SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
        CmdObj.Parameters.Add("@Image",SqlDbType.Binary, len).Value = buf;
      
    
        Con.Open();
        if (CmdObj.ExecuteNonQuery() > 0)
        {
            if (Request.QueryString["action"].Trim() == "xg")
            {
                String SqlCmd2 = "update czjl set zpx=@Image where id='" + Session["jlid"].ToString().Trim() + "'";
                SqlCommand CmdObj2 = new SqlCommand(SqlCmd2, Con);
                CmdObj2.Parameters.Add("@Image", SqlDbType.Binary, len).Value = buf;
                CmdObj2.ExecuteNonQuery();
                Session["num"] = "2";
                Response.Write("<script>location.href='shangchuanzp.aspx?id=" + Request.QueryString["id"].Trim() + "'&action=" + Request.QueryString["action"].Trim() + "</script>");
            }
        }
        else
        {
            Jscript.MessageBox(this.Page,"上传失败,请重试!");
        }
        Con.Close();      

asp.net 显示图片:

string id = Request.QueryString["id"].Trim();
       string sql = "select zp,zpflag from form_my where id='" + id + "'";
       try
       {
           DataSet ds = new DataSet();
           ds = objbase.ExecSelect(sql, "form_my");
           if (ds.Tables[0].Rows.Count > 0)
           {
               try
               {
                   if (Session["num"].ToString() == "1")
                   {
                       int len = 1000000;
                       byte[] buf = new byte[len];
                       buf = (byte[])ds.Tables[0].Rows[0]["zp"];
                       SqlConnection Con = new SqlConnection(objbase.connectionstring);
                       String SqlCmd = "update czjl set zpy=@Image,zpx=@Image2 where id='" + Session["jlid"].ToString().Trim() + "'";
                       SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
                       CmdObj.Parameters.Add("@Image", SqlDbType.Binary, len).Value = buf;
                       CmdObj.Parameters.Add("@Image2", SqlDbType.Binary, len).Value = buf;

                       Con.Open();
                       CmdObj.ExecuteNonQuery();
                       Con.Close();
                       Session["num"] = "2";
                   }
               }
               catch
               {
               }

               Response.Clear();
               Response.ContentType = "image/*";
               Response.BinaryWrite((byte[])ds.Tables[0].Rows[0]["zp"]);
              
           }       
       }
       catch
       {
       }
       Response.End();

显示图片的image控件的url指向该页,如:

this.Image1.ImageUrl = "image.aspx?id=" + Request.QueryString["id"].Trim() + "";

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章