答案 0 :(得分:1)
为了动态加载图像,我创建了一个名为 ImageModel 的新类/模型。
public class ImageModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Byte[] ImageBytes { get; set; }
}
此类有一个名为 Image 的字段,它将加载图像。
下一步是在报告中添加 ImageBytes 字段。
通过Crystal Report界面,使用Field Explorer,创建一个新连接。在这个新连接中,我使用 ImageModel 类作为模型。
通过添加 ImageBytes 字段,我注意到Crystal Reports添加了一个类型 crBlobFieldObject 对象。
要加载图像,必须制作以下代码:
public Byte[] GetImageBytes(string image_name)
{
Byte[] bytes = null;
if (!string.IsNullOrEmpty(image_name))
{
string app_path = ((System.Web.HttpRequestWrapper)this.Request)
.PhysicalApplicationPath;
app_path += "Content\\images\\";
string full_path = app_path + image_name;
//
if (System.IO.File.Exists(full_path))
{
FileStream fs = new FileStream(full_path, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
bytes = br.ReadBytes(Convert.ToInt32(br.BaseStream.Length));
}
}
return bytes;
}
主要代码如下:
public JsonResult GenerateCrystalReportImage()
{
List<ImageModel> list_image = new List<ImageModel>();
//
ImageModel imageone = new ImageModel();
imageone.Id = 1;
imageone.Name = "Image name one";
imageone.Description = "This is a image description one";
imageone.Image = GetImageBytes("imageone.png");
list_image.Add(imageone);
//
ImageModel imagetwo = new ImageModel();
imagetwo.Id = 2;
imagetwo.Name = "Image name two";
imagetwo.Description = "This is a image description two";
imagetwo.Image = GetImageBytes("imagetwo.png");
list_image.Add(imagetwo);
//
ReportDocument rp = new ReportDocument();
rp.Load(System.Web.HttpContext.Current.Server.MapPath("~/Reports/") + "Test.rpt");
rp.SetDataSource(list_image);
rp.ExportToHttpResponse(ExportFormatType.PortableDocFormat,
System.Web.HttpContext.Current.Response,
false,
"image_list_" + DateTime.Now);
rp.Close();
return Json(new
{
data = "ok",
results = 1,
success = true,
errors = String.Empty
}, JsonRequestBehavior.AllowGet);
}