C# tutorial: creating an image in iTextSharp


Creating an image in iTextSharp

An image consists of a series of pixels. Each pixel has a color value. The series of pixels can be stored in an array of bytes. You can create a raw image from the byte array by using the GetInstance method of the Image class. The two images below are created by using a version of the GetInstance method as shown below.

GetInstance(int width, int height, int components, int bpc, byte[] data)

This version of the GetInstance method has four parameters:

- width--The width of the image in pixel.

- height--The height of the image in pixel.

- components--1,3 or 4 for GrayScale, RGB and CMYK.

- bpc--The number of bits per component.

- data--The byte array that is the data of the image.

pdf create image

The C# code below creates two images as shown above. The first image has 256 pixels x 1 pixel. It is a GrayScale image (1 component) and 8 bits is used per component. You need 256 bytes to describe the image. When you create the image data, you let the color value vary from 0 to 255. The second image also has 256 pixels x 1 pixel. However, because you used 3 components (RGB) in creating the image, you will use 256 bytes x 3bytes to describe the image.

//create gray image
Image img=Image.GetInstance(256,1,1,8,data);
img.ScaleAbsolute(256, 50);
img.SetAbsolutePosition(100, 570);
content.AddImage(img);
//create an array of bytes for the second and third images
byte[] cgradient = new byte[256 * 3];
for (int i = 0; i < 256; i++) {
cgradient[i * 3] = (byte) (255 - i);
cgradient[i * 3 + 1] = (byte) (i);
cgradient[i * 3 + 2] = (byte) i;
}
//create the second image
Image img2 = Image.GetInstance(256, 1, 3, 8, cgradient);
img2.ScaleAbsolute(256, 50);
img2.SetAbsolutePosition(100, 500);
content.AddImage(img2);

Make the image transparency

You can make the image transparent by using the PdfGState class. As you learnt from the previous post, this class allows you to specify an opacity value to the image when it is added to the PdfContentByte object.

pdf image transparency

//get PdfContentByte object
PdfContentByte content = writer.DirectContent;

//create an array of bytes for the first images
byte[] fimgdata = new byte[256 * 3];
for (int i = 0; i < 256; i++) {
fimgdata[i * 3] = (byte)(255 - i);
fimgdata[i * 3 + 1] = (byte)(255 - i);
fimgdata[i * 3 + 2] = (byte)i;
}

//create an array of bytes for the second images
byte[] simgdata = new byte[256 * 3];
for (int i = 0; i < 256; i++)
{
simgdata[i * 3] = (byte)(i);
simgdata[i * 3 + 1] = (byte)(255 - i);
simgdata[i * 3 + 2] = (byte)i;
}
//create the first image
Image img1 = Image.GetInstance(256, 1, 3, 8, fimgdata);
img2.ScaleAbsolute(256, 50);
img2.SetAbsolutePosition(100, 500);
content.AddImage(img1);
//create the third image
Image img2 = Image.GetInstance(256, 1, 3, 8, simgdata);
img2.ScaleAbsolute(256, 50);
img2.SetAbsolutePosition(50,530);
//make the second image transparent
PdfGState state = new PdfGState();
state.FillOpacity = 0.4f;
content.SetGState(state);
content.AddImage(img2);



Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.