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

推荐订阅源

Y
Y Combinator Blog
Jina AI
Jina AI
雷峰网
雷峰网
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
美团技术团队
V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
博客园 - Franky
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
爱范儿
爱范儿
Hugging Face - Blog
Hugging Face - Blog
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
Apple Machine Learning Research
Apple Machine Learning Research
量子位
IT之家
IT之家
人人都是产品经理
人人都是产品经理
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - 风语战士

asp.net 命名规范 CSS 色彩值... 请问 CType和DirectCast函数有何区别 soundex RadMenu菜单的父节指定image - 风语战士 - 博客园 vs2005 web.config大全 javascript中showModalDialog和showModelessDialog的使用 设为主页 - 风语战士 - 博客园 CSS样式表 截取字付串 表达是做为参数验证用 DataList翻页 .net安全防止写不规范字付 引用一个falsh使它显示出来 删除成功后.添加前.可判断 弹出另一页面 (转)VS2005(c#)项目调试问题解决方案集锦 常用正则表达式 在ASP.NET中使用Session常见问题集锦
从数据库中存取二进制数据并显示 - 风语战士 - 博客园
风语战士 · 2008-03-06 · via 博客园 - 风语战士

以前有讲过如何把图片信息以二进制的方式存储在sql数据库中,(详情请看:http://www.yxsoft.net.cn/article.asp?id=22)今天我们来看看,

如果把以前存储的图片读取出来,并显示:
代码如下:
SqlConnection con = new SqlConnection("server=(local);user id=sa;pwd=;database=db");
        string imagename = "";
        try
        {
            con.Open();
            SqlCommand com = new SqlCommand("select name from tb where id=1", con);
            SqlDataReader dr = com.ExecuteReader();
            dr.Read();
            MemoryStream ms = new MemoryStream((Byte[])dr["name"]);
            Bitmap image = new Bitmap(ms);
            string filepath = Server.MapPath("Files/");
            DirectoryInfo dir = new DirectoryInfo(filepath);
            FileInfo[] filecount = dir.GetFiles();
            int i = filecount.Length;
            imagename = filepath + ((i + 1) + ".jpg");
            image.Save(imagename);
            dr.Close();
            Image1.ImageUrl = "Files/" + ((i + 1) + ".jpg");//把图片显示在image1控件上
        }
        finally
        {
            con.Close();
        }

说明:
MemoryStream类:用于创建其支持存储区为内存的流.
Bitmap类的save方法:将image保存到指定的文件或流.