2012-02-03 92 views
-1

如何将VBNet代码转换为C#? (ByteToImage用户定义函数使用,以字节数组转换成位图C#转换为字节数组

Dim Bytes() As Byte = CType(SQLreader("ImageList"), Byte()) 
picStudent.Image = jwImage.ByteToImage(Bytes) 

我试图

byte[] Bytes = Convert.ToByte(SQLreader("ImageList")); // Error Here 
picStudent.Image = jwImage.ByteToImage(Bytes); 

,但它会产生一个错误说:Cannot implicitly convert type 'byte' to 'byte[]'

我在做什么是基本上将图像从数据库转换为字节数组并将其显示在图片框上。

+3

是什么类型'SQLReader'? – Oded 2012-02-03 15:38:53

+0

我假设SQLreader是一个DataReader?列“ImageList”是什么类型? – 2012-02-03 15:39:01

+0

'Convert.ToByte()'返回一个'byte'。您需要一种方法将任何类型的'SQLreader(“ImageList”)转换为'byte []'。 – jrummell 2012-02-03 15:39:03

回答

3

的问题是你有个字节(byte[]在C#和Byte()在VB.Net)的阵列,但Convert.ToByte调用只返回一个简单byte。要完成这项工作,您需要将SQLreader的回报投入byte[]

有在C#中CType没有完美的类似结构,但这里简单的铸件应该做的伎俩

byte[] Bytes = (byte[])SQLreader("ImageList"); 
11
byte[] Bytes = (byte[]) SQLreader("ImageList"); 
picStudent.Image = jwImage.ByteToImage(Bytes); 
5

试试这个

byte[] Bytes = (byte[])SQLreader("ImageList"); 

希望这有助于

2

CTYPE是一个类型转换相当于,并非实际conversion.Besides,Convert.ToByte尝试它的输入转换为一个单字节,而不是数组。等效代码是

byte[] bytes=(byte[])SQLreader("ImageList");