12

I am pretty new in iTextSharp (the C# version of iText):

I have something like this:

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 });  

As you can see I have classic System.Drawing.Bitmap immage named bitmap and I want put it inside a cell of a PDF document table.

The problem is that this line is signed as error:

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

The error is:

Error 75 The best overloaded method match for 'iTextSharp.text.Image.GetInstance(iTextSharp.text.Image)' has some invalid arguments c:\Develop\EarlyWarning\public\Implementazione\Ver2\PdfReport\PdfVulnerability.cs 120 27 PdfReport

So I think that I need to obtain an iTextSharp.text.Image object from a classic System.Drawing.Bitmap object.

What can I do to do it? I am going crazy trying to do it.

Tnx

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

1 Answers1

23

There are no overloads that take just a System.Drawing.Image. You need to used one of these:

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)

The first one is probably the best choice and I'm 99% sure you can pass null for the color parameter.

Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • Just to verify: you can pass null as the color. Code documentation: if different from null (the basecolor) the transparency pixels are replaced by this color – Michaël Demey May 09 '14 at 07:07
  • @Chris Haas I'm using a GetInstance(System.Drawing.Image image, System.Drawing.Imaging.ImageFormat format) - why it always gets me a black rectangle instead my bitmap? – whizzzkey Sep 09 '14 at 04:49
  • 1
    I used it for Barcode image insert into pdf file wit null for color: Zen.Barcode.Code128BarcodeDraw barcode = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum; System.Drawing.Image barCodeImage = barcode.Draw("*y21456*", 50); BaseColor color = null; Image image = Image.GetInstance(barCodeImage, color); – Sharunas Bielskis Dec 21 '16 at 10:09
  • Using version 4.1.6.0 have to put some color. Altough have seen no difference using diffrent colors. – pnunes540 May 06 '20 at 16:54