Better solution ?

L

LB

Hello everybody,
I'm trying to display into a system.Web.UI.WebControls.Image a picture
coming from the DB.
I used the code below, and after specifying my image1.url property, the
image is displayed.

But now, I have still the image stored in my "DestFilePath " directory.
Is there a way to directly read the blob, and assign it to my
system.Web.UI.WebControls.Image without writing the file itself to the disk
?

Thank you so much in advance
LB


Public Sub GetBlobInfo(DestFilePath As String)
Try
Dim PictureCol As Integer = 0 ' the column # of the BLOB field
Dim cn As New SqlConnection("server=localhost;integrated
security=yes;database=SomeDB")
Dim cmd As New SqlCommand("SELECT Picture FROM mytable WHERE
somecondition='Test'", cn)
cn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue)) As
[Byte]
dr.GetBytes(PictureCol, 0, b, 0, b.Length)
dr.Close()
cn.Close()
Dim fs As New System.IO.FileStream(DestFilePath,
System.IO.FileMode.Create, System.IO.FileAccess.Write)

fs.Write(b, 0, b.Length)
fs.Close()
MessageBox.Show("Image written to file successfully")
Catch ex As SqlException
MessageBox.Show(ex.Message)
End Try
End Sub
 
E

Eliyahu Goldin

The standard solution is to a have a special web page, called, for example,
GetImage.aspx. Your image url will point to the page:

ImageUrl="GetImage.aspx?id=myImageId"

The page will find the image in the database according to id and stream it
down to the client in the response stream.

Eliyahu
 
K

Kevin Spencer

Sure. You fetch it from the db, convert the array of bytes to a stream, sets
the Response.ContentType to "image/jpg", and save the stream to the
Response.OutputStream. This can be done by creating an ASPX page that serves
as the "image url."

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
J

Joseph Byrns

So just to add to what the other two said, here is an example, this is the
load event of a page called GetImage.aspx:

Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Clear()
Response.ContentType = "image/jpg"
Dim map As Bitmap = CType(Session("map"), Bitmap) ''you would get the image
from the db here
Dim ms As New MemoryStream
map.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Response.BinaryWrite(ms.ToArray)
Response.End()


Kevin Spencer said:
Sure. You fetch it from the db, convert the array of bytes to a stream,
sets the Response.ContentType to "image/jpg", and save the stream to the
Response.OutputStream. This can be done by creating an ASPX page that
serves as the "image url."

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
What You Seek Is What You Get.

LB said:
Hello everybody,
I'm trying to display into a system.Web.UI.WebControls.Image a picture
coming from the DB.
I used the code below, and after specifying my image1.url property, the
image is displayed.

But now, I have still the image stored in my "DestFilePath " directory.
Is there a way to directly read the blob, and assign it to my
system.Web.UI.WebControls.Image without writing the file itself to the
disk ?

Thank you so much in advance
LB


Public Sub GetBlobInfo(DestFilePath As String)
Try
Dim PictureCol As Integer = 0 ' the column # of the BLOB field
Dim cn As New SqlConnection("server=localhost;integrated
security=yes;database=SomeDB")
Dim cmd As New SqlCommand("SELECT Picture FROM mytable WHERE
somecondition='Test'", cn)
cn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue)) As
[Byte]
dr.GetBytes(PictureCol, 0, b, 0, b.Length)
dr.Close()
cn.Close()
Dim fs As New System.IO.FileStream(DestFilePath,
System.IO.FileMode.Create, System.IO.FileAccess.Write)

fs.Write(b, 0, b.Length)
fs.Close()
MessageBox.Show("Image written to file successfully")
Catch ex As SqlException
MessageBox.Show(ex.Message)
End Try
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top