BTC
BTC

Reputation: 4072

Retrieve BLOB from MySQL with vb.net not using picturebox

This may seem like a trivial question, however I have searched for a decent explanation and tried to decipher this problem myself with little to no success. I wish to retrieve a blob I have stored on my MySql database, render it as an image, and display it as an image in my associated ASPX page. I am not using a picturebox. If there is a resource which I have just not seen, I will accept it as an answer and apologize for the duplicate question. I need no assistance establishing a connection.

Edited: Here's what I've been working with most recently, besides making a file stream to a directory I do not have access to write files to. The error occurs at Image.FromStream(lstr)

    Dim Query As String = "Select PIC from USERS where ID = 1"

    Dim adapter As New MySql.Data.MySqlClient.MySqlDataAdapter

    adapter.SelectCommand = New MySql.Data.MySqlClient.MySqlCommand(Query, connection)

    Dim Data As New DataTable

    Dim commandbuild As New MySql.Data.MySqlClient.MySqlCommandBuilder(adapter)

    adapter.Fill(data)


    Dim lb() As Byte = Data.Rows(Data.Rows.Count - 1).Item("PIC")
    Dim lstr As New System.IO.MemoryStream(lb)
    Dim X As Image = Image.FromStream(lstr)
    lstr.Close()

    Return X

Upvotes: 0

Views: 2941

Answers (1)

matzone
matzone

Reputation: 5719

In this case http://msdn.microsoft.com/en-us/library/93z9ee4x.aspx said that You must keep the stream open for the lifetime of the Image.

So you have to remove lstr.Close()

You can do that after you dispose lstr when you not neede it anymore ..

EDITED AGAIN :

Changed this

Dim lstr As New System.IO.MemoryStream(lb)
Dim X As Image = Image.FromStream(lstr)
lstr.Close()

to

Dim lstr As New System.IO.MemoryStream(lb)
Dim X As Image = Image.FromStream(lstr)
Dim X2 As Image = X
x.Dispose
lstr.Close()

then you can use X2 as your image

Upvotes: 1

Related Questions