掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
隨著互聯(lián)網(wǎng)的快速發(fā)展,圖片在網(wǎng)站中的使用也逐漸增多,特別是在電商、在線教育等領(lǐng)域,大量的圖片給網(wǎng)站帶來(lái)了更好的用戶體驗(yàn)和更強(qiáng)的視覺(jué)效果。然而,網(wǎng)站中的圖片都是以二進(jìn)制大塊存儲(chǔ)在數(shù)據(jù)庫(kù)中,如果使用不當(dāng),就會(huì)嚴(yán)重影響網(wǎng)站的性能。本文將介紹如何,從而提高網(wǎng)站的性能和用戶體驗(yàn)。
一、ASP.NET讀取數(shù)據(jù)庫(kù)中的圖片的方法
ASP.NET讀取數(shù)據(jù)庫(kù)中的圖片有兩種方法:一種是在程序中將圖片的二進(jìn)制數(shù)據(jù)讀取出來(lái),然后將其轉(zhuǎn)換為圖片格式顯示在網(wǎng)頁(yè)上;另一種是直接將圖片的完整路徑存儲(chǔ)在數(shù)據(jù)庫(kù)中,然后在網(wǎng)頁(yè)上通過(guò)該路徑加載圖片。這兩種方法各有優(yōu)缺點(diǎn)。
二、將圖片二進(jìn)制數(shù)據(jù)讀取出來(lái)顯示在網(wǎng)頁(yè)上
ASP.NET讀取數(shù)據(jù)庫(kù)中的圖片,首先需要從數(shù)據(jù)庫(kù)中將圖片的二進(jìn)制數(shù)據(jù)讀取出來(lái)。假設(shè)數(shù)據(jù)庫(kù)中圖片的數(shù)據(jù)類(lèi)型為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ù)之后,接下來(lái)需要將其轉(zhuǎn)換為圖片格式,然后將其顯示在網(wǎng)頁(yè)上。
“`C#
System.IO.MemoryStream ms = new System.IO.MemoryStream(data, false);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
“`
以上代碼使用System.Drawing.Image類(lèi)將二進(jìn)制數(shù)據(jù)轉(zhuǎn)換為圖片,然后將其顯示在網(wǎng)頁(yè)上。
“`C#
Response.ContentType = “image/JPEG”;
image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
“`
以上代碼指定網(wǎng)頁(yè)的ContentType為圖像類(lèi)型,然后將以JPEG格式保存的圖片輸出到網(wǎng)頁(yè)中。
三、將圖片的路徑存儲(chǔ)在數(shù)據(jù)庫(kù)中
ASP.NET讀取數(shù)據(jù)庫(kù)中的圖片,另一種方法是將圖片的完整路徑存儲(chǔ)在數(shù)據(jù)庫(kù)中,然后在網(wǎng)頁(yè)上通過(guò)該路徑加載圖片。這種方法的好處是可以減少數(shù)據(jù)庫(kù)中的存儲(chǔ)量和數(shù)據(jù)傳輸量,但可能會(huì)帶來(lái)一定的安全隱患。
如果將圖片的完整路徑存儲(chǔ)在數(shù)據(jù)庫(kù)中,讀取圖片的代碼如下:
“`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語(yǔ)句從數(shù)據(jù)庫(kù)中讀取圖片的完整路徑,然后將其轉(zhuǎn)換為HTML代碼,最后通過(guò)Response.Write方法輸出到網(wǎng)頁(yè)上。
四、如何提高ASP.NET讀取數(shù)據(jù)庫(kù)中的圖片的效率
ASP.NET讀取數(shù)據(jù)庫(kù)中的圖片,有以下幾個(gè)方面可以提高其效率:
1. 設(shè)置緩存。設(shè)置緩存可以減少對(duì)數(shù)據(jù)庫(kù)的讀寫(xiě)次數(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;
}
}
“`
以上代碼在頁(yè)面加載時(shí)通過(guò)Response.Buffer、Response.Expires和Response.CacheControl等屬性設(shè)置緩存,從而提高讀取效率。
2. 圖片縮放。在讀取圖片時(shí),將其按照一定比例縮放,可以減少圖片的大小和數(shù)據(jù)傳輸量,提高網(wǎng)站的性能和用戶體驗(yàn)。
3. 圖片壓縮。在將圖片存儲(chǔ)到數(shù)據(jù)庫(kù)時(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ù)庫(kù)中的圖片需要注意以下幾點(diǎn):
1. 選擇合適的存儲(chǔ)方式。將圖片的二進(jìn)制數(shù)據(jù)存儲(chǔ)在數(shù)據(jù)庫(kù)中,或?qū)⑵渫暾窂酱鎯?chǔ)在數(shù)據(jù)庫(kù)中,各有優(yōu)缺點(diǎn),需要根據(jù)實(shí)際情況選擇。
2. 設(shè)置緩存。設(shè)置緩存可以減少對(duì)數(shù)據(jù)庫(kù)的讀寫(xiě)次數(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)問(wèn)題拓展閱讀:
存文件路徑(文件名)到數(shù)據(jù)庫(kù),圖片存到磁盤(pán)上,如果直接存儲(chǔ)圖片不但浪費(fèi)數(shù)據(jù)庫(kù)空間,更不易操作
1、建所需數(shù)據(jù)庫(kù)和表,語(yǔ)句如下:
–建立察尺數(shù)據(jù)庫(kù)
create database test
–使用該數(shù)據(jù)庫(kù)
use test
–建立存放圖片的表
create table piclist(
id int Identity primary key,
pic Image not null
)
2、制作上傳圖片的模塊,代碼如下:
前臺(tái)html代碼:
無(wú)標(biāo)題頁(yè)
后臺(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ù)庫(kù)
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ú)顯示圖片,即沒(méi)用到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ù)庫(kù)
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 頁(yè)面,名稱(chēng)為piclist.aspx和StreamImg.aspx。
piclist.aspx前臺(tái)代碼為:
無(wú)標(biāo)題頁(yè)
成都服務(wù)器托管租用、綿陽(yáng)服務(wù)器租用托管、重慶服務(wù)器托管租用、貴陽(yáng)服務(wù)器機(jī)房服務(wù)器托管租用。

我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流