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

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
K
Kaspersky official blog
A
Arctic Wolf
Attack and Defense Labs
Attack and Defense Labs
L
LINUX DO - 热门话题
N
News | PayPal Newsroom
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
Lohrmann on Cybersecurity
PCI Perspectives
PCI Perspectives
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
W
WeLiveSecurity
Know Your Adversary
Know Your Adversary
博客园 - Franky
T
Tenable Blog
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Help Net Security
Help Net Security
WordPress大学
WordPress大学
T
The Exploit Database - CXSecurity.com
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
S
Security Affairs
J
Java Code Geeks
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Apple Machine Learning Research
Apple Machine Learning Research
NISL@THU
NISL@THU
O
OpenAI News
The Cloudflare Blog
月光博客
月光博客
Google Online Security Blog
Google Online Security Blog
V
V2EX
罗磊的独立博客
美团技术团队
博客园 - 三生石上(FineUI控件)
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyberwarzone
Cyberwarzone
L
LINUX DO - 最新话题
Hacker News - Newest:
Hacker News - Newest: "LLM"
大猫的无限游戏
大猫的无限游戏

博客园 - 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;