c# - LEADTOOLS 将 QRBarcode 添加到现有图像

标签 c# asp.net-mvc leadtools-sdk

我当前的代码写入了二维码,但它仅用二维码覆盖了我的文件。我不知道如何调整二维码的大小以放置在文档的一个角落而不是占据整个页面。还不确定 RasterImage.Create 是否意味着它仅使用 qr 创建一个新文件并丢弃我的原始文件?

代码:- 将 PDF 转换为 Bmp 以添加 QR,然后保存回 PDF

 public void PDFFileExample()
    {
        RasterCodecs codecs1 = new RasterCodecs();

        codecs1.Options.Pdf.InitialPath = @"C:\LEADTOOLS 18\Bin\Dotnet4\Win32";
        codecs1.Dispose();
        RasterCodecs codecs2 = new RasterCodecs();
        codecs2.ThrowExceptionsOnInvalidImages = true;
        System.Diagnostics.Debug.Assert(codecs2.Options.Pdf.InitialPath == @"C:\LEADTOOLS 18\Bin\Dotnet4\Win32");

        string pdfFile = @"C:\QRCodeTesting\bottomRight.pdf";
        string destFileName1 = @"C:\QRCodeTesting\bottomRightOutputTemp.pdf";
        string destFileName2 = @"C:\QRCodeTesting\bottomRightOutput.bmp";

        RasterCodecs codecs = new RasterCodecs();

        if (codecs.Options.Pdf.IsEngineInstalled)
        {
            // Resulting image pixel depth.
            codecs.Options.Pdf.Load.DisplayDepth = 24;
            codecs.Options.Pdf.Load.GraphicsAlpha = 4;
            codecs.Options.Pdf.Load.Password = "";

            // Type of font anti-aliasing to use.
            codecs.Options.Pdf.Load.TextAlpha = 1;
            codecs.Options.Pdf.Load.UseLibFonts = true;

            // Horizontal,vertical  display resolution in dots per inch.
            codecs.Options.RasterizeDocument.Load.XResolution = 150;
            codecs.Options.RasterizeDocument.Load.YResolution = 150;

            using (RasterImage image = codecs.Load(pdfFile, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1))
            {

                // Set the PDF version to be v1.4
                codecs.Options.Pdf.Save.Version = CodecsRasterPdfVersion.V14;

                try
                {
                    // Save the image back as PDF
                    codecs.Save(image, destFileName1, RasterImageFormat.RasPdf, 24);
                }
                catch (RasterException ex)
                {
                    if (ex.Code == RasterExceptionCode.FileFormat)
                        MessageBox.Show(string.Format("Image in file {0} is loaded", destFileName1));
                    else
                    {
                        MessageBox.Show(string.Format("Could not load the file {0}.{1}{2}", destFileName1, Environment.NewLine, ex.Message));
                    }
                }
            }

            // And load it back before saving it as BMP
            using (RasterImage image = codecs.Load(destFileName1))
            {
                codecs.Save(image, destFileName2, RasterImageFormat.Bmp, image.BitsPerPixel);
                writeQRTag(destFileName2);
            }
        }
        else
        {
            MessageBox.Show("PDF Engine is not found!");
        }

        // Clean up
        codecs.Dispose();

    }

QRCode写入方法

private void writeQRTag(string imageFileName)
    {
        BarcodeEngine engine = new BarcodeEngine();

        // Create the image to write the barcodes to
        int resolution = 300;
        using (RasterImage image = RasterImage.Create((int)(8.5 * resolution), (int)(11.0 * resolution), 1, resolution, RasterColor.FromKnownColor(RasterKnownColor.Red)))
        {
            // Write two QR barcodes
            WriteQRCode(engine.Writer, image, QRBarcodeSymbolModel.Model1AutoSize, "QR Data 1", true);

            // Save the image
            using (RasterCodecs codecs = new RasterCodecs())
            {
                codecs.Save(image, imageFileName, RasterImageFormat.CcittGroup4, 1);
            }
        }
    }

最佳答案

我是来自 LEADTOOLS 支持的 Maen。

我检查了您的代码并注意到以下内容:

1) 当您调用 RasterImage.Create() 方法时,它将创建一个新的 RasterImage 对象,其中包含一个空的红色图像,您随后将其传递给 writeQRTag() 函数,然后使用给定的文件名保存。 保存时,红色会被黑色替换,因为您使用的文件格式仅支持黑白。由于您使用的是新图像,因此旧图像会丢失(被覆盖)。

如果您想在原始文件的图像上写入条形码,则不应创建新图像。相反,您需要使用已经使用 codecs.Load() 加载的图像并在其上写入条形码。

2) 代码执行多个加载和保存操作。通常,您不需要这样做,除非您的应用程序需要不同的文件格式(PDF、BMP 和 TIFF)。

3) 您创建了 RasterCodecs 对象的不同实例,但实际上仅使用其中之一。代码中不需要 4 个 RasterCodecs 对象中的 3 个。

如果您在使用我们的工具包的代码中仍然遇到问题,您可以通过电子邮件将详细信息发送至 [email protected]我们会尽力帮助您。

关于c# - LEADTOOLS 将 QRBarcode 添加到现有图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16596043/

相关文章:

javascript - 为什么 javascript 变量 get 会反射(reflect) ng-model 的变化?

asp.net-mvc - MVC Ajax.BeginForm 替换奇怪的行为

.net - 转换为 .NET 4.5 后,TableAttribute 在命名空间 System.ComponentModel.DataAnnotations.Schema 中不明确

c# - LeadTools MaximumGlobalRasterImageMemory

asp.net - 从 ASP.net Web 应用程序扫描文档

c# - 保存 XML 时强制无 BOM

c# - 为什么在编辑器中按Play时Unity崩溃?

ios - LEADTOOLS 条码生成 iOS

c# - 并行聚合不捕获所有迭代

c# - 包装和重新抛出异常会影响性能吗?