gpt4 book ai didi

c# - 将图像转换为字节流

转载 作者:行者123 更新时间:2023-11-30 17:42:47 25 4
gpt4 key购买 nike

我的数据库中有一个包含图像路径的字段。我需要将图像转换为字节数组以便在 Crystal Reports 中使用。由于图像已经在我的根目录中,我不想使用上传功能。有什么办法可以实现这一目标吗?

最佳答案

研究问题后,我找到了适合我的解决方案。

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)

这应该可以完成工作

关于c# - 将图像转换为字节流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31646374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com