asp.net 二进制数据写入及读出以 图片方式展现

前端时间做视频智能监控,第三方自动拍照时保存至数据库,以二进制存入。 为了团队更方便后续关于此功能的开发。

在此做下简易的总结:
1. 数据库里  用 varbinary(MAX)  , PerHomeLogo
2. 写入到数据库时,如下文示例代码
3.  读取时,如果winform就更简单了。连路径都不用转换了. 


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class imgToDb : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {//写入数据库

        string PerHomeName = tbPerHomeName.Text;
        string PerHomeSign = txtPerSign.Text;
        string imaPath = uploadFile.PostedFile.FileName;
        string lastName = imaPath.Substring(imaPath.LastIndexOf(".") + 1);

        if (uploadFile.PostedFile.FileName != "" && lastName.ToLower() == "jpg" || lastName.ToLower() == "gif" || lastName.ToLower() == "bmp" || lastName.ToLower() == "png")
        {
            if (uploadFile.PostedFile.ContentLength > 40960000)
            {
                Response.Write("");
                return;
            }
            int imaLength = uploadFile.PostedFile.ContentLength;
            Byte[] imageData = new Byte[imaLength];
            HttpPostedFile hp = uploadFile.PostedFile;
            Stream imageStream = hp.InputStream;
            imageStream.Read(imageData, 0, imaLength);

           

            //===============
            string sql = "insert into PerHomeDetail(PerHomeName,PerHomeSign,PerHomeLogo)  values(@PerHomeName,@PerHomeSign,@PerHomeLogo); select @@identity ;";
            int idx = Convert.ToInt32(SqlHelper.ExecuteScalar(CommandType.Text, sql
               , new SqlParameter("@PerHomeName", PerHomeName)
               , new SqlParameter("@PerHomeSign", PerHomeSign)
               , new SqlParameter("@PerHomeLogo", imageData)
                ));
            Response.Write("");


        }
        else
        {
            Response.Write("");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {//读取
        DBEntity.PerHomeDetail ent = new DBEntity.PerHomeDetail();
        ent = ent.Get("1");

        byte[] bytes = (byte[])ent.PerHomeLogo;
        MemoryStream ms = new MemoryStream(bytes);

        System.Drawing.Bitmap img = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromStream(ms, false); //转换成Bitmap 

        //图片路径
        string strPath = "~/upload/wangwu.JPG";
        string strPhotoPath = Server.MapPath(strPath);
        //保存图片文件
        BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));
        bw.Write(bytes);
        bw.Close();

        this.Image1.ImageUrl = strPath;
    }
}


前端页面

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="imgToDb.aspx.cs" Inherits="imgToDb" %>





    无标题页
    


    
 个性空间名 :      
       
个性头像:    
 个性签名:     
 
   

小实体类


public partial class PerHomeDetail

		 public System.Int32? idx { get; set; } 	//
		 public System.String PerHomeName { get; set; } 	//
		 public System.String PerHomeSign { get; set; } 	//
		 public System.Object PerHomeLogo { get; set; } 	//



 
  
 
 

你可能感兴趣的:(.Net,2.0)