當前位置:首頁 » 數據倉庫 » 從資料庫中獲取圖片
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

從資料庫中獲取圖片

發布時間: 2022-09-25 14:40:37

『壹』 php中如何從資料庫中讀取圖片

<?php

//將圖片存進資料庫再讀出,注意存儲圖片的欄位類型必須為blob
$user=』root』;
$password=』root』;
$db=』test』;
$connect=mysql_connect(『localhost』,$user,$password);
mysql_set_charset(『utf8′,$connect);
mysql_select_db($db);

$photo = 「0x」.bin2hex(file_get_contents(「./test.jpg」));
$sql=」INSERT INTO `test`.`test` (`photo`) VALUES ($photo);」;//$photo不需要用引號,切記
mysql_query($sql);

//$result=mysql_query(「SELECT *
//FROM `test`
//LIMIT 0 , 30〃);
//$img=mysql_fetch_array($result);
//echo $img['photo'];
?>

『貳』 如何才能往資料庫里讀取圖片數據或者從資料庫里讀圖片能告訴我具體步驟嗎謝謝

具體步驟:
1.連接資料庫
2.查詢資料庫
3.調用資料庫中的圖片(有些是按照地址保存,有的是按照二進制保存)
在調用的地方用<img src="<%=rs("存放圖片的欄位")%>">
這樣就可以了

『叄』 怎麼從資料庫中調取照片

不知道你是jsp還是asp,給你個框架思路:
比如圖片文件夾絕對路徑為D:\IMAGE,資料庫列「照片」中存的是文件名
那在輸出時,需要將路徑與文件名拼接起來,做為html的img元素,
比如假設rs為結果集,以asp為例:
dim imgpath
imgpath="D:\IMAGE\"
response.write("<img src='" & imgpath & rs("照片") & "'>)

『肆』 如何從資料庫中讀取圖片,圖片存在文件夾中

我來回答你吧!我這些天碰到了和你一樣的問題,後來我解決了,我給你兩種方法。
方法一:圖片是直接存在SQL Server中的Image類型中的,你首先新建一個空白的aspx網頁,在這個新建的網頁的Page_Load()中讀出Image,然後用Response.BinaryWrite()函數顯示出來,然後在你原來的那個網頁中的Image控制項的ImageURL屬性填那個新建的aspx網頁,就是
image1.ImageUrl = "temp.aspx";這樣就好了
方法二:資料庫存的不是Image欄位,而是圖片的的地址,然後再Image的ImageURL中填這個地址就行了,直接能顯示出來。
我用的第二種方法,在我前幾天就這問題感到很迷茫的時候搜了很多資料,假如你還是不清楚就可以跟我說,我這段時間都在線!祝你好運!

『伍』 圖片怎麼從資料庫里讀取出來

推薦你先看這篇文章:
使用asp.net將圖片上傳並存入SqlServer中,然後從SqlServer中讀取並顯示出來
一,上傳並存入SqlServer
資料庫結構
create table test
{
id identity(1,1),
FImage image
}
相關的存儲過程
Create proc UpdateImage
(
@UpdateImage Image
)
As
Insert Into test(FImage) values(@UpdateImage)
GO

在UpPhoto.aspx文件中添加如下:
<input id="UpPhoto" name="UpPhoto" runat="server" type="file">
<asp:Button id="btnAdd" name="btnAdd" runat="server" Text="上傳"></asp:Button>

然後在後置代碼文件UpPhoto.aspx.cs添加btnAdd按鈕的單擊事件處理代碼:
private void btnAdd_Click(object sender, System.EventArgs e)
{
//獲得圖象並把圖象轉換為byte[]
HttpPostedFile upPhoto=UpPhoto.PostedFile;
int upPhotoLength=upPhoto.ContentLength;
byte[] PhotoArray=new Byte[upPhotoLength];
Stream PhotoStream=upPhoto.InputStream;
PhotoStream.Read(PhotoArray,0,upPhotoLength);

//連接資料庫
SqlConnection conn=new SqlConnection();
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";

SqlCommand cmd=new SqlCommand("UpdateImage",conn);
cmd.CommandType=CommandType.StoredProcere;

cmd.Parameters.Add("@UpdateImage",SqlDbType.Image);
cmd.Parameters["@UpdateImage"].Value=PhotoArray;

//如果你希望不使用存儲過程來添加圖片把上面四句代碼改為:
//string strSql="Insert into test(FImage) values(@FImage)";
//SqlCommand cmd=new SqlCommand(strSql,conn);
//cmd.Parameters.Add("@FImage",SqlDbType.Image);
//cmd.Parameters["@FImage"].Value=PhotoArray;

conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}

二,從SqlServer中讀取並顯示出來
在需要顯示圖片的地方添加如下代碼:
<asp:image id="imgPhoto" runat="server" ImageUrl="ShowPhoto.aspx"></asp:image>

ShowPhoto.aspx主體代碼:
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
SqlConnection conn=new SqlConnection()
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";

string strSql="select * from test where id=2";//這里假設獲取id為2的圖片
SqlCommand cmd=new SqlCommand()
reader.Read();
Response.ContentType="application/octet-stream";
Response.BinaryWrite((Byte[])reader["FImage"]);
Response.End();
reader.Close();
}
}

『陸』 怎樣從資料庫讀取圖片

把圖片路徑寫入資料庫,然後直接從裡面讀取圖片的路徑地址就好了。

『柒』 如何才能往資料庫里讀取圖片數據或者從資料庫里讀圖片

給你提供個ACCESS版的VB代碼,使用時調用這些過程即可:

'使用ADODB.Stream來保存/讀取圖像文件到資料庫
'引用Microsoft ActiveX Data Objects 2.5 Library及以上版本

'保存文件到資料庫中
Sub SaveFile()
Dim Stm As New ADODB.Stream
Dim Cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strCnn As String

strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & _
App.Path & "\DB1.mdb"
Cnn.Open strCnn

'讀取文件到內存(二進制模式)
With Stm
.Type = adTypeBinary
.Open
.LoadFromFile App.Path + "\Image1.bmp"
End With

With rs
.Open "SELECT * FROM TABLE1", Cnn, 1, 3
.AddNew
.Fields("IMAGE") = Stm.Read
.Update
End With

rs.Close
Stm.Close
Set rs = Nothing
Set Cnn = Nothing
Set Stm = Nothing
End Sub

'從資料庫中讀取圖像文件
Sub ReadFile()
Dim Stm As New ADODB.Stream
Dim Cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strCnn As String

strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & _
App.Path & "\DB1.mdb"
Cnn.Open strCnn
rs.Open "SELECT IMAGE FROM TABLE1 WHERE ID = 18", Cnn, adOpenKeyset, adLockReadOnly

'保存到文件
With Stm
.Mode = adModeReadWrite
.Type = adTypeBinary
.Open
.Write rs("IMAGE")
.SaveToFile App.Path + "\Image2.bmp"
End With

'顯示圖片
Picture1.Picture = LoadPicture(App.Path + "\Image2.bmp")

rs.Close
Stm.Close
Set rs = Nothing
Set Cnn = Nothing
Set Stm = Nothing
End Sub

『捌』 php中如何從資料庫中讀取圖片

比較普遍的方法是通過代碼實現。

『玖』 怎麼讀取資料庫中的圖片

確保你的圖片已經保存到資料庫,如果沒什麼錯誤,那就看下面 showming.asp '連接資料庫 <% id=clng(trim(request("id"))) if id="" then response.End response.Expires=0 response.buffer=true response.Clear() set rs=server.CreateObject("adodb.recordset") sql="select * from proct where proctid="&id&"" rs.open sql,conn,3,1 response.ContentType="image/*" response.BinaryWrite rs("photo") rs.close set rs=nothing conn.close set conn=nothing %> 顯示的圖片的頁面:picshow.asp <img src="showimg.asp?id=<%=rs(" proctid")%=>" width="400" height="300" border="0" alt="這是一張圖片" >

『拾』 資料庫以img存儲,如何讀取圖片

直接使用企業管理器好像沒有辦法操作吧,通過軟體或自己做個小軟體讀取。

#region //讀取資料庫中圖片到內存.並顯示
public void LoadToMemoryAndDisable(string serverAdress, string database)
{
//讀取資料庫中圖片到內存.並顯示
SqlConnection conn = new SqlConnection("server=" + serverAdress + ";integrated security = sspi;database = " + database);
SqlCommand cmd = new SqlCommand("select * from imgtable where imgname like '%bmp%'", conn);
conn.Open();
SqlDataReader dr;
try
{
dr = cmd.ExecuteReader();
dr.Read();
System.Data.SqlTypes.SqlBinary sb = dr.GetSqlBinary(2);
//或byte[] imageData = (byte[])dr[2];
MemoryStream ms = new MemoryStream(sb.Value);//在內存中操作圖片數據
Bitmap bmp = new Bitmap(Bitmap.FromStream(ms));
this.pictureBox1.Image = bmp;
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
#endregion