Image in Crystal Reports

Asked

Viewed 483 times

0

I am using Crystal Reports in my Asp.Net MVC project.
In Database Fields in Crystal Reports in my Visual Studio, I connected a C# class instead of going to the Database.
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.
My View is 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 added the field Imagem in sessão 5 (Rodapé).
I’m currently having a Exception in the following line:

relatorio.SetDataSource(img);

"The data source object is invalid."

1 answer

0


Well, I figured out the problem. Everything is perfect! Except for one thing...
The Setdatasource method does not accept this object, in my case I used a list despite being only an image.

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

Now you can use the Setdatasource method:

relatorio.SetDataSource(listImagem);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.