简体   繁体   English

将 iTextSharp.text.pdf.BarcodeQRCode 转换为 System.Drawing.Image

[英]Convert iTextSharp.text.pdf.BarcodeQRCode to System.Drawing.Image

Looking for a way to convert iTextSharp.text.pdf.BarcodeQRCode to System.Drawing.Image寻找将iTextSharp.text.pdf.BarcodeQRCode转换为System.Drawing.Image的方法

This is what I have so far...这是我目前所拥有的......

public System.Drawing.Image GetQRCode(string content)
{
     iTextSharp.text.pdf.BarcodeQRCode qrcode = new iTextSharp.text.pdf.BarcodeQRCode(content, 115, 115, null);
     iTextSharp.text.Image img = qrcode.GetImage();
     MemoryStream ms = new MemoryStream(img.OriginalData);
     return System.Drawing.Image.FromStream(ms); 
}

In line 3 above using img.OriginalData returns null Using img.RawData on line 3 instead thows invalid parameter error on line 4.在上面的第 3 行中,使用 img.OriginalData 返回 null 在第 3 行使用 img.RawData 而不是在第 4 行出现无效参数错误。

I've googled some of the code samples on how to perform the thing you want and your code (the "OriginalData" approach) is basicaly the same: https://csharp.hotexamples.com/examples/iTextSharp.text.pdf/BarcodeQRCode/-/php-barcodeqrcode-class-examples.html .我已经搜索了一些关于如何执行您想要的东西的代码示例,并且您的代码(“OriginalData”方法)基本相同: https://csharp.hotexamples.com/examples/iTextSharp.text.pdf/ BarcodeQRCode/-/php-barcodeqrcode-class-examples.html

However, I don't see how it could work.但是,我不明白它是如何工作的。 From my investigations of BarcodeQRCode#getImage it seems that OriginalData is not set while processing such a barcode, so it will always be null.从我对BarcodeQRCode#getImage的调查来看,处理此类条形码时似乎未设置OriginalData ,因此它始终为 null。

More than that, the code you mention belongs to iText 5, which is end of life and no longer maintained (with an exception of considerable security fixes), so it's recommended to update to iText 7.不仅如此,您提到的代码属于iText 5,该代码已终止并且不再维护(除了大量安全修复程序),因此建议更新到iText 7。

As for iText 7, I do see how to achieve the same in Java, since barcode classes do have a createAwtImage method there.至于 iText 7,我确实看到了如何在 Java 中实现相同的效果,因为条形码类确实有一个createAwtImage方法。 .NET, on the other hand, lacks such a functionality, so I'd day that one unfortunately couldn't do it in .NET.另一方面,.NET 缺少这样的功能,所以我很遗憾在 .NET 中无法做到这一点。

There are some good reasons for that.这有一些很好的理由。 iText's Image s (and a barcode could be easily converted to an iText's Image object as shown here: https://kb.itextpdf.com/home/it7kb/faq/how-to-generate-2d-barcode-as-vector-image ) represent a PDF's XObject. iText 的Image (和条形码可以很容易地转换为 iText 的Image object 如下所示: https://kb.itextpdf.com/home/it7kb/faq/how-to-generate-2d-barcode-as-vector- image ) 表示 PDF 的 XObject。 In PDF syntax, an image file (jpg, png, etc.) is an XObject with the raw image data stored inside.在 PDF 语法中,图像文件(jpg、png 等)是一个 XObject,其中存储了原始图像数据。 However, an XObject can also contain PDF syntaxt content (it is not just used for image files).但是,XObject 也可以包含 PDF 语法内容(它不仅用于图像文件)。 So to render such a content one needs to process the data from PDF syntax to image syntax, which is not that easy.所以要渲染这样一个内容,需要将数据从 PDF 语法处理为图像语法,这并不容易。 There are some means in Java's awt to do so, that's why it's implemented in Java. Java的awt中有一些方法可以做到这一点,这就是它在Java中实现的原因。 As for .NET, since there is no out-of-the-box means to convert PDF images to System.Drawing.Image, it was decided not to implement it.至于.NET,由于没有开箱即用的方法将PDF图像转换为System.Drawing.Image,所以决定不实现。

To conclude, there is another iText product, pdfRender , which allows one to convert PDF files (and you could create a page just for a barcode) to images.总而言之,还有另一个 iText 产品pdfRender ,它允许将 PDF 文件(您可以为条形码创建一个页面)转换为图像。 Perhaps you might want to play with it: https://itextpdf.com/en/products/itext-7/convert-pdf-to-image-pdfrender也许您可能想玩它: https://itextpdf.com/en/products/itext-7/convert-pdf-to-image-pdfrender

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM