From 52a31127edc430995db88ad4cba3c20276c62118 Mon Sep 17 00:00:00 2001 From: ROBERTO ALVES PEREIRA Date: Thu, 2 Nov 2023 19:01:53 -0300 Subject: [PATCH] fix: gerando imagem em bytes --- NFe.AppTeste/MainWindow.xaml.cs | 4 ++-- NFe.Danfe.Nativo/NFCe/DanfeNativoNfce.cs | 28 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/NFe.AppTeste/MainWindow.xaml.cs b/NFe.AppTeste/MainWindow.xaml.cs index f71ce32e0..ec6b5a633 100644 --- a/NFe.AppTeste/MainWindow.xaml.cs +++ b/NFe.AppTeste/MainWindow.xaml.cs @@ -1768,8 +1768,8 @@ private void BtnCupom_Click(object sender, RoutedEventArgs e) //impr.Imprimir(salvarArquivoPdfEm: fileDialog.FileName.Replace(".pdf", "") + ".pdf"); - impr.GerarJPEG(fileDialog.FileName.Replace(".jpeg", "") + ".jpeg"); - + var bytes = impr.GerarImagem(); + var base64 = Convert.ToBase64String(bytes); } catch (Exception ex) { diff --git a/NFe.Danfe.Nativo/NFCe/DanfeNativoNfce.cs b/NFe.Danfe.Nativo/NFCe/DanfeNativoNfce.cs index 7ecb72839..6854668a0 100644 --- a/NFe.Danfe.Nativo/NFCe/DanfeNativoNfce.cs +++ b/NFe.Danfe.Nativo/NFCe/DanfeNativoNfce.cs @@ -137,6 +137,34 @@ public void GerarImagem(string filename, ImageFormat format) } } + public byte[] GerarImagem() + { + // Feito esse de cima para poder pegar o tamanho real da mesma desenhando + using (Bitmap bmp = new Bitmap(300, 70000)) + { + using (Graphics g = Graphics.FromImage(bmp)) + { + g.Clear(Color.White); + GerarNfCe(g); + } + + // Obtive o tamanho real na posição y agora vou fazer um com tamanho exato + using (Bitmap bmpFinal = new Bitmap(300, _y)) + using (var streamer = new MemoryStream()) + { + using (Graphics g = Graphics.FromImage(bmpFinal)) + { + g.Clear(Color.White); + g.DrawImage(bmp, 0, 0); + + bmpFinal.Save(streamer, ImageFormat.Jpeg); + } + + return streamer.ToArray(); + } + } + } + public void GerarJPEG(string filename) { GerarImagem(filename, ImageFormat.Jpeg);