ASP.NET Image Disply from DB still not functioning properly

N

Neo Geshel

I still have a problem displaying images from an access DB using
asp.net. This showimage.aspx is for an admin site.

I need to pull the correct image from the correct table, hence the two
GET variables

I need to re-size the image, while keeping the proper proportions, on
the fly (before it gets to the browser). The images in the DB need to be
full size for the actual web site; thumbnails are only needed in the
admin site, hence the two functions that are included.

Right now I am receiving an error:
Exception Details: System.InvalidCastException: Specified cast is not
valid.

Source Error:

myDataReader.Read()
-> Dim imgStream As System.IO.MemoryStream = myDataReader.Item("Image")
Dim imgbin() As Byte
imgbin = createThumbnail(imgStream, 100, 100)
Response.ContentType="image/jpeg"
Response.BinaryWrite(imgbin)

Problem is, when I set imgStream to:

Dim imgStream As Byte = DirectCast(myDataReader.Item("Image"),Byte)

I get the following error:

Compiler Error Message: BC30311: Value of type 'Byte' cannot be
converted to 'System.IO.Stream'.

Source Error:

myDataReader.Read()
Dim imgStream As Byte = DirectCast(myDataReader.Item("Image"),Byte)
Dim imgbin() As Byte
-> imgbin = createThumbnail(imgStream, 100, 100)
Response.ContentType="image/jpeg"
Response.BinaryWrite(imgbin)


HEEEEELP!!! What could I possibly do to make this work???

TIA
....Geshel
--
**********************************************************************
My reply-to is an automatically monitored spam honeypot. Do not use it
unless you want to be blacklisted by SpamCop. Please reply to my first
name at my last name dot org.
**********************************************************************
 
K

Ken Tucker [MVP]

Hi,

Here is how to get an image from an access database. The northwind
database has an offset of 78 yours might not have one. Anyway your data
should be stored in an byte array not a memory stream. Hope this helps.


Private Sub ListBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ListBox1.SelectedValueChanged

Dim dr As DataRow = ds.Tables("Categories").Rows(ListBox1.SelectedIndex)

Dim ms As New System.IO.MemoryStream

Dim bm As Bitmap

Dim arData() As Byte = dr.Item("Picture")

ms.Write(arData, 78, arData.Length - 78)

bm = New Bitmap(ms)

PictureBox1.Image = bm

End Sub



Ken
-------------------
I still have a problem displaying images from an access DB using
asp.net. This showimage.aspx is for an admin site.

I need to pull the correct image from the correct table, hence the two
GET variables

I need to re-size the image, while keeping the proper proportions, on
the fly (before it gets to the browser). The images in the DB need to be
full size for the actual web site; thumbnails are only needed in the
admin site, hence the two functions that are included.

Right now I am receiving an error:
Exception Details: System.InvalidCastException: Specified cast is not
valid.

Source Error:

myDataReader.Read()
-> Dim imgStream As System.IO.MemoryStream = myDataReader.Item("Image")
Dim imgbin() As Byte
imgbin = createThumbnail(imgStream, 100, 100)
Response.ContentType="image/jpeg"
Response.BinaryWrite(imgbin)

Problem is, when I set imgStream to:

Dim imgStream As Byte = DirectCast(myDataReader.Item("Image"),Byte)

I get the following error:

Compiler Error Message: BC30311: Value of type 'Byte' cannot be
converted to 'System.IO.Stream'.

Source Error:

myDataReader.Read()
Dim imgStream As Byte = DirectCast(myDataReader.Item("Image"),Byte)
Dim imgbin() As Byte
-> imgbin = createThumbnail(imgStream, 100, 100)
Response.ContentType="image/jpeg"
Response.BinaryWrite(imgbin)


HEEEEELP!!! What could I possibly do to make this work???

TIA
....Geshel
--
**********************************************************************
My reply-to is an automatically monitored spam honeypot. Do not use it
unless you want to be blacklisted by SpamCop. Please reply to my first
name at my last name dot org.
**********************************************************************
 
B

Bruce Barker

generally a datareader return an image as a byte array. you cannot cast a
byte array to a stream. you must create a stream reader that knows how to
read a byte array, say like the MemoryStream reader.


myDataReader.Read()
Dim imgStream As new MemoryStream(myDataReader.Item("Image"))
imgbin = createThumbnail(imgStream, 100, 100)
Response.ContentType="image/jpeg"
Response.BinaryWrite(imgbin)

-- bruce (sqlwork.com)
 
N

Neo Geshel

Bruce said:
generally a datareader return an image as a byte array. you cannot cast a
byte array to a stream. you must create a stream reader that knows how to
read a byte array, say like the MemoryStream reader.


myDataReader.Read()
Dim imgStream As new MemoryStream(myDataReader.Item("Image"))
imgbin = createThumbnail(imgStream, 100, 100)
Response.ContentType="image/jpeg"
Response.BinaryWrite(imgbin)

-- bruce (sqlwork.com)

Thanks! Your response was the only one really on-target (the first two
talked about UPLOADS... huh?)

However, I ran into problems when I tried to implement your code:

Error: BC30519: Overload resolution failed because no accessible 'New'
can be called without a narrowing conversion:
Code: Dim imgStream As New MemoryStream(myDataReader.Item("Image"))

Error: BC30638: Array bounds cannot appear in type specifiers.
Code: Dim imgStream As MemoryStream(myDataReader.Item("Image"))

Any suggestions??

TIA
....Geshel
--
**********************************************************************
My reply-to is an automatically monitored spam honeypot. Do not use it
unless you want to be blacklisted by SpamCop. Please reply to my first
name at my last name dot org.
**********************************************************************
 
G

Guest

The DataReader.item("xxx") returns a "object" type. Hence it is necessary to
convert it into byte array so that the memory stream can read.

Hope this helps.
 

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

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top