Image in Crystal Reports

0

I'm using Crystal Reports in my Asp.Net MVC project. Home In Database Fields in Crystal Reports in my Visual Studio, I connected a C # class instead of going to the Database. Home This is the class:

public class MinhaImagem
    {
        public byte[] Imagem{ get; set; }
    }

The goal is to get a canvas on the client side and print it. Home My View looks like this:

function GerarImagem()
    {
        var $conteudo = $("#canvas-container");
        html2canvas($conteudo[0], {
            onrendered: function (canvas) {
                var base64 = canvas.toDataURL('image/jpeg');
                EnviarImagem(base64)
            },
            logging: true
        });
    }

    function EnviarImagem(base64) {
        $.post('@Url.Action("MinhaAction", "MeuController")', { imagem: base64 })
            .done(function (data) {
                // Código para baixar o pdf (que ainda não sei como)
            })
            .fail(function () {
                alert("Error");
            });
    }

My Controller looks like this:

public ActionResult MinhaAction(string imagem)
{
   try
   {
         MinhaImagem img = new MinhaImagem();

         string dataWithoutJpegMarker = imagem.Replace("data:image/jpeg;base64,", String.Empty);
         img.Imagem = Convert.FromBase64String(dataWithoutJpegMarker);

         using (reports.MeuRelatorio relatorio = new reports.MeuRelatorio ())
         {
             relatorio.SetDataSource(img);
             PdfFormatOptions pfo = ExportOptions.CreatePdfFormatOptions();
             pfo.CreateBookmarksFromGroupTree = true;

             Stream stream = relatorio.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
             byte[] fileBytes = new byte[stream.Length];
             int byteCount = stream.Read(fileBytes, 0, (int)stream.Length);
             string fileContent = Convert.ToBase64String(fileBytes);

             return Json(new
             {
                 success = false,
                 data = fileContent,
                 error = "Erro",
                 title = "Filtro"
              }, JsonRequestBehavior.AllowGet);
         }
   }
   catch (Exception ex)
   {
      return Json(new
                {
                    success = false,
                    message = ex.Message,
                    error = "Erro",
                    title = "Filtro"
                }, JsonRequestBehavior.AllowGet);
   }
}

Note: In my file .rpt , I added the Imagem field to sessão 5 (Rodapé) . Home I'm currently having an Exception in the following line:

relatorio.SetDataSource(img);
  

"The data source object is invalid."

    
asked by anonymous 19.08.2015 / 23:02

1 answer

0

Well, I discovered the problem. Everything is perfect! Except for one thing ...
The SetDataSource method does not accept this object, in my case I used a list although it is just an image.

List<MinhaImagem> listImagem = new List<MinhaImagem>();
listImagem.Add(img);

Now you can use the SetDataSource method:

relatorio.SetDataSource(listImagem);
    
20.08.2015 / 23:38