【问题标题】:How to create a iTextSharp.text.Image object startng to a System.Drawing.Bitmap object?如何从 System.Drawing.Bitmap 对象开始创建 iTextSharp.text.Image 对象?
【发布时间】:2014-06-26 05:20:54
【问题描述】:

我是 iTextSharpiText 的 C# 版本)的新手:

我有这样的事情:

System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)ChartHelper.GetPdfChart((int)currentVuln.UrgencyRating * 10);

iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bitmap);

vulnerabilityDetailsTable.AddCell(new PdfPCell(img) { Border = PdfPCell.RIGHT_BORDER, BorderColor = new BaseColor(79, 129, 189), BorderWidth = 1, Padding = 5, MinimumHeight = 30, PaddingTop = 10 });  

如您所见,我有一个名为 bitmap 的经典 System.Drawing.Bitmap 图像,我想将它放在 PDF 文档表的单元格中。

问题是这一行被标记为错误:

iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bitmap);

错误是:

错误 75 最佳重载方法匹配 'iTextSharp.text.Image.GetInstance(iTextSharp.text.Image)' 有一些 无效的 参数 c:\Develop\EarlyWarning\public\Implementazione\Ver2\PdfReport\PdfVulnerability.cs 120 27 PdfReport

所以我认为我需要从经典的 System.Drawing.Bitmap 对象中获取 iTextSharp.text.Image 对象。

我能做些什么呢?我要疯了。

Tnx

【问题讨论】:

    标签: c# pdf pdf-generation itextsharp itext


    【解决方案1】:

    没有只需要System.Drawing.Image 的重载。您需要使用以下之一:

    GetInstance(System.Drawing.Image image, BaseColor color)
    GetInstance(System.Drawing.Image image, BaseColor color, bool forceBW)
    GetInstance(System.Drawing.Image image, System.Drawing.Imaging.ImageFormat format)
    

    第一个可能是最好的选择,我 99% 确定您可以将 null 传递给 color 参数。

    【讨论】:

    • 只是为了验证:您可以将 null 作为颜色传递。代码文档:如果与 null(基色)不同,则透明度像素将替换为此颜色
    • @Chris Haas 我正在使用 GetInstance(System.Drawing.Image image, System.Drawing.Imaging.ImageFormat 格式) - 为什么它总是给我一个黑色矩形而不是我的位图?
    • 我用它来将条形码图像插入到pdf文件中,颜色为空: Zen.Barcode.Code128BarcodeDraw 条形码 = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum; System.Drawing.Image barCodeImage = 条码.Draw("y21456", 50); BaseColor 颜色 = null; Image image = Image.GetInstance(barCodeImage, color);
    • 使用 4.1.6.0 版本必须放一些颜色。尽管使用不同的颜色没有任何区别。
    猜你喜欢
    • 2019-06-18
    • 1970-01-01
    • 2013-04-06
    • 2010-09-13
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    相关资源
    最近更新 更多