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

 

你可能感兴趣的:(.net,Web,asp.net,asp,LINQ)