怎样读取数据库中存储的二进制图片文件

怎样读取数据库中存储的二进制图片文件,第1张

下面我们将示例一个文件读取存储至数据并从数据库中读取信息并显示的案例:

1、首先读取硬盘上的某一具体文件,读取模式设置为readBinary方式:

<cffile action = "readBinary" file = "temp directory here#fileserverFile#" variable = "test">

2、将读取出来的二进制内容存储至数据库中(注:数据库字段需设置成能存储类型的字段,如blob类型):

<cfquery datasource = "datasource">

insert into imageTest values (<cfqueryparam cfsqltype="cf_sql_blob" value="#test#">)

</cfquery>

通过1、2两个步骤,我们轻松实现了读取文件并存储至数据库的 *** 作过程。

3、从数据库中读取信息,该文件可命名为dispImagecfm:

<!--- 在此需特别注意enablecfoutputonly的压缩空白功能,如果不对该页面进行空白压缩,很可能会造成无法显示的问题 --->

<cfprocessingdirective suppressWhiteSpace="yes">

<cfsetting enablecfoutputonly="yes">

<!--- 读取相应的信息 --->

<cfquery datasource = "datasource">

select image from imageTest where variable here#

</cfquery>

<!--- 设置浏览器输出的格式,我们将它设置为的JPG类型,用户可根据实际情况改动类型设置 --->

<cfcontent type="image/jpg">

<!--- 输出 --->

<cfoutput>#toString(imageTestimage)#</cfoutput>

</cfprocessingdirective>

<cfabort>

4、显示内容,调用dispImagecfm页面:

<img src = "dispImagecfmid=your variable here">

通过3、4两个步骤,我们也很容易的就完成了从数据库中读取信息并在页面显示的功能。

总结:实际上,除了文件可以如此处理,其它的文件也能通过类似方式进行处理,可将任意文件类型存储至数据库,只是文件大小的原因以及数据库存储读取速度性能限制,我们基本上还是不建议将文件存储至数据库,毕竟硬盘读取要快得多。

1将以二进制存入数据库

//保存到数据库

protected void Button1_Click(object sender, EventArgs e)

{

//路径

string strPath = "~/photo/03JPG";

string strPhotoPath = ServerMapPath(strPath);

//读取

FileStream fs = new SystemIOFileStream(strPhotoPath, FileModeOpen, FileAccessRead);

BinaryReader br = new BinaryReader(fs);

byte[] photo = brReadBytes((int)fsLength);

brClose();

fsClose();

//存入

SqlConnection myConn = new SqlConnection("Data Source=127001;Initial Catalog=TestDB;User ID=sa;Password=sa");

string strComm = " INSERT INTO personPhoto(personName, personPhotoPath, personPhoto) ";

strComm += " VALUES('wangwu', '" + strPath + "', @photoBinary )";

SqlCommand myComm = new SqlCommand(strComm, myConn);

myCommParametersAdd("@photoBinary", SqlDbTypeBinary,photoLength);

myCommParameters["@photoBinary"]Value = photo;

myConnOpen();

myCommExecuteNonQuery();

myConnClose();

}

2读取二进制在页面显示

//读取

SqlConnection myConn = new SqlConnection("Data Source=127001;Initial Catalog=TestDB;User ID=sa;Password=sa");

string strComm = " SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ";

SqlCommand myComm = new SqlCommand(strComm, myConn);

myConnOpen();

SqlDataReader dr = myCommExecuteReader();

while (drRead())

{

byte[] photo = (byte[])dr["personPhoto"];

thisResponseBinaryWrite(photo);

}

drClose();

myConnClose();

SqlConnection myConn = new SqlConnection("Data Source=127001;Initial Catalog=TestDB;User ID=sa;Password=sa");

SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='11' ", myConn);

DataSet myds = new DataSet();

myConnOpen();

mydaFill(myds);

myConnClose();

byte[] photo = (byte[])mydsTables[0]Rows[0]["personPhoto"];

thisResponseBinaryWrite(photo);

3设置Image控件显示从数据库中读出的二进制

---------------------------------------------

SqlConnection myConn = new SqlConnection("Data Source=19216801;Initial Catalog=TestDB;User ID=sa;Password=sa");

SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='11' ", myConn);

DataSet myds = new DataSet();

myConnOpen();

mydaFill(myds);

myConnClose();

byte[] photo = (byte[])mydsTables[0]Rows[0]["personPhoto"];

//路径

string strPath = "~/photo/wangwuJPG";

string strPhotoPath = ServerMapPath(strPath);

//保存文件

BinaryWriter bw = new BinaryWriter(FileOpen(strPhotoPath,FileModeOpenOrCreate));

bwWrite(photo);

bwClose();

3显示

thisImage1ImageUrl = strPath;

4GridView中ImageField以URL方式显示

--------------------------

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">

<Columns>

<asp:BoundField DataField="personName" HeaderText="姓名" />

<asp:ImageField DataImageUrlField="personPhotoPath"

HeaderText="">

</asp:ImageField>

</Columns>

</asp:GridView>

5GridView显示读出的二进制

//样板列

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">

<Columns>

<asp:BoundField DataField="personName" HeaderText="姓名" />

<asp:ImageField DataImageUrlField="personPhotoPath"

HeaderText="">

</asp:ImageField>

<asp:TemplateField HeaderText="">

<ItemTemplate>

<asp:Image ID="Image1" runat="server" />

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (eRowRowIndex < 0)

return;

// SystemComponentModelContainer

string strPersonName = (string)DataBinderEval(eRowDataItem, "personName");

Image tmp_Image = (Image)eRowCells[2]FindControl("Image1");

if (!SystemConvertIsDBNull(DataBinderEval(eRowDataItem, "personPhoto")))

{

//

byte[] photo = (byte[])DataBinderEval(eRowDataItem, "personPhoto");

//路径

string strPath = "~/photo/" + strPersonNameTrim() + "JPG";

string strPhotoPath = ServerMapPath(strPath);

//保存文件

BinaryWriter bw = new BinaryWriter(FileOpen(strPhotoPath, FileModeOpenOrCreate));

bwWrite(photo);

bwClose();

//显示

tmp_ImageImageUrl = strPath;

}

}

image类型的数据库、图像数据库处理图像类数据。

图像数据库系统由数据输入系统、数据表示与管理系统、数据检索与 *** 作系统和应用系统组成。图像数据库系统处理的对象包括图像数据、地图、图形数据、一般文本、图形等,统称为模式数据。数据输入系统从模式数据中提取计算机可以使用的信息。一个典型的例子是自动地将图输入系统。典型的应用系统包括:利用地图数据的地理信息系统、利用遥感图像的土地信息系统、利用各种绘图数据的计算机辅助设计系统、计算机动画设计系统以及其他图像处理和图案信息处理系统。

以上就是关于怎样读取数据库中存储的二进制图片文件全部的内容,包括:怎样读取数据库中存储的二进制图片文件、Sqlserver数据库存储的图片格式(二进制数据)怎么显示到页面、哪类数据库处理图像类数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存