我想把图片编写成二进制 然后存入数据库  以及从数据库中读取出来  这种代码怎么写

解决方案 »

  1.   

    很简单,cs.中
    protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.PostedFile.FileName == "")
            {
                Label1.Text = "您还没有选择图片!";
                return;
            }
            else
            {
                string filepath = FileUpload1.PostedFile.FileName;
                string filename=filepath.Substring(filepath.LastIndexOf("\\")+1);
                string fileEx = filepath.Substring(filepath.LastIndexOf(".") + 1);
                string serverpath = Server.MapPath("File/") + filename;
                if (fileEx == "jpg" || fileEx == "bmp" || fileEx == "gif"  )
                {                FileUpload1.PostedFile.SaveAs(serverpath);
                    Image1.ImageUrl = "File/" + filename;
                    Label1.Text = "上传成功!";
                }
          
                   
                    else
                    {
                        Label1.Text = "上传的图片扩展名错误!";
                    }        }
    前台:<td style="width: 100px; height: 30px">
                                    <asp:Label ID="Label3" runat="server" Font-Size="9pt" Text="选择图片"></asp:Label></td>
                                <td align="left" style="width: 100px; height: 30px">
                                    <asp:FileUpload ID="FileUpload1" runat="server" Font-Size="9pt" /></td>
                            </tr>
                            <tr>
                                <td style="width: 100px; height: 30px">
                                </td>
                                <td align="left" style="width: 100px; height: 30px">
                                    <asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="确定"
                                        Width="80px" /></td>
                            </tr>
                            <tr>
                                <td style="width: 100px; height: 30px">
                                </td>
                                <td align="left" style="width: 100px; height: 30px">
                                    <asp:Label ID="Label2" runat="server" Font-Bold="True" Font-Size="9pt" ForeColor="Red"
                                        Text="*只允许上传.bpm/.jpg/.gif类型的图片!" Width="243px"></asp:Label></td>
                            </tr>
                            <tr>
                                <td style="width: 100px; height: 30px">
                                </td>
                                <td align="left" style="width: 100px; height: 30px">
                                    <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="9pt" ForeColor="Red"></asp:Label></td>
                            </tr>
                            <tr>
                                <td style="width: 100px; height: 30px">
                                </td>
                                <td align="left" style="width: 100px; height: 30px">
                                    <asp:Image ID="Image1" runat="server" /></td>
      

  2.   

    public void UpFile(string Action,string Id,byte[] bytes)
    {
    string UpdateSql=string.Empty;
    if(Action!='')
    { UpdateSql=string.Format("update {0} set {1} =@p1 where {2}='{3}'",
    Business.Data.CTableJiBenYangLaoBaoXian.DBName,//0
    Business.Data.JiBenYangLaoBaoXianRow.PJiBenYangLaoBaoXian_BaoXianImage,//1
    Business.Data.JiBenYangLaoBaoXianRow.PJiBenYangLaoBaoXian_ID,//2
    Id//3
    );
    }
    IDBManager dbm = (IDBManager)Serplate.InterfaceManager.InterfaceProvider.GetInterface(typeof(IDBManager));
    ICmdAccess cmd = dbm.GetMainCmdAccess();
    cmd.Open();
    try
    {
    DBParamStruct[] paras = new DBParamStruct[]{new DBParamStruct("@p1",bytes)};
    dbm.ExecuteNonQuery(new BaseDBCommandStruct(System.Data.CommandType.Text,UpdateSql,paras));
    }
    catch(Exception ex)
    {
    throw(ex);
    }
    finally
    {
    cmd.Close();
    }
    }
      

  3.   

     static void Main(string[] args)
            {
                //char a = '';
                //byte  b = (byte)a;
                //Console.WriteLine(b);
                //byte b=009090;
                //string  a=(string) b;
                //Console.WriteLine(a);
                string content="小";
                string a = "";
                int len;
                //FileStream fs = new FileStream("c:\\content.txt");
                Encoding encoding = Encoding.GetEncoding("GB2312");
                byte[] data = encoding.GetBytes(content);
                for (int i = 0; i < data.Length; i++)
                {
                    a +=data[i];
                }
                //len = content.Length;
                len = a.Length;
                Console.WriteLine(a);
            }
      

  4.   

     protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                
                string ImgPath = FileUpload1.PostedFile.FileName;
                string ImgName = ImgPath.Substring(ImgPath.LastIndexOf("\\") + 1);
                string ImgExtend = ImgPath.Substring(ImgPath.LastIndexOf(".") + 1);
                if (!(ImgExtend == "bmp" || ImgExtend == "jpg" || ImgExtend == "gif"))
                {
                    Label3.Text = "上传图片的格式不正确!";
                    return;
                }
                int FileLen = this.FileUpload1.PostedFile.ContentLength;
                Byte[] FileData = new Byte[FileLen];
                HttpPostedFile hp = FileUpload1.PostedFile;//创建访问客户端上传文件的对象 
                Stream sr = hp.InputStream;//创建数据流对象
                sr.Read(FileData, 0, FileLen);//将图片数据放到FileData数组对象实例中,其中0代表数组指针的起始位置,FileLen表示要读取流的长度(指针的结素位置)
                SqlConnection con = new SqlConnection("server=(local);user id=sa;pwd=;database=db_07");
                con.Open();
                SqlCommand com = new SqlCommand("INSERT INTO tb_15 (name) VALUES (@imgdata)", con);
                com.Parameters.Add("@imgdata", SqlDbType.Image);
                com.Parameters["@imgdata"].Value = FileData;
                com.ExecuteNonQuery();
                Label3.Text = "保存成功!";
            }
            catch (Exception error)
            {
                Label3.Text = "处理失败!原因为:" + error.ToString();
            }
        }
        
    }
      

  5.   

     
    显示:
      SqlConnection con = new SqlConnection("server=(local);user id=sa;pwd=;database=db_07");
                SqlDataAdapter ada = new SqlDataAdapter("select * from tb_17 ", con);
                con.Open();
                DataSet ds = new DataSet();
                ada.Fill(ds);
                DropDownList1.DataSource = ds;
                DropDownList1.DataTextField = "id";
                DropDownList1.DataValueField = "id";
                DropDownList1.DataBind();
                Image1.Visible=false;
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Image1.Visible = true;
            SqlConnection con = new SqlConnection("server=(local);user id=sa;pwd=sa;database=db_07");
            string imagename = "";
            try
            {
                con.Open();
                SqlCommand com = new SqlCommand("select name from tb_17 where id="+DropDownList1.Text+"", con);
                SqlDataReader dr = com.ExecuteReader();
                dr.Read();
                MemoryStream ms = new MemoryStream((Byte[])dr["name"]);
                Bitmap image = new Bitmap(ms);
                string filepath = Server.MapPath("Files/");
                DirectoryInfo dir = new DirectoryInfo(filepath);
                FileInfo[] filecount = dir.GetFiles();
                int i = filecount.Length;
                imagename = filepath + ((i + 1) + ".jpg");
                image.Save(imagename);
                dr.Close();
                Image1.ImageUrl = "Files/" + ((i + 1) + ".jpg");
            }
            finally
            {
                con.Close();
            }