数据库中可以存储照片么怎么存储

数据库中可以存储照片么怎么存储,第1张

数据中可以存储,但是需要注意不能直接存储,而是转换成二进制或者Base64等的“文本”来存储,在用的时候,可以再转换回来。

在网站开发中,一般将存储在文件系统中,而不是数据系统中,数据库系统中只记录在文件系统中的路径而已。

资源简介的常见存储与读取凡是有以下几种:存储:以二进制的形式存储时,要把数据库中的字段设置为Image数据类型(SQL Server),存储的数据是Byte[]1参数是路径:返回Byte[]类型: public byte[] GetPictureData(string imagepath) { //////根据文件的路径使用文件流打开,并保存为byte[] FileStream fs = new FileStream(imagepath, FileModeOpen);//可以是其他重载方法 byte[] byData = new byte[fsLength]; fsRead(byData, 0, byDataLength); fsClose(); return byData; }2参数类型是Image对象,返回Byte[]类型: public byte[] PhotoImageInsert(SystemDrawingImage imgPhoto) { //将Image转换成流数据,并保存为byte[] MemoryStream mstream = new MemoryStream(); imgPhotoSave(mstream, SystemDrawingImagingImageFormatBmp); byte[] byData = new Byte[mstreamLength]; mstreamPosition = 0; mstreamRead(byData, 0, byDataLength); mstreamClose(); return byData; }好了,这样通过上面的方法就可以把转换成Byte[]对象,然后就把这个对象保存到数据库中去就实现了把的二进制格式保存到数据库中去了。下面我就谈谈如何把数据库中的读取出来,实际上这是一个相反的过程。读取:把相应的字段转换成Byte[]即:Byte[] bt=(Byte[])XXXX1参数是Byte[]类型,返回值是Image对象: public SystemDrawingImage ReturnPhoto(byte[] streamByte) { SystemIOMemoryStream ms = new SystemIOMemoryStream(streamByte); SystemDrawingImage img = SystemDrawingImageFromStream(ms); return img; }2参数是Byte[] 类型,没有返回值,这是针对aspnet中把从输出到网页上(ResponseBinaryWrite) public void WritePhoto(byte[] streamByte) { // ResponseContentType 的默认值为默认值为“text/html” ResponseContentType = "image/GIF"; //输出的类型有: image/GIF image/JPEG ResponseBinaryWrite(streamByte); }补充:针对ResponseContentType的值,除了针对的类型外,还有其他的类型: ResponseContentType = "application/msword"; ResponseContentType = "application/x-shockwave-flash"; ResponseContentType = "application/vndms-excel";另外可以针对不同的格式,用不同的输出类型以适合不同的类型: switch (dataread("document_type")) { case "doc": ResponseContentType = "application/msword"; case "swf": ResponseContentType = "application/x-shockwave-flash"; case "xls": ResponseContentType = "application/vndms-excel"; case "gif": ResponseContentType = "image/gif"; case "Jpg": ResponseContentType = "image/jpeg"; }立即获得您的

数据库是存储数据的,不应该是存储文件的!并且存储为二进制的代码会增加查询时间,从而影响呈现时间!!

一般的做法是存储的路径,删除时也是通过这个路径去删除文件,再删除这条记录!!

public static byte[] ImageDatabytes(string FilePath)

{

if(!FileExists(FilePath))

return null;

Bitmap myBitmap = new Bitmap(ImageFromFile(FilePath));

using (MemoryStream curImageStream = new MemoryStream())

{

myBitmapSave(curImageStream, SystemDrawingImagingImageFormatPng);

curImageStreamFlush();

byte[] bmpBytes = curImageStreamToArray();

//如果转字符串的话

//string BmpStr = ConvertToBase64String(bmpBytes);

return bmpBytes ;

}

}

用上面的方法将转换成二进制,再存进数据库。

以上就是关于数据库中可以存储照片么怎么存储全部的内容,包括:数据库中可以存储照片么怎么存储、如何将图片转换成二进制存储、怎么把图片转换成二级制数据然后存到存储过程再存储到数据库里面去等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9477161.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-28
下一篇2023-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存