我的png HTML渲染器有问题, 我正在尝试将png of View发送到电子邮件,但在电子邮件中我得到0B .png PS:Ticket.pdf没问题
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);
}
}
答案 0 :(得分:0)
您尝试在有效写入ms2
之前发送邮件。
您需要先刷新ms2
流缓冲区,然后再将其添加到mm
。 (正如您对ms
所做的那样,这就是正确处理pdf部分的原因)
(另外,小错字:“application / png”而不是“application / Png”,可能不是问题)
答案 1 :(得分:0)
问题:流在最后位置
结果: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);
}
}
谢谢大家