保存图片的方法有很多,保存路径名是最简单的也是用的最多的!
如果用数据库保存,数据库会很大的,而且还不方便操作!
我有sql保存图片的代码!

解决方案 »

  1.   

    上传图片:.aspx文件
    <asp:label id="Message" Text="选择文件和文件名字:" runat="server" />
    <hr>
    <b>文件名字:</b><input id="MyFileName" type="text" runat="server" NAME="MyFileName">
    <b>文件:</b><input id="MyFile" type="file" runat="server" NAME="MyFile">
    <br>
    <br>
    <asp:Button ID="Submit" Runat="server" Text="开始上传"></asp:Button>aspx.cs文件:protected System.Web.UI.HtmlControls.HtmlInputText MyFileName;
    protected System.Web.UI.HtmlControls.HtmlInputFile MyFile;
    protected System.Web.UI.WebControls.Button Submit;
    protected System.Web.UI.WebControls.Label Message;

    private void Submit_Click(object sender, System.EventArgs e)
    {
    //得到提交的文件
    Stream fileDataStream = MyFile.PostedFile.InputStream; //得到文件大小
    int fileLength = MyFile.PostedFile.ContentLength; //创建数组
    byte[] fileData = new byte[fileLength]; //把文件流填充到数组
    fileDataStream.Read(fileData,0,fileLength); //得到文件名字
    string fileTitle = MyFileName.Value; //得到文件类型
    string fileType = MyFile.PostedFile.ContentType; //构建数据库连接,SQL语句,创建参数
    SqlConnection connection = new SqlConnection("Server=127.0.0.1;uid=sa;pwd=20030922;Database=image");
    SqlCommand command = new SqlCommand("INSERT INTO image (title,image,datetime,type)" + 
    "VALUES (@MyFileName,@MyFile,@FileDate,@FileType)", connection); SqlParameter paramTitle = new SqlParameter("@MyFileName", SqlDbType.VarChar,50); 
    paramTitle.Value = fileTitle;
    command.Parameters.Add(paramTitle); SqlParameter paramData = new SqlParameter("@MyFile", SqlDbType.Image);
    paramData.Value = fileData;
    command.Parameters.Add(paramData); SqlParameter paramDate = new SqlParameter("@FileDate", SqlDbType.SmallDateTime); 
    paramDate.Value = DateTime.Now;
    command.Parameters.Add(paramDate); SqlParameter paramType = new SqlParameter ("@FileType", SqlDbType.VarChar,50); 
    paramType.Value = fileType;
    command.Parameters.Add(paramType); //打开连接,执行查询
    connection.Open();
    command.ExecuteNonQuery();
    connection.Close(); Message.Text="你的文件已经成功上载";
    MyFileName.Value = "";
    }
      

  2.   

    显示图片:aspx.cs文件
    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    string id=Request.Params["id"];
    string sql="SELECT * FROM image  WHERE id ='"+id+"'";
    SqlConnection connection = new SqlConnection("Server=127.0.0.1;uid=sa;pwd=20030922;Database=image");
    SqlCommand command = new SqlCommand(sql, connection);
    connection.Open();
    SqlDataReader dr = command.ExecuteReader();
    if(dr.Read())
    {
    Response.Clear();
    Response.AddHeader("Content-Type",dr["type"].ToString());
    Response.BinaryWrite((byte[])dr["image"]);
    }
    dr.Close();
    connection.Close();
    }
      

  3.   

    加入图片可以像这段代码一样的,你先把图片转化为memerystream:Private Sub SaveStream()
            Dim str As String
            Dim arr() As Byte        str = "system"
            arr = System.Text.Encoding.ASCII.GetBytes(str)        Dim conn As New OleDbConnection("Jet OLEDB:Engine Type=5;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\test.mdb")
            conn.Open()
            Dim para As New OleDbParameter("@content", OleDbType.Binary)
            para.Value = arr        Dim cmd As New OleDbCommand        Try            With cmd
                    .CommandText = "insert into tbl(content) values (@content)"
                    .Connection = conn
                    .Parameters.Add(para)
                    .ExecuteNonQuery()
                End With
                conn.Close()
                conn.Dispose()
                conn = Nothing            MsgBox("保存成功!")        Catch ex As Exception
                MsgBox(ex.Message)
            End Try    End Sub
      

  4.   

    我设计表时设计了图片字段,但是不会输入图片
    **************************************
    我来回答点简单的:
    在设计视图完成后->打开表->选中图象字段->在插入菜单中选择对象->可以新建对象也可以浏览指定的文件作为对象->确定后图象就保存到数据库中了。
    然后你可以依据楼上的方法测试以下能否调用祝你成功