首页 新闻 会员 周边

asp.net 图片存储与读取,使用MS SQL SERVER

0
悬赏园豆:10 [已解决问题] 解决于 2011-04-25 11:25

需求:
1.页面上使用摄像头拍照,然后保存用户头像保存二进制到数据库。
2.读入用户头像数据显示到页面上。

----

第一点倒没什么问题,请问当得到一个二进制图像数据,怎么显示到页面上呢?

注:不要把二进制转码保存到硬盘再显示。

YangBayker的主页 YangBayker | 初学一级 | 园豆:95
提问于:2011-04-24 15:25
< >
分享
最佳答案
0

1)右键网站新建,选择 generic handler,就是扩展名为ashx的那个.

2)

<%@ WebHandler Language="C#" class="ImageFetch" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.IO;

public class ImageFetch : IHttpHandler
{
    const int BUFFERSIZE = 1024;

    public bool IsReusable
    {
        get
        {
            return true;
        }
    }

    public void ProcessRequest(HttpContext context)
    {
        HttpResponse response = context.Response;
        HttpRequest request = context.Request;
        response.ContentType = "image/jpeg";
        response.Cache.SetCacheability(HttpCacheability.Public);
        response.BufferOutput = false;
       //这里取出你存在数据库里的图片字段 自己的数据访问请替换下面的

string cxnstr = System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString;
SqlConnection connection = new SqlConnection(cxnstr);
string query = "select largeimage from images where id=@id";
SqlCommand command = new SqlCommand(query, connection);
SqlParameter param0 = new SqlParameter("@id", SqlDbType.Int);
param0.Value = request.QueryString["ImageID"];
command.Parameters.Add(param0);
connection.Open();

Stream output = response.OutputStream;

byte[] d = ((byte[])(command.ExecuteScalar()));
output.Write(d, 0, d.Length);
connection.Close();

        response.End();
    }


}

代码大致如此.

3)前台调用:

 avatarImage.ImageUrl = "AvatarImagefetch.ashx?id=" + user.id.ToString();

如有不明白,欢迎进QQ群讨论,QQ群:149385300.

收获园豆:10
DYStudio.Net | 小虾三级 |园豆:1747 | 2011-04-24 18:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册