【发布时间】:2015-10-17 05:48:35
【问题描述】:
我的数据库中有一个带有图像路径的字段。我需要将图像转换为要在 Crystal Reports 中使用的字节数组。由于图像已经在我的根目录中,我不想使用上传功能。有什么办法可以实现吗?
【问题讨论】:
-
要将文件加载到字节数组中,请使用
File.ReadAllBytes。
标签: c# asp.net image bytearray
我的数据库中有一个带有图像路径的字段。我需要将图像转换为要在 Crystal Reports 中使用的字节数组。由于图像已经在我的根目录中,我不想使用上传功能。有什么办法可以实现吗?
【问题讨论】:
File.ReadAllBytes。
标签: c# asp.net image bytearray
在研究了这个问题后,我找到了一个适合我的解决方案。
customerReport.SetDataSource(dt);之前
我称这个方法为:showImageonReport(dt);
dt 是数据表:
"Image" 是保存图像路径的列名
private void showImageonReport(DataTable dt)
{
for (int index = 0; index < dt.Rows.Count; index++)
{
if (dt.Rows[index]["Image"].ToString() != "")
{
string s = this.Server.MapPath(
dt.Rows[index]["Image"].ToString());
if (File.Exists(s))
{
LoadImage(dt.Rows[index], "image_stream", s);
}
else
{
LoadImage(dt.Rows[index], "image_stream",
"DefaultPicturePath");
}
}
else
{
LoadImage(dt.Rows[index], "image_stream",
"DefaultPicturePath");
}
}
}
用于将图像从 url 加载到字节流
private void LoadImage(DataRow objDataRow, string strImageField, string FilePath)
{
try
{
FileStream fs = new FileStream(FilePath,
System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] Image = new byte[fs.Length];
fs.Read(Image, 0, Convert.ToInt32(fs.Length));
fs.Close();
objDataRow[strImageField] = Image;
}
catch (Exception ex)
{
Response.Write("<font color=red>" + ex.Message + "</font>");
}
}
请务必注意,当您在创建的数据集中添加 image_stream 时,请记住在您的数据库中添加此列并将其声明为 VARBINARY(MAX)
这应该可以完成工作
【讨论】: