向數據庫中添加圖片,顯示圖片

在窗體上顯示從數據庫中讀出的圖片:

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() + "";

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