[英]Unable to cast object of type 'System.Byte[]' to type 'System.String[]'
Hello I'm trying to pass image
converted from Byte[]
to String[]
and display it in ReportViewer Image
as following: 您好,我正在尝试将从Byte[]
转换为String[]
image
传递到ReportViewer Image
,如下所示:
String[] dataImage;
private void showLogo()
{
try
{
SqlDataAdapter dataAdapter = new SqlDataAdapter( new SqlCommand("SELECT logo
FROM company WHERE id = 1", spojeni));
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
if (dataSet.Tables[0].Rows.Count == 1)
{
dataImage = new String[0];
dataImage = (String[])(dataSet.Tables[0].Rows[0]["logo"]);
}
}
catch (Exception ex)
{
MessageBox.Show(""+ex);
}
}
And this is the ReportViewer
parameter: 这是ReportViewer
参数:
ReportParameter[] parameter = new ReportParameter[24];
parameter[23] = new ReportParameter("rp_logo", dataImage );
this.reportViewer1.LocalReport.SetParameters(parameter);
this.reportViewer1.RefreshReport();
But I get following Exception
Unable to cast object of type 'System.Byte[]' to type 'System.String[]' 但是我得到以下Exception
无法将类型为“ System.Byte []”的对象转换为类型为“ System.String []”的对象
Can someone help me solve this? 有人可以帮我解决这个问题吗?
Thank you for your time. 感谢您的时间。
Given ReportViewer
requires a path to an image and not the actual image data itself, your best bet here is to save your byte[]
to disk and reference this in the parameter instead eg 给定ReportViewer
需要图像的路径,而不是实际图像数据本身,此处最好的选择是将byte[]
保存到磁盘并在参数中引用它,例如
using (var cmd = new SqlCommand("SELECT logo FROM company WHERE id = 1", spojeni))
using (var dataAdapter = new SqlDataAdapter(cmd))
using (var dataSet = new DataSet())
{
dataAdapter.Fill(dataSet);
if (dataSet.Tables[0].Rows.Count == 1)
{
// generate temp file destination
dataImage = System.IO.Path.GetTempFileName() + ".jpg"; // use whatever extension you expect the file to be in
File.WriteAllBytes(dataImage, (byte[])dataSet.Tables[0].Rows[0]["logo"]); // save image to disk
}
}
byte to string conversion can be done the following way 字节到字符串的转换可以通过以下方式完成
Encoding.Ascii.GetString(yourByteArray)
if you need a further conversion you can do somthing like that: 如果您需要进一步的转换,可以执行以下操作:
Encoding.Ascii.GetString(yourByteArray).Select(c => c as string).ToArray()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.