PDFsharp & MigraDoc Foundation http://forum.pdfsharp.de/ |
|
Image has no valid type error while rendering pdf http://forum.pdfsharp.de/viewtopic.php?f=2&t=4441 |
Page 1 of 1 |
Author: | amulya [ Sun May 14, 2023 9:31 pm ] |
Post subject: | Image has no valid type error while rendering pdf |
The below code was working perfectly fine with pdfsharp-migradoc (1.50.4820-RC1). I recently migrated it to pdfsharp-migradoc (6.0.0-preview-2) and I get the error "Image has no valid type" when I open the pdf Attachment: pdf error.PNG [ 9.04 KiB | Viewed 13879 times ] I have an image in base64 format which I add to the document using the following code Code: private static void AddLabels(Document document, IList<string> images) { var section = document.LastSection; // Add an image for (int i = 0; i < images.Count; ++i) { if (i > 0) { section.AddPageBreak(); } var image = section.AddImage("base64:" + images[i]); image.LockAspectRatio = true; image.Height = "4in"; } } Code: AddLabels(document, images);
var renderer = new PdfDocumentRenderer { // Associate the MigraDoc document with a renderer. Document = document, PdfDocument = new PdfDocument() }; renderer.RenderDocument(); |
Author: | TH-Soft [ Mon May 15, 2023 8:07 am ] |
Post subject: | Re: Image has no valid type error while rendering pdf |
amulya wrote: The below code was working perfectly fine with pdfsharp-migradoc (1.50.4820-RC1). I recently migrated it to pdfsharp-migradoc (6.0.0-preview-2) and I get the error "Image has no valid type" when I open the pdf Which versions and flavours of MigraDoc are you using?What can we say without having the image (image file, not BASE64 encoded). The list of supported formats can be seen here: https://docs.pdfsharp.net/PDFsharp/Over ... ge-formats |
Author: | Extragorey [ Wed Jan 10, 2024 12:41 am ] | ||
Post subject: | Re: Image has no valid type error while rendering pdf | ||
I'm encountering this issue too. I've tried using AddImage() with the base64: prefix for both BMP and PNG formats with no success. The image in question is a QR code representing "https://www.example.com", which I've attached (in PNG format). I'm using the PDFsharp-MigraDoc nuget package, version 6.0.0 (not GDI). Here's the code I'm using to generate the QR code base-64 string (requires QRCoder nuget package version 1.4.3): Code: var qrGenerator = new QRCodeGenerator(); var qrData = qrGenerator.CreateQrCode("https://www.example.com", QRCodeGenerator.ECCLevel.Q); var qrCode = new BitmapByteQRCode(qrData); var qrCodeImage = qrCode.GetGraphic(pixelsPerModule: 8); var base64 = Convert.ToBase64String(qrCodeImage); paragraph.AddImage("base64:" + base64); The same code worked fine with the GDI build of PDFsharp-MigraDoc (6.0.0), but I've had to switch to the platform-agnostic version due to difficulties targeting net6.0-windows in a web application (as opposed to just targeting net6.0).
|
Author: | Thomas Hoevel [ Wed Jan 10, 2024 8:39 am ] | ||
Post subject: | Re: Image has no valid type error while rendering pdf | ||
Extragorey wrote: I'm encountering this issue too. I've tried using AddImage() with the base64: prefix for both BMP and PNG formats with no success. I converted it to BMP using MS Paint and it works as expected.The image in question is a QR code representing "https://www.example.com", which I've attached (in PNG format). How did you convert to BMP?
|
Author: | Thomas Hoevel [ Wed Jan 10, 2024 8:42 am ] |
Post subject: | Re: Image has no valid type error while rendering pdf |
Extragorey wrote: I'm encountering this issue too. I've tried using AddImage() with the base64: prefix for both BMP and PNG formats with no success. The PNG file you attached here works with PDFsharp. This format is supported.
The image in question is a QR code representing "https://www.example.com", which I've attached (in PNG format). |
Author: | Extragorey [ Thu Jan 11, 2024 12:15 am ] | ||
Post subject: | Re: Image has no valid type error while rendering pdf | ||
Thomas Hoevel wrote: Extragorey wrote: I'm encountering this issue too. I've tried using AddImage() with the base64: prefix for both BMP and PNG formats with no success. The PNG file you attached here works with PDFsharp. This format is supported.The image in question is a QR code representing "https://www.example.com", which I've attached (in PNG format). The image I previously attached was actually a BMP that I had to convert to PNG (using image software) because the forum doesn't support BMP uploads. I've now changed my code to generate a PNG image instead (and removed the white border), and have included that image with this post. Apologies for the confusion. You can also reproduce it with the code I provided - just in case the forum processes image uploads somehow and doesn't preserve the original byte code. (Edit: I've just re-downloaded the image below and verified it has the same byte representation as the image that fails to load in MigraDoc.) The weird thing is, the same document renders a barcode in BMP format from memory just fine (from the BarcodeLib nuget library), using the base64: prefix, but there must be something different about the way the QRCoder library generates images that is incompatible with MigraDoc. I've also tried copying the base-64 representation into HTML to render the image there (i.e. <img src="data:image/png;base64,[bytes]" /> which it renders as expected), then save that as a PNG file and import the file using MigraDoc, and that doesn't work either. So it's not specific to the base64: implementation, but something about the image format it seems.
|
Author: | TH-Soft [ Thu Jan 11, 2024 9:04 am ] |
Post subject: | Re: Image has no valid type error while rendering pdf |
Extragorey wrote: The weird thing is, the same document renders a barcode in BMP format from memory just fine (from the BarcodeLib nuget library), using the base64: prefix, but there must be something different about the way the QRCoder library generates images that is incompatible with MigraDoc. As of today, PDFsharp Core build supports monochrome BMP images, but does not support monochrome PNG images. That's an implementation limitation.Convert the monochrome PNG image to a monochrome BMP image or turn it into a color-PNG image and it will work. Using a monochrome BMP will have no impact on the size. https://docs.pdfsharp.net/PDFsharp/Over ... tions.html |
Author: | Extragorey [ Thu Jan 11, 2024 11:13 pm ] |
Post subject: | Re: Image has no valid type error while rendering pdf |
TH-Soft wrote: Extragorey wrote: The weird thing is, the same document renders a barcode in BMP format from memory just fine (from the BarcodeLib nuget library), using the base64: prefix, but there must be something different about the way the QRCoder library generates images that is incompatible with MigraDoc. As of today, PDFsharp Core build supports monochrome BMP images, but does not support monochrome PNG images. That's an implementation limitation.Convert the monochrome PNG image to a monochrome BMP image or turn it into a color-PNG image and it will work. Using a monochrome BMP will have no impact on the size. https://docs.pdfsharp.net/PDFsharp/Over ... tions.html The BMP is what I tried first though, and that didn't work either. I've uploaded the full byte representation here, since the forum doesn't allow BMP uploads (and most image hosts convert it to PNG): https://justpaste.it/85i6r It makes sense there's a difference between monochrome and colour formats though, I didn't think of that. I may have to try doing some additional image processing to get it to work. |
Author: | Extragorey [ Fri Jan 12, 2024 2:54 am ] |
Post subject: | Re: Image has no valid type error while rendering pdf |
I've found the issue! The images I was trying to render (both BMP and PNG formats) had a pixel format of PixelFormat.Format1bppIndexed, which MigraDoc apparently doesn't like. I converted it to PixelFormat.Format32bppArgb (using System.Drawing, since I only need the code to run on Windows) and viola, the image renders. Here's my helper function to perform the conversion: Code: // Required namespaces
using System.Drawing; using System.Drawing.Imaging; using System.IO; private static byte[] ConvertPngToArgb(byte[] imageData) { var ms = new MemoryStream(imageData); var image = new Bitmap(ms); var newImage = image.Clone(new Rectangle(0, 0, image.Width, image.Height), PixelFormat.Format32bppArgb); var newMs = new MemoryStream(); newImage.Save(newMs, ImageFormat.Png); return newMs.ToArray(); } |
Author: | TH-Soft [ Fri Jan 12, 2024 8:11 am ] |
Post subject: | Re: Image has no valid type error while rendering pdf |
Extragorey wrote: I've found the issue! The images I was trying to render (both BMP and PNG formats) had a pixel format of PixelFormat.Format1bppIndexed, which MigraDoc apparently doesn't like. 1 BPP works in BMP format, but not in PNG format.1 BPP works for PNG when using the GDI or WPF builds. And "since I only need the code to run on Windows" you can use one of those builds. You might get smaller PDF files when converting to 1 BPP BMP instead of 32 BPP PNG. And 8 BPP PNG is also supported and may also lead to smaller PDF files. |
Author: | Extragorey [ Tue Jan 16, 2024 4:16 am ] |
Post subject: | Re: Image has no valid type error while rendering pdf |
TH-Soft wrote: Extragorey wrote: I've found the issue! The images I was trying to render (both BMP and PNG formats) had a pixel format of PixelFormat.Format1bppIndexed, which MigraDoc apparently doesn't like. 1 BPP works in BMP format, but not in PNG format.1 BPP works for PNG when using the GDI or WPF builds. And "since I only need the code to run on Windows" you can use one of those builds. You might get smaller PDF files when converting to 1 BPP BMP instead of 32 BPP PNG. And 8 BPP PNG is also supported and may also lead to smaller PDF files. You're right, the BMP's pixel format was Format24bppRgb, not 1 BPP. Annoying how the QRCoder library's output is so inconsistent. I've now changed it to use Format8bppIndexed, which seems to work. My initial approach was to use the GDI build as you suggest, which greatly simplifies font and image handling, but it forces projects to target net6.0-windows which has massive caveats for a web application, even when only running it on Windows. Namely, it doesn't even use the same .NET 6 runtime, but the "Desktop Runtime" instead. This also seems to prevent on-the-fly updates/deployments as the application's DLL files are locked by the system. All fine and dandy for a WinForms application, perhaps, but not for a web API hosted in IIS. I found all this out through a long, frustrating process of trial and error, as you would think there's no harm in targeting net6.0-windows in an application that's only going to run on Windows. Big mistake. |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |