zoukankan      html  css  js  c++  java
  • 二进制方式存储图片(数据库)

    将图片以二进制格式存储到数据库中

    核心技术:
    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表示要读取流的长度(指针的结素位置).....

    1.前台
    <table cellpadding="0" cellspacing="0" style=" 290px">
                            <tr>
                                <td colspan="2" style="height: 30px">
                                    <asp:Label ID="Label4" runat="server" BackColor="#C0FFFF" Font-Size="9pt" ForeColor="Black"
                                        Text="注意:只允许保存bmp、jpg和gif类型的图片"></asp:Label></td>
                            </tr>
                            <tr>
                                <td style=" 66px; height: 30px">
                                    <asp:Label ID="Label1" runat="server" Font-Size="9pt" Text="选择图片"></asp:Label></td>
                                <td align="left" style=" 100px; height: 30px">
                                    <asp:FileUpload ID="FileUpload1" runat="server" Font-Size="9pt" /></td>
                            </tr>
                            <tr>
                                <td style=" 66px; height: 30px">
                                </td>
                                <td align="left" style=" 100px; height: 30px">
                                    <asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="保存"
                                        Width="66px" /></td>
                            </tr>
                            <tr>
                                <td style=" 66px; height: 30px">
                                </td>
                                <td align="left" style=" 100px; height: 30px">
                                    <asp:Label ID="Label3" runat="server" Font-Size="9pt" Width="216px"></asp:Label></td>
                            </tr>
                        </table>

    2.后台

    using System.IO;
    using System.Data.SqlClient;
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        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();
            }
        }
       
    }

    读取并显示数据库中二进制图片
    核心技术:
    using System.Data.SqlClient;
    using System.IO;
    using System.Drawing;
    .....
                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);

    1.前台
    <table cellpadding="0" cellspacing="0" style=" 214px">
                            <tr>
                                <td style=" 45px; height: 30px"></td>
                                <td align="left" style=" 130px; height: 30px">
                                    <asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="读取并显示"
                                        Width="100px" /></td>
                            </tr>
                            <tr>
                                <td style=" 45px; height: 30px">
                                    <asp:Label ID="Label1" runat="server" Font-Size="9pt" Text="记录编号" Width="52px"></asp:Label></td>
                                <td align="left" style=" 130px; height: 30px">
                                    <asp:DropDownList ID="DropDownList1" runat="server" Width="100px"></asp:DropDownList></td>
                            </tr>
                            <tr>
                                <td style=" 45px; height: 120px"></td>
                                <td style=" 130px; height: 120px; text-align: left"><asp:Image ID="Image1" runat="server" /></td>
                            </tr>
                        </table>
    2.后台
    using System.Data.SqlClient;
    using System.IO;
    using System.Drawing;

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                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=;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();
            }
        }
    }

  • 相关阅读:
    MongoDB慢查询性能分析
    redis的LRU算法(二)
    Skynet服务热点火焰图分析
    内存爆灯
    时区问题
    与机器共生
    bug狩猎
    Lesson Learned
    下划线引起的血案
    Intel的CPU漏洞:Spectre
  • 原文地址:https://www.cnblogs.com/astar/p/967990.html
Copyright © 2011-2022 走看看