ASP.NET中让图片以二进制的形式存储在数据库中

http://www.niunan.org/show.php?id=276

 

生成规定大小的图片(缩略图生成)

 

http://niunan.org/show.php?id=272

using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.SessionState;   //一般处理程序不能用session解决办法(1)
namespace Picture  (要改的地方1)
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Handler1(要改的地方2):IHttpHandler, IReadOnlySessionState   //一般处理程序不能用session解决办法(2)
    {

        public void ProcessRequest(HttpContext context)
        {
            MemoryStream stream = new MemoryStream();
            SqlConnection conn = new
            SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=picture;Integrated Security=True");
            conn.Open();
            string sql = "select   [content]   from   picture where ID=@id";
            SqlCommand cmd = new SqlCommand(sql, conn);
            string id = context.Session["picid"].ToString();  //一般处理程序不能用session解决办法(3)
            SqlParameter[] parmas = new SqlParameter[] { new SqlParameter("@id", id) };
            cmd.Parameters.AddRange(parmas);
            SqlDataReader reader = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(reader);
            byte[] pic = (byte[])dt.Rows[0]["Content"];
            stream.Write(pic, 0, pic.Length);
            Bitmap bitmap = new Bitmap(stream);
            context.Response.ContentType = "image/jpeg";
            bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            conn.Close();
            stream.Close();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
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;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;

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

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int len = FileUpload1.PostedFile.ContentLength;  //获得图片的大小
            byte[] pic = new byte[len];  //创建一个与图片大小一致的二进制数组
            FileUpload1.PostedFile.InputStream.Read(pic, 0, len);  //把图片读到二进制数组中
            SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=picture;Integrated Security=True");
            conn.Open();
            string sql = "insert into picture ([Name],[Content]) values (@name,@content)";
            SqlCommand cmd = new SqlCommand(sql,conn);
            SqlParameter[] parmas = new SqlParameter[] { new SqlParameter("@name", TextBox1.Text), new SqlParameter("@content", pic) };
            cmd.Parameters.AddRange(parmas);
            int res = cmd.ExecuteNonQuery();
            if (res!=0)
            {
                Label1.Text = "上传成功";
                TextBox1.Text = "";
            }
            conn.Close();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Image1.ImageUrl = "Handler1.ashx?t=" + DateTime.Now.Ticks;
            string id = TextBox2.Text.Trim();
            Session["picid"] = id;
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值