简体   繁体   English

C#转换为字节数组

[英]C# Convert to Byte Array

How can i convert this VBNet code into C#? 如何将VBNet代码转换为C#? ( ByteToImage is a User Defined Function use to convert Byte Array into Bitmap. ByteToImage用户定义的函数,用于将字节数组转换为位图。

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

I tried 我试过了

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

but it generates an error saying: Cannot implicitly convert type 'byte' to 'byte[]' 但它会产生一条错误消息: Cannot implicitly convert type 'byte' to 'byte[]'

What i am doing is basically converting an Image from database to byte array and displaying it on the picturebox. 我所做的基本上是将图像从数据库转换为字节数组,并将其显示在图片框上。

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

Try this 尝试这个

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

Hope this helps 希望这可以帮助

The problem is you have an array of bytes ( byte[] in C# and Byte() in VB.Net) but the Convert.ToByte call just returns a simple byte . 问题是您有一个字节数组(C#中的byte[]和VB.Net中的Byte() ),但是Convert.ToByte调用仅返回一个简单的byte To make this work you need to cast the return of SQLreader to byte[] . 为此,您需要将SQLreader的返回值SQLreaderbyte[]

There is no perfect analogous construct for CType in C# but a simple cast here should do the trick 在C#中没有完美的CType类似构造,但是在这里进行简单的转换应该可以解决问题

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

CType is the equivalent of a type cast, not an actual conversion.Besides, Convert.ToByte tries to convert its input to a single byte, not an array. CType等同于类型转换,不是实际转换。此外,Convert.ToByte尝试将其输入转换为单个字节,而不是数组。 The equivalent code is 等效代码是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM