.Net下图片的常见存储与读取凡是有以下几种:
存储图片:以二进制的形式存储图片时,要把数据库中的字段设置为Image数据类型(SQL Server),存储的数据是Byte[].
1.参数是图片路径:返回Byte[]类型:

 public byte[] GetPictureData(string imagepath)
        {
            ////根据图片文件的路径使用文件流打开,并保存为byte[]   
            FileStream fs = new FileStream(imagepath, FileMode.Open);//可以是其他重载方法 
            byte[] byData = new byte[fs.Length];
            fs.Read(byData, 0, byData.Length);
            fs.Close();
            return byData;
        }

2.参数类型是Image对象,返回Byte[]类型:

 public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto)
        {
            //将Image转换成流数据,并保存为byte[]   
            MemoryStream mstream = new MemoryStream();
            imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] byData = new Byte[mstream.Length];
            mstream.Position = 0;
            mstream.Read(byData, 0, byData.Length);
            mstream.Close();
            return byData;
        }

好了,这样通过上面的方法就可以把图片转换成Byte[]对象,然后就把这个对象保存到数据库中去就实现了把图片的二进制格式保存到数据库中去了。下面我就谈谈如何把数据库中的图片读取出来,实际上这是一个相反的过程。
读取图片:把相应的字段转换成Byte[]即:Byte[] bt=(Byte[])XXXX
1.参数是Byte[]类型,返回值是Image对象:

 public System.Drawing.Image ReturnPhoto(byte[] streamByte)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);
            System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
            return img;
        }

2.参数是Byte[] 类型,没有返回值,这是针对asp.net中把图片从输出到网页上(Response.BinaryWrite)

 public void WritePhoto(byte[] streamByte)
        {
            // Response.ContentType 的默认值为默认值为“text/html”
            Response.ContentType = "image/GIF";
            //图片输出的类型有: image/GIF  image/JPEG
            Response.BinaryWrite(streamByte);
        }

补充:
针对Response.ContentType的值,除了针对图片的类型外,还有其他的类型:

            Response.ContentType = "application/msword";
            Response.ContentType = "application/x-shockwave-flash";
            Response.ContentType = "application/vnd.ms-excel";

另外可以针对不同的格式,用不同的输出类型以适合不同的类型:

  switch (dataread("document_type"))
            {
                case "doc":
                    Response.ContentType = "application/msword";
                case "swf":
                    Response.ContentType = "application/x-shockwave-flash";
                case "xls":
                    Response.ContentType = "application/vnd.ms-excel";
                case "gif":
                    Response.ContentType = "image/gif";
                case "Jpg":
                    Response.ContentType = "image/jpeg";
            }

一些相关的东西,可以作为参考

Image image= GetImageFromClipboard();//实现从剪切板获取图像的功能 
System.IO.MemoryStream stream = new System.IO.MemoryStream(); 
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter 
= new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); formatter.Serialize(stream, image);

FileStream fs=new FileStream("xx",FileMode.Open,FileAccess.Write); 
fs.Write(stream.ToArray(),0,stream.ToArray().Length);

.Net下二进制形式的文件存储与读取的更多相关文章

  1. Linux基础篇学习——Linux文件系统之文件存储与读取:inode,block,superblock

    Linux文件类型 代表符号 含义 - 常规文件,即file d directory,目录文件 b block device,块设备文件,支持以"block"为单位进行随机访问 c ...

  2. 【Android】14.2 外部文件存储和读取

    分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 1.基本概念 内部存储的私有可用存储空间一般都不会很大,对于容量比较大的文件,例如视频等,应该将其存储在外部存储设 ...

  3. 【Android】14.1 内部文件存储和读取

    分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 内部存储(Internal storage)是指将应用程序建立的私有文件保存在内部存储器(移动经销商卖的那种容量较 ...

  4. C#查找指定路径下的所有指定文件,并读取

    string path="指定路径"; string filename =“需要查找的文件名.csv"; List<string> lineStringLis ...

  5. 不同Hadoop模式下,Hive元数据文件存储位置

    假如在hive的配置文件hive-site.xml中,属性hive.metastore.warehouse.dir被设置为/root/hive/warehouse. 如果Hadoop是本地模式,则仓库 ...

  6. android之外部文件存储和读取

    这次借用上次读写内部存储的代码,只是对将更换文件的读写路径即可.这里需要对获取SDcard的读写权限. 一.AndroidManifest.xml 这里增加了对外部存储设备的读写权限 <?xml ...

  7. android之文件存储和读取

    一.权限问题 手机中存储空间分为ROM和SDcard,ROM中放着操作系统以及我们安装的APP,而sdcard中一般放置着我们APP产生的数据.当然,Android也为每个APP在ROM中创建一个数据 ...

  8. 搭建基于 HDFS 碎片文件存储服务

    安装 JDK HDFS 依赖 Java 环境,这里我们使用 yum 安装 JDK 8,在终端中键入如下命令: yum -y install java-1.8.0-openjdk* 使用如下命令查看下 ...

  9. VS下对Resx资源文件的操作

    原文:VS下对Resx资源文件的操作 读取 using System.IO; using System.Resources; using System.Collections; using Syste ...

随机推荐

  1. .NET Standard

    A formal specification of the APIs that are common across .NET implementations What is .NET Standard ...

  2. SpringMVC之ajax与表单 Post 数据提交差异小结

    最近在写一个富文本框的后台数据服务的时候遇到一些关于 ajax 提交与 表单提交的比较特殊的案例,这里拿来跟大家分享,希望能让大家有所启发. 1. 首先是常见表单提交在SpringMVC的控制器中的代 ...

  3. insmod内核模块时提示Failed to find the folder holding the modules怎么办?

    答:笔者通过重新编译内核和根文件系统解决了此问题 (笔者使用的是openwrt系统) 分析: 1. ’Failed to find the folder holding the modules‘这句l ...

  4. C++内容记录

    仅个人记录 https://ke.qq.com/course/336509 M了个J - 博客园 https://www.cnblogs.com/mjios/

  5. kotlin中匿名对象

    open class MyClass { private fun too()=object { var x : String ="x" } fun publictoo()=obje ...

  6. c++ for each

    #include <iostream>#include <vector>#include <list> using namespace std; int main( ...

  7. mysql学习笔记11_12(查询)

    1.建表和插入值 创建company数据库 创建 department表 create table department(d_id int(10) primary key not null uniqu ...

  8. 一个老程序员PHP程序员说的话(用来提醒自己)

    我,一个老程序员,也是一个学生,把玩过甚多语言,大多不精.我既非名牌学校,也不是高学历,仅代表一部分比较蛋疼的人.接触PHP也是很早了,从04年的OFSTAR开始的,到现在六年了,期间也接触过不少的语 ...

  9. Linux进程批量管理工具

    在使用docker容器时,可以有单机的docker-compose批量编排工具,甚至还有集群的k8s之类编排工具,那么在Linux系统中同样也有相关的批量管理进程的工具,其中使用最多的应该就是supe ...

  10. Elasticsearch 环境配置

    1.下载启动Elasticsearch Elasticsearch下载地址: https://www.elastic.co/cn/downloads/elasticsearch (2)     Run ...