2012-04-05 47 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

相关问题