Can you convert this from C# to VB?

R

Rob T

Hi, I'm trying to convert his line from C# to VB:

System.Drawing.Image _img=
System.Drawing.Image.FromStream(new
System.IO.MemoryStream((byte[])SqlReader["Image"]);

I have this so far:
dim Img as SystemDrawing.Image
Img=System.Drawing.Image.FromStream(new
System.IO.MemoryStream(Convert.ToByte(SqlReader("Image"))))

I'm getting the error "specified cast is not valid". Basically, this line
reads in an image stored in a SQL server. My c# knowledge is lacking.....

Thanks. -Rob T.
 
B

Brian Lowe

Rob T said:
Hi, I'm trying to convert his line from C# to VB:

System.Drawing.Image _img=
System.Drawing.Image.FromStream(new
System.IO.MemoryStream((byte[])SqlReader["Image"]);

I have this so far:
dim Img as SystemDrawing.Image
Img=System.Drawing.Image.FromStream(new
System.IO.MemoryStream(Convert.ToByte(SqlReader("Image"))))

I'm getting the error "specified cast is not valid". Basically, this line
reads in an image stored in a SQL server. My c# knowledge is lacking.....

Looks to me like you're casting an Image to a Byte (not an Array of Bytes as
you intend)

Is there a .ToArray() method?

Brian Lowe
---------@
 
C

Claes Bergefall

Try this:

Dim Img as SystemDrawing.Image
Img=System.Drawing.Image.FromStream(new
System.IO.MemoryStream(CType(SqlReader("Image"), Byte()))

/claes
 
C

Cowboy \(Gregory A. Beamer\) [MVP]

You need to make a byte array, not a byte, so ConvertToByte() will not work
here. I would use CType(readerNameHere, Byte()).

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
 
R

Rob T

Ah....the array is what did me in.

This is what I did to get it work:

Dim AryByte() as Byte

AryByte = SqlReader("Image")

Img=System.Drawing.Image.FromStream(new

System.IO.MemoryStream(AryByte)

Thanks for your help!
 
C

Cor Ligthert

Hi Richard,

No converting gives extra errors the line from Claes was already fine in my
opinion, I did want to send a message when I saw it.

Cor
 

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