2012-04-05 114 views
0

我已經成功地在sql server數據庫中輸入了一個圖像。現在我想從數據庫中檢索這個圖像並在Image控件中顯示它。我不想在頁面上放置任何ListBox。我在ASP點網寫道..如何從數據庫檢索圖像並將其顯示在圖像控件中?

Dim MS As MemoryStream 
Dim IMG As System.Drawing.Image 
Dim FS As FileStream 
Dim FI As FileInfo 
Dim IMGSTRM As Stream 
Dim IMGBYTS As Byte() 
Dim ImgData() As Byte 

    CMD1 = New SqlCommand("select * from IMG where userid=0", CON1) 
    If Not IsNothing(RDR1) Then RDR1.Close() 
    RDR1 = CMD1.ExecuteReader() 
    If RDR1.Read Then 
     IMGBYTS = RDR1!img 
     MS = New MemoryStream(IMGBYTS, False) 
     IMG = Image.FromStream(MS) 
     PIC1.ImageUrl = IMG 
    End If 

的問題是大膽的2行以上的結束如果

+0

可能的複製:[從Binarydata轉換爲圖像控件在asp.net](http://stackoverflow.com/q/7390983/55209),[顯示圖像從數據庫在ASP.net與C#](http://stackoverflow.com/ q/6987433/55209),[Ho w在Asp.net的圖像控件中顯示數據庫中的圖像?(http://stackoverflow.com/q/2482104/55209) – 2012-04-05 06:45:37

+0

你真的很喜歡你的shift/capslock鍵,不是嗎? – ThiefMaster 2012-04-05 08:05:39

+0

是的,我知道這是一個不好的做法,但我總是聲明我的變量L AND CLE CLE :) :) – Dev 2012-04-05 14:09:56

回答

1

如果您正在使用的桌面應用程序,這是解決

私人小組SqlBlob2File(BYVAL DestFilePath作爲字符串)

Dim PictureCol As Integer = 0 ' the column # of the BLOB field 
    Dim cn As New SqlConnection("server=localhost;integrated security=yes;database=NorthWind") 
    Dim cmd As New SqlCommand("SELECT Picture FROM Categories WHERE CategoryName='Test'", cn) 

    cn.Open() 
    Dim dr As SqlDataReader = cmd.ExecuteReader() 
    dr.Read() 
    Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte 
    dr.GetBytes(PictureCol, 0, b, 0, b.Length) 
    dr.Close() 

    cn.Close() 

    Dim ms As New System.IO.MemoryStream(b) 
    Me.PictureBox1.Image = System.Drawing.Image.FromStream(ms) 
End Sub 

這就是你可能想看看鏈接:http://support.microsoft.com/kb/321900/en-us

asp.net解決方案

在網頁或aspx頁面的情況下,你只能從一個網址的網頁顯示的圖像中,首先應該是一個圖片的網頁作爲webform2只有一個圖片框應該是那裏。此頁面是webform1的圖像。

這樣你可以把儘可能多的圖片,只要你想在頁面上,這取決於 當然,你做僞頁面相同數量的(並且不給他們 相同名稱)。

我希望這將是一個不錯的網頁。

\\ 必須有一個imagebox,一個按鈕,並在WebForm1的標籤

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
If Not IsPostBack Then 
Dim conn As New SqlConnection(connStr) 
Dim cmd As New SqlCommand("SELECT FileName, PictureID FROM Picture", conn) 
da = New SqlDataAdapter(cmd) 
cbd = New SqlCommandBuilder(da) 
dsPictures = New DataSet 
da.Fill(dsPictures) 
Me.Image1.Visible = False 
ListBox1.AutoPostBack = True 
Try 
ListBox1.DataSource = dsPictures.Tables(0) 
ListBox1.DataTextField = "FileName" 
ListBox1.DataValueField = "PictureID" 
ListBox1.DataBind() 
Catch sqlExc As SqlException 
Me.Label1.Text = "Database Error" 'sqlExc.ToString 
Catch exc As Exception 
Me.Label1.Text = "Datbase Connection Failed!" 
End Try 
conn.Close() 
End If 
End Sub 

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, 
ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged 
Session.Item("img") = ListBox1.SelectedItem.Value 
Image1.Visible = True 
Image1.ImageUrl = "http://localhost/testSQLPlaatjesWeb/WebForm2.aspx" 
End Sub 

/// \\

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles MyBase.Load 
Dim conn As New SqlConnection(connStr) 
Dim sqlstr As String = String.Format("SELECT Picture FROM Picture WHERE 
(PictureID = {0})", CInt(Session.Item("img"))) 
Dim cmd As New SqlCommand(sqlstr, conn) 
conn.Open() 
Dim rdr As SqlDataReader = cmd.ExecuteReader() 
rdr.Read() 
Response.BinaryWrite(CType(rdr.Item("Picture"), Byte())) 
rdr.Close() 
conn.Close() 
End Sub 

///

+0

我親愛的「Me.PictureBox1.Image」不存在於Asp Dotnet中。 ImageUrl存在於其中。我應該如何將Image1.ImageUrl分配給System.Drawing.Image.FromStream(ms)? – Dev 2012-04-05 07:06:20

+0

我已經更新了我的答案,請檢查並讓我知道如果這確實對你有用.. – 2012-04-05 09:33:56

+0

非常感謝它爲我工作最好。上帝祝福你.. – Dev 2012-04-05 14:08:54

相關問題