2016-10-05 1 views

Antwort

0

Um Bilder dynamisch zu laden, habe ich eine neue Klasse/ein neues Modell ImageModel erstellt.

public class ImageModel 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; }  
    public Byte[] ImageBytes { get; set; } 
} 

Diese Klasse ein Feld Bild, es lädt das Bild genannt hat. Der nächste Schritt ist das Hinzufügen von ImageBytes Feld zum Bericht.

Mithilfe der Crystal Report-Benutzeroberfläche mithilfe des Feld-Explorers wurde eine neue Verbindung erstellt. In dieser neuen Verbindung habe ich meine ImageModel Klasse als Modell verwendet. Durch Hinzufügen der ImageBytes Feld, bemerkte ich, dass Crystal Reports einen Typ crBlobFieldObject Objekt hinzugefügt.

das Bild zu laden war notwendig, den folgenden Code zu machen:

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; 
} 

Der Haupt Code wie folgt aussieht:

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); 
}