日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
利用ASP.NET實(shí)現(xiàn)高效讀取數(shù)據(jù)庫中的圖片 (asp.net讀取數(shù)據(jù)庫圖片)

隨著互聯(lián)網(wǎng)的快速發(fā)展,圖片在網(wǎng)站中的使用也逐漸增多,特別是在電商、在線教育等領(lǐng)域,大量的圖片給網(wǎng)站帶來了更好的用戶體驗(yàn)和更強(qiáng)的視覺效果。然而,網(wǎng)站中的圖片都是以二進(jìn)制大塊存儲(chǔ)在數(shù)據(jù)庫中,如果使用不當(dāng),就會(huì)嚴(yán)重影響網(wǎng)站的性能。本文將介紹如何,從而提高網(wǎng)站的性能和用戶體驗(yàn)。

一、ASP.NET讀取數(shù)據(jù)庫中的圖片的方法

ASP.NET讀取數(shù)據(jù)庫中的圖片有兩種方法:一種是在程序中將圖片的二進(jìn)制數(shù)據(jù)讀取出來,然后將其轉(zhuǎn)換為圖片格式顯示在網(wǎng)頁上;另一種是直接將圖片的完整路徑存儲(chǔ)在數(shù)據(jù)庫中,然后在網(wǎng)頁上通過該路徑加載圖片。這兩種方法各有優(yōu)缺點(diǎn)。

二、將圖片二進(jìn)制數(shù)據(jù)讀取出來顯示在網(wǎng)頁上

ASP.NET讀取數(shù)據(jù)庫中的圖片,首先需要從數(shù)據(jù)庫中將圖片的二進(jìn)制數(shù)據(jù)讀取出來。假設(shè)數(shù)據(jù)庫中圖片的數(shù)據(jù)類型為image,圖片的ID為1,代碼如下:

“`C#

byte[] data = null;

SqlCommand cmd = new SqlCommand(“SELECT IMAGE FROM IMAGES WHERE ID = 1”, conn);

using (SqlDataReader reader = cmd.ExecuteReader())

{

if (reader.Read())

{

data = (byte[])reader[“IMAGE”];

}

}

“`

讀取圖片的二進(jìn)制數(shù)據(jù)之后,接下來需要將其轉(zhuǎn)換為圖片格式,然后將其顯示在網(wǎng)頁上。

“`C#

System.IO.MemoryStream ms = new System.IO.MemoryStream(data, false);

System.Drawing.Image image = System.Drawing.Image.FromStream(ms);

“`

以上代碼使用System.Drawing.Image類將二進(jìn)制數(shù)據(jù)轉(zhuǎn)換為圖片,然后將其顯示在網(wǎng)頁上。

“`C#

Response.ContentType = “image/JPEG”;

image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

“`

以上代碼指定網(wǎng)頁的ContentType為圖像類型,然后將以JPEG格式保存的圖片輸出到網(wǎng)頁中。

三、將圖片的路徑存儲(chǔ)在數(shù)據(jù)庫中

ASP.NET讀取數(shù)據(jù)庫中的圖片,另一種方法是將圖片的完整路徑存儲(chǔ)在數(shù)據(jù)庫中,然后在網(wǎng)頁上通過該路徑加載圖片。這種方法的好處是可以減少數(shù)據(jù)庫中的存儲(chǔ)量和數(shù)據(jù)傳輸量,但可能會(huì)帶來一定的安全隱患。

如果將圖片的完整路徑存儲(chǔ)在數(shù)據(jù)庫中,讀取圖片的代碼如下:

“`C#

string imagePath = null;

SqlCommand cmd = new SqlCommand(“SELECT IMAGEPATH FROM IMAGES WHERE ID = 1”, conn);

using (SqlDataReader reader = cmd.ExecuteReader())

{

if (reader.Read())

{

imagePath = (string)reader[“IMAGEPATH”];

}

}

string imageUrl = string.Format(““, imagePath);

Response.Write(imageUrl);

“`

以上代碼使用SELECT語句從數(shù)據(jù)庫中讀取圖片的完整路徑,然后將其轉(zhuǎn)換為HTML代碼,最后通過Response.Write方法輸出到網(wǎng)頁上。

四、如何提高ASP.NET讀取數(shù)據(jù)庫中的圖片的效率

ASP.NET讀取數(shù)據(jù)庫中的圖片,有以下幾個(gè)方面可以提高其效率:

1. 設(shè)置緩存。設(shè)置緩存可以減少對(duì)數(shù)據(jù)庫的讀寫次數(shù),提高讀取效率??梢允褂肙utputCache附加器或在代碼中設(shè)置緩存。

“`C#

public partial class Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

Response.Buffer = true;

Response.ExpiresAbsolute = DateTime.Now.AddSeconds(60);

Response.Expires = 60;

Response.CacheControl = “public”;

Response.ContentType = “image/jpeg”;

string id = Request.QueryString[“id”];

byte[] data = GetImageData(id);

Response.OutputStream.Write(data, 0, data.Length);

}

private byte[] GetImageData(string id)

{

byte[] data = null;

SqlCommand cmd = new SqlCommand(“SELECT IMAGE FROM IMAGES WHERE ID = ” + id, conn);

using (SqlDataReader reader = cmd.ExecuteReader())

{

if (reader.Read())

{

data = (byte[])reader[“IMAGE”];

}

}

return data;

}

}

“`

以上代碼在頁面加載時(shí)通過Response.Buffer、Response.Expires和Response.CacheControl等屬性設(shè)置緩存,從而提高讀取效率。

2. 圖片縮放。在讀取圖片時(shí),將其按照一定比例縮放,可以減少圖片的大小和數(shù)據(jù)傳輸量,提高網(wǎng)站的性能和用戶體驗(yàn)。

3. 圖片壓縮。在將圖片存儲(chǔ)到數(shù)據(jù)庫時(shí),可以使用壓縮算法將圖片壓縮,從而減少圖片的大小和數(shù)據(jù)傳輸量,提高網(wǎng)站的性能和用戶體驗(yàn)。

4. 圖片分離。將網(wǎng)站中的靜態(tài)資源(如圖片、CSS樣式表、JavaScript文件等)單獨(dú)存儲(chǔ)在獨(dú)立的服務(wù)器上,可以減小網(wǎng)站的流量和負(fù)載,提高網(wǎng)站的性能和可靠性。

五、

本文介紹了的方法和技巧,以及提高讀取效率的方法。ASP.NET讀取數(shù)據(jù)庫中的圖片需要注意以下幾點(diǎn):

1. 選擇合適的存儲(chǔ)方式。將圖片的二進(jìn)制數(shù)據(jù)存儲(chǔ)在數(shù)據(jù)庫中,或?qū)⑵渫暾窂酱鎯?chǔ)在數(shù)據(jù)庫中,各有優(yōu)缺點(diǎn),需要根據(jù)實(shí)際情況選擇。

2. 設(shè)置緩存。設(shè)置緩存可以減少對(duì)數(shù)據(jù)庫的讀寫次數(shù),提高讀取效率。

3. 圖片縮放和壓縮。在讀取和存儲(chǔ)圖片時(shí),將其按照一定比例縮放或壓縮,可以減少圖片的大小和數(shù)據(jù)傳輸量,提高網(wǎng)站的性能和用戶體驗(yàn)。

4. 圖片分離。將網(wǎng)站中的靜態(tài)資源單獨(dú)存儲(chǔ)在獨(dú)立的服務(wù)器上,可以減小網(wǎng)站的流量和負(fù)載,提高網(wǎng)站的性能和可靠性。

相關(guān)問題拓展閱讀:

  • 高分!ASP.NET中如何把圖片以二進(jìn)制方式存入SQL Server數(shù)據(jù)庫,并能讀取出來

高分!ASP.NET中如何把圖片以二進(jìn)制方式存入SQL Server數(shù)據(jù)庫,并能讀取出來

存文件路徑(文件名)到數(shù)據(jù)庫,圖片存到磁盤上,如果直接存儲(chǔ)圖片不但浪費(fèi)數(shù)據(jù)庫空間,更不易操作

1、建所需數(shù)據(jù)庫和表,語句如下:

–建立察尺數(shù)據(jù)庫

create database test

–使用該數(shù)據(jù)庫

use test

–建立存放圖片的表

create table piclist(

id int Identity primary key,

pic Image not null

)

2、制作上傳圖片的模塊,代碼如下:

前臺(tái)html代碼:

無標(biāo)題頁

后臺(tái)代碼:

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.IO;

using System.Data.SqlClient;

public partial class Test_UpPhoto : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void btnAdd_Click(object sender, EventArgs e)

{

//獲得圖象并把圖象轉(zhuǎn)換為byte

HttpPostedFile upPhoto = UpPhoto.PostedFile;

int upPhotoLength = upPhoto.ContentLength;

byte PhotoArray = new Byte;

Stream PhotoStream = upPhoto.InputStream;

PhotoStream.Read(PhotoArray, 0, upPhotoLength);

//連接數(shù)據(jù)庫

string ConStr = “server=(local);user id=sa;pwd=sa;database=test”;

SqlConnection conn = new SqlConnection(ConStr);

string strSql = “旦迅Insert into piclist(pic) values(@pic)”;

SqlCommand cmd = new SqlCommand(strSql, conn);

cmd.Parameters.Add(“@pic”, SqlDbType.Image);

cmd.Parameters.Value = PhotoArray;

conn.Open();

cmd.ExecuteNonQuery();

conn.Close();

Response.Write(“圖片上傳成功”);

}

}

3、制作顯示圖片的模塊(單獨(dú)顯示圖片,即沒用到datalist):

后臺(tái)代碼:

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

using System.IO;

public partial class Test_ShowPhoto : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if(!Page.IsPostBack)

{

//連接數(shù)據(jù)庫

string ConnStr = “server=(local);user id=sa;pwd=sa;database=test”;

string strSql = “select * from piclist”;

SqlConnection conn = new SqlConnection(ConnStr);

conn.Open();

SqlCommand cmd=new SqlCommand(strSql,conn);

SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())

{

Response.ContentType = “application/octet-stream”;

Response.BinaryWrite((Byte)reader);

Response.Write(“successful”);

}

reader.Close();

conn.Close();

Response.End();

}

}

}

補(bǔ)充步驟3,用datalist顯示圖片方法:

建立兩個(gè)asp.net 頁面,名稱為piclist.aspx和StreamImg.aspx。

piclist.aspx前臺(tái)代碼為:

無標(biāo)題頁

  成都服務(wù)器托管租用、綿陽服務(wù)器租用托管、重慶服務(wù)器托管租用、貴陽服務(wù)器機(jī)房服務(wù)器托管租用。


分享題目:利用ASP.NET實(shí)現(xiàn)高效讀取數(shù)據(jù)庫中的圖片 (asp.net讀取數(shù)據(jù)庫圖片)
網(wǎng)頁鏈接:http://m.5511xx.com/article/dppejsp.html