Working with Transparency in PostScript | .NET
Add transparency in PS document
PostScript doesn’t support transparency in painting vector graphics objects. However, translucent (partially transparent) images can be rendered as a set of fully transparent and fully opaque pixels. Such images are called masks.
Aspose.Page for .NET library offers a method that adds the transparent image to the PS document. As for painting vector graphics: shapes or text, we offer “pseudo-transparency”.
“Pseudo-transparency” is a process of paling colors that have an Alpha component of less than 255. It is reached by the specific blending of Red, Green, and Blue components with Alpha one.
“Pseudo-transparency”, of course, doesn’t allow us to see the lower colored layer from under the upper transparent layer, but makes an illusion of transparency if the bottom layer is white.
Add transparent image in PS document
As we wrote earlier transparent images can be added to the PS document as a mask and Aspose.Page for .NET library offers for this purpose a method AddTransparentImage(). This method recognizes whether the image is fully opaque or fully transparent or translucent. If it is fully opaque it is added as the opaque image in AddImage() method, if it is fully transparent it is not added to the document at all, if it is the translucent image it is added as a PostScript image mask.
In the example below we demonstrate the difference between adding a transparent image in a PS document with AddImage() and AddTransparentImage(). In order to see the white translucent image we set the page background color to non-white.
In order to add any image to a new PsDocument with Aspose.Page for .NET library in this example we do the following steps:
- Create an output stream for the resulting PS file.
- Create PsSaveOptions object with default options. Change the background color if it is required.
- Create a 1-paged PsDocument with an already created output stream and save options.
- Create a new graphics state.
- Create Bitmap from image file.
- Create the necessary transformation for the image.
- Add the image to PsDocument as a fully opaque image (using AddImage() method) if we are sure that the image is opaque or add one as a transparent image (using AddTransparentImage() method) if we are not sure that the image is opaque.
- Exit from the current graphics state to upper level one.
- Close the page.
- Save the document.
1//Create an output stream for PostScript document
2using (Stream outPsStream = new FileStream(dataDir + "AddTransparentImage_outPS.ps", FileMode.Create))
3{
4 //Create save options with A4 size
5 PsSaveOptions options = new PsSaveOptions();
6 //Set page's background color to see white image on it's own transparent background
7 options.BackgroundColor = Color.FromArgb(211, 8, 48);
8
9 // Create new 1-paged PS Document
10 PsDocument document = new PsDocument(outPsStream, options, false);
11
12
13 document.WriteGraphicsSave();
14 document.Translate(20, 100);
15
16 //Create a bitmap from translucent image file
17 using (Bitmap image = new Bitmap(dataDir + "mask1.png"))
18 {
19 //Add this image to the document as usual opaque RGB image
20 document.DrawImage(image, new Matrix(1, 0, 0, 1, 100, 0), Color.Empty);
21 }
22
23 //Again create a bitmap from the same image file
24 using (Bitmap image = new Bitmap(dataDir + "mask1.png"))
25 {
26 //Add this image to the document as transparent image
27 document.DrawTransparentImage(image, new Matrix(1, 0, 0, 1, 350, 0), 255);
28 }
29
30 document.WriteGraphicsRestore();
31
32 //Close current page
33 document.ClosePage();
34
35 //Save the document
36 document.Save();
37}
For Linux, MacOS and other non-Windows operation systems we offer to use our Aspose.Page.Drawing Nuget package. It uses Aspose.Drawing backend instead of System.Drawing system library.
So import Aspose.Page.Drawing namespace instead of System.Drawing one. In the above and the following code snippets Aspose.Page.Drawing.Bitmap will be used instead of System.Drawing.Bitmap, Aspose.Page.Drawing.Drawing2D.Matrix will be used instead of System.Drawing.Drawing2D.Matrix and so on. Our code examples on GitHub contain all the necessary substitutions.
See working with transparency in PS document in Java.
The result of running this code is next
Adding transparent vector graphics object
Earlier we wrote that Aspose.Page for .NET library uses a paling algorithm for transparent shapes and text, which we called “pseudo-transparency”. In the example below we demonstrate a difference between two shapes painted with the same color, but in the first shape without the Alpha component and in the second case with the Alpha component.
1//Create an output stream for PostScript document
2using (Stream outPsStream = new FileStream(dataDir + "ShowPseudoTransparency_outPS.ps", FileMode.Create))
3{
4 //Create save options with A4 size
5 PsSaveOptions options = new PsSaveOptions();
6
7 // Create new 1-paged PS Document
8 PsDocument document = new PsDocument(outPsStream, options, false);
9
10 float offsetX = 50;
11 float offsetY = 100;
12 float width = 200;
13 float height = 100;
14
15///////////////////////////////// Create a rectangle with opaque gradient fill /////////////////////////////////////////////////////////
16 GraphicsPath path = new GraphicsPath();
17 path.AddRectangle(new RectangleF(50, 100, 200, 100));
18
19 LinearGradientBrush opaqueBrush = new LinearGradientBrush(new RectangleF(0, 0, 200, 100), Color.FromArgb(0, 0, 0),
20 Color.FromArgb(40, 128, 70), 0f);
21 Matrix brushTransform = new Matrix(200, 0, 0, 100, 50, 100);
22 opaqueBrush.Transform = brushTransform;
23 Aspose.Page.EPS.GradientBrush gradientBrush = new GradientBrush(opaqueBrush);
24 gradientBrush.WrapMode = WrapMode.Clamp;
25
26 document.SetPaint(gradientBrush);
27 document.Fill(path);
28/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
29
30 offsetX = 350;
31
32///////////////////////////////// Create a rectangle with translucent gradient fill ///////////////////////////////////////////////////
33 //Create graphics path from the first rectangle
34 path = new GraphicsPath();
35 path.AddRectangle(new RectangleF(offsetX, offsetY, width, height));
36
37 //Create linear gradient brush colors which transparency are not 255, but 150 and 50. So it are translucent.
38 LinearGradientBrush translucentBrush = new LinearGradientBrush(new RectangleF(0, 0, width, height), Color.FromArgb(150, 0, 0, 0),
39 Color.FromArgb(50, 40, 128, 70), 0f);
40 //Create a transform for the brush.
41 brushTransform = new Matrix(width, 0, 0, height, offsetX, offsetY);
42 //Set the transform
43 translucentBrush.Transform = brushTransform;
44
45 //Set the paint
46 document.SetPaint(translucentBrush);
47 //Fill the rectangle
48 document.Fill(path);
49/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
50
51 //Close current page
52 document.ClosePage();
53
54 //Save the document
55 document.Save();
56}
The result of running this code is appeared as
You can download examples and data files from GitHub.