-1

i have problem with png HTML renderer, i am trying to send png of View to email, but on email i get 0B .png PS: Ticket.pdf is ok

 using (MemoryStream ms = new MemoryStream())
        {

            var pdf = PdfGenerator.GeneratePdf(RenderRazorViewToString("TicketTemplateBig", model), PdfSharp.PageSize.A4);
            pdf.Save(ms, false);

            /////////////////
            //Bitmap bitmap = new Bitmap(Convert.ToInt32(1024), Convert.ToInt32(1024), System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            using (MemoryStream ms2 = new MemoryStream())
            {
                //Image image = TheArtOfDev.HtmlRenderer.WinForms.HtmlRender.RenderToImage(RenderRazorViewToString("TicketTemplateBig", model));

                Bitmap bitmap = (Bitmap)Image.FromFile(@"C:\logo.png");
                bitmap.Save(ms2, ImageFormat.Png);


                /////////////////
                await ms.FlushAsync();
                await ms2.FlushAsync();
                mm.Attachments.Add(new Attachment(ms, string.Format("Ticket.pdf"), "application/pdf"));
                streams.Add(ms);
                mm.Attachments.Add(new Attachment(ms2, string.Format("logo.png"), "application/png"));
                streams.Add(ms2);
                await client.SendMailAsync(mm);
            }
        }
Johny.M
  • 1
  • 3
  • try changing application/Png to application/img or application/png – Glubus Nov 14 '17 at 14:24
  • as I see it you are not writing to the memory stream ms2. – Igor Jakovljevic Nov 14 '17 at 14:25
  • 1
    Have you verified that this code can actually create a valid .ping file? I'd try writing to a `FileStream` pointed to a local .png file, and making sure you can open it first. That will let you know if the problem is with the rendering, the stream, or the email. – Bradley Uffner Nov 14 '17 at 14:30
  • @BradleyUffner i tried , and its sending 0B png too and i added flush to ms2 and still same – Johny.M Nov 14 '17 at 14:56

2 Answers2

0

You are trying to send the mail before having effectively written to ms2.

you need to do flush the ms2 stream buffer before adding it to mm . (as you did for ms, that's why the pdf part was handled correctly)

(Also, minor typo : "application/png" instead of "application/Png", probably not an issue)

Pac0
  • 21,465
  • 8
  • 65
  • 74
0

problem : stream was on last position

result : ms2.Position = 0;

           using (MemoryStream ms = new MemoryStream())
        {

            var pdf = PdfGenerator.GeneratePdf(RenderRazorViewToString("TicketTemplateBig", model), PdfSharp.PageSize.A4);
            pdf.Save(ms, false);

            using (MemoryStream ms2 = new MemoryStream())
            {
                Image image = TheArtOfDev.HtmlRenderer.WinForms.HtmlRender.RenderToImage(RenderRazorViewToString("TicketTemplateBig", model));
                image.Save(ms2, ImageFormat.Png);
                ms2.Position = 0;
                await ms.FlushAsync();
                await ms2.FlushAsync();
                mm.Attachments.Add(new Attachment(ms, string.Format("Ticket.pdf"), "application/pdf"));
                mm.Attachments.Add(new Attachment(ms2, string.Format("Ticket.png"), "application/png"));
                await client.SendMailAsync(mm);
            }
        }

Thanks Guys

Johny.M
  • 1
  • 3