惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
Simon Willison's Weblog
Simon Willison's Weblog
H
Help Net Security
F
Fortinet All Blogs
H
Heimdal Security Blog
S
Schneier on Security
L
LangChain Blog
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
NISL@THU
NISL@THU
P
Palo Alto Networks Blog
J
Java Code Geeks
博客园 - 【当耐特】
The Last Watchdog
The Last Watchdog
W
WeLiveSecurity
www.infosecurity-magazine.com
www.infosecurity-magazine.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
I
InfoQ
Recorded Future
Recorded Future
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
CERT Recently Published Vulnerability Notes
T
Tenable Blog
腾讯CDC
C
Check Point Blog
量子位
M
MIT News - Artificial intelligence
GbyAI
GbyAI
罗磊的独立博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
B
Blog
小众软件
小众软件
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
V2EX - 技术
V2EX - 技术
T
Threatpost
Engineering at Meta
Engineering at Meta
Attack and Defense Labs
Attack and Defense Labs
T
Tailwind CSS Blog
S
Securelist
The Cloudflare Blog
博客园 - 叶小钗
L
LINUX DO - 最新话题
T
Troy Hunt's Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
爱范儿
爱范儿

博客园 - KiddLee

三层体系结构总结(六) Spring.Core IoC(一) Spring.Net 模块组成 Generating user instances in Sql Server is disabled. Use sp_configure 'user instances enabled' to generate user instances 面向对象分析设计学习与探索(六):好的设计=软件的灵活程度(good design=flexible software)继续 面向对象分析设计学习与探索(六):面向对象的灾难(OO Catastrophe) 面向对象分析设计学习与探索(六):好的设计=软件的灵活程度(good design=flexible software) 面向对象分析设计学习与探索(五):分析(Analysis) 面向对象分析设计学习与探索(四):需求变化(Requirements Change) 面向对象分析设计学习与探索(三):收集需求(Gathering Requirement) 面向对象分析设计学习与探索(二):好的应用程序设计(Well-designed apps rock) 面向对象分析设计学习与探索(一):开篇 类型构造器 装箱拆箱续 Session Cookie and Persistent Cookie 第一次执行方法 我犯错了 装箱拆箱 接口函数还可以声明为private
数据库存储图像及使用Image控件显示
KiddLee · 2008-07-08 · via 博客园 - KiddLee
 

在数据库中存取并显示到页面Image控件的功能。原来认为并不复杂,但是实际中却遇到了一些问题。

首先来看看保存图片:

图片在数据库中使用二进制方式存储,所以要把图片转化为二进制流文件在存储到数据库中,代码如下:

    private void Save()

    {

        MyImage img = new MyImage();

        img.Img = System.Drawing.Image.FromStream(this.FileUpload1.PostedFile.InputStream);

        System.IO.MemoryStream ms = new MemoryStream();

        img.Img.Save(ms, img.Img.RawFormat);

        SqlParameter[] paras = new SqlParameter[1];

        paras[0] = new SqlParameter("@img", SqlDbType.Image, 2147483647);

        paras[0].Value = ms.ToArray();

        sql.DBOption("Insert Into T_img (img) Values (@img)",paras);

}

再来看看读取文件:

    SqlDataAdapter adapter = new SqlDataAdapter("Select * From T_img Where imgID = " + Request.QueryString["imgID"].ToString(), sql.SqlConn);

    DataSet Ds = new DataSet();

    adapter.Fill(Ds);

    if (Ds.Tables[0].Rows.Count > 0)

    {

        img.ImgID = Convert.ToInt32(Ds.Tables[0].Rows[0]["imgID"]);

        Byte[] bytes = (Byte[])Ds.Tables[0].Rows[0]["img"];

        Response.BinaryWrite(bytes);

    }

最后要将其在Image控件中显示:

在网上看了很多资料都是使用Response.BinaryWrite(bytes),但是这只能重写页面,造成页面上只显示图片,但是在实际的项目中我们有可能是要显示在某一个特定的位置,或者说在System.Web.UI.WebControls.Image控件中显示。一开始我有一个误区,想将System.Drawing.Image对象放入WebControls.Image控件中,实际上是不能直接转换的。网上很多人提出了建立一个页面只显示图像,然后把这个页面的地址传给Image1.ImageUrl。这样做显然可以成功,但是有一点我不太满意,有一个多余的页面,此时可以使用HttpHandler来解决。

Web.config文件中加入一个HttpHandler

<system.web>

<httpHandlers>

<addverb="*"path="imgPage1.aspx"type="imgPageHandler"/>

</httpHandlers>

</system.web>

App_Code里加入一个imgPageHandler类型,并使他继承自IHttpHandler,并重写IsReusable属性和ProcessRequest方法,代码如下:

    public bool IsReusable

     {

         get { return false; }

     }

     public void ProcessRequest(HttpContext context)

     {

         MyImage img = new MyImage();

         try

         {

             sql.DBConnect();

             SqlDataAdapter adapter = new SqlDataAdapter("Select * From T_img Where imgID = " + context.Request.QueryString["imgID"].ToString(), sql.SqlConn);

             DataSet Ds = new DataSet();

             adapter.Fill(Ds);

             if (Ds.Tables[0].Rows.Count > 0)

             {

                 img.ImgID = Convert.ToInt32(Ds.Tables[0].Rows[0]["imgID"]);

                 Byte[] bytes = (Byte[])Ds.Tables[0].Rows[0]["img"];

                 context.Response.BinaryWrite(bytes);

             }

         }

         catch (Exception ex)

         {

             throw ex;

         }

         finally

         {

             sql.DBClose();

         }

     }

在要显示图像的页面中将虚拟出的页面地址给Image1.ImageUrl

       this.Image1.ImageUrl = "imgPage1.aspx?imgID=" + this.TextBox1.Text;