• 数据库读取图片展示


    将bitmap保存到页面的输出流中。在其它页面的img的src就可以使用这个流。

    img.aspx:

    protected void Page_Load(object sender, EventArgs e)
    {
    Bitmap img = null;
    string connStr = ConfigurationManager.ConnectionStrings["dbConn"].ToString();
    using (SqlConnection conn = new SqlConnection(connStr))
    {
    conn.Open();
    string sql = "SELECT IMG FROM MOVIE WHERE IMG IS NOT NULL";
    SqlCommand cmd = new SqlCommand(sql, conn);
    object obj = cmd.ExecuteScalar();
    conn.Close();
    if (obj != null)
    {
    byte[] buffer = (byte[])obj;
    MemoryStream mStream = new MemoryStream(buffer);
    img = new Bitmap(mStream);
    }
    else
    {
    img = new Bitmap(Server.MapPath("img/android.jpg"));
    }
    }
    img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    Response.End();
    }

    <img alt="" src="img.aspx" />

    上传图片到数据库(img字段为image格式):

            protected void btnUpload_Click(object sender, EventArgs e)
    {
    int fileLeng = FileUpload1.PostedFile.ContentLength;
    byte[] buffer = new byte[fileLeng];
    Stream stream = FileUpload1.FileContent;
    stream.Read(buffer, 0, fileLeng);

    string connStr = ConfigurationManager.ConnectionStrings["dbConn"].ToString();
    using (SqlConnection conn = new SqlConnection(connStr))
    {
    conn.Open();
    string sql = "UPDATE Movie SET IMG=@img WHERE IMG IS NULL";
    SqlCommand cmd = new SqlCommand(sql, conn);
    cmd.Parameters.Add("@img", SqlDbType.Image);
    cmd.Parameters["@img"].Value = buffer;
    cmd.ExecuteNonQuery();
    }
    }



  • 相关阅读:
    OpenCV+iOS开发使用文档
    Mac下OpenCV开发
    vs2010+cuda5.0+qt4.8
    对于基类添加虚析构函数问题
    PMVS学习中学习c++
    解决ubuntu上opengl的问题
    js中const,var,let区别
    phpstorm 快捷键
    Chrome 控制台console的用法
    【PHP】进一法取整、四舍五入取整、忽略小数等的取整数方法大全
  • 原文地址:https://www.cnblogs.com/xingbinggong/p/2193796.html
Copyright © 2020-2023  润新知