Can you convert this from C# to VB?

  • Thread starter Thread starter Rob T
  • Start date Start date
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.
 
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
---------@
 
Try this:

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

/claes
 
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!
************************************************
 
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!
 
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
 
Back
Top