By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,344 Members | 1,139 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,344 IT Pros & Developers. It's quick & easy.

ASP.NET Image Disply from DB still not functioning properly

P: n/a
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.
************************************************** ********************
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
You need to convert the data to a Byte Array before giving assigning it to a
memory stream.

Here's more info:
http://SteveOrr.net/articles/EasyUploads.aspx
http://SteveOrr.net/articles/ImproveYourImages.aspx

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net

"Neo Geshel" <go****@geshel.org> wrote in message
news:uU**************@TK2MSFTNGP14.phx.gbl...
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.
************************************************** ********************

Nov 21 '05 #2

P: n/a
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.SelectedInde x)

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
-------------------
"Neo Geshel" <go****@geshel.org> wrote in message
news:uU**************@TK2MSFTNGP14.phx.gbl...
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.
************************************************** ********************
Nov 21 '05 #3

P: n/a
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)

"Neo Geshel" <go****@geshel.org> wrote in message
news:uU**************@TK2MSFTNGP14.phx.gbl...
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.
************************************************** ********************

Nov 21 '05 #4

P: n/a
Bruce Barker wrote:
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.
************************************************** ********************
Nov 21 '05 #5

P: n/a
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.
"Neo Geshel" wrote:
Bruce Barker wrote:
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.
************************************************** ********************

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.