Lavorare con la trasparenza in PostScript | C++

Aggiungere trasparenza in un documento PS

PostScript non supporta la trasparenza nella pittura di oggetti di grafica vettoriale. Tuttavia, le immagini traslucide (parzialmente trasparenti) possono essere renderizzate come un insieme di pixel completamente trasparenti e completamente opachi. Tali immagini sono chiamate maschere.

La libreria Aspose.Page per C++ offre un metodo che aggiunge l’immagine trasparente al documento PS. Per quanto riguarda la pittura di grafica vettoriale: forme o testo, offriamo la “pseudo-trasparenza”.

La “pseudo-trasparenza” è un processo di schiarimento dei colori con una componente alfa inferiore a 255. Si ottiene miscelando specificamente le componenti Rosso, Verde e Blu con la componente alfa.

La “pseudo-trasparenza”, ovviamente, non ci permette di vedere il livello colorato inferiore da sotto il livello trasparente superiore, ma crea un’illusione di trasparenza se il livello inferiore è bianco.

Aggiunta di un’immagine trasparente in un documento PS

Come accennato in precedenza, le immagini trasparenti possono essere aggiunte al documento PS come maschera e la libreria Aspose.Page per C++ offre a questo scopo il metodo AddTransparentImage(). Questo metodo riconosce se l’immagine è completamente opaca, trasparente o traslucida. Se è completamente opaca, viene aggiunta come immagine opaca nel metodo AddImage(), se è completamente trasparente, non viene aggiunta al documento, mentre se è traslucida, viene aggiunta come maschera di immagine PostScript.

Nell’esempio seguente mostriamo la differenza tra l’aggiunta di un’immagine trasparente in un documento PS con AddImage() e AddTransparentImage(). Per visualizzare l’immagine traslucida bianca, impostiamo il colore di sfondo della pagina su un colore diverso dal bianco.

Per aggiungere un’immagine a un nuovo PsDocument con la libreria Aspose.Page per C++, in questo esempio seguiamo i seguenti passaggi:

  1. Creiamo un flusso di output per il file PS risultante.
  2. Creiamo un oggetto PsSaveOptions con le opzioni predefinite. Se necessario, modifichiamo il colore di sfondo.
  3. Creiamo un PsDocument a pagina singola con un flusso di output e opzioni di salvataggio già creati.
  4. Creiamo un nuovo stato grafico.
  5. Creiamo una Bitmap dal file immagine.
  6. Applichiamo la trasformazione necessaria per l’immagine. 7. Aggiungere l’immagine a PsDocument come immagine completamente opaca (utilizzando il metodo AddImage()) se siamo sicuri che l’immagine sia opaca, oppure aggiungerne una come immagine trasparente (utilizzando il metodo AddTransparentImage()) se non siamo sicuri che l’immagine sia opaca.
  7. Uscire dallo stato grafico corrente e tornare al livello superiore.
  8. Chiudere la pagina.
  9. Salvare il documento.
 1    // The path to the documents directory.
 2    System::String dataDir = RunExamples::GetDataDir_WorkingWithTransparency();
 3    
 4    //Create output stream for PostScript document
 5    {
 6        System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"AddTransparentImage_outPS.ps", System::IO::FileMode::Create);
 7        // Clearing resources under 'using' statement
 8        System::Details::DisposeGuard<1> __dispose_guard_2({ outPsStream});
 9        // ------------------------------------------
10        
11        try
12        {
13            //Create save options with A4 size
14            System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>();
15            //Set page's background color to see white image on it's own transparent background
16            options->set_BackgroundColor(System::Drawing::Color::FromArgb(211, 8, 48));
17            
18            // Create new 1-paged PS Document
19            System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, false);
20            
21            
22            document->WriteGraphicsSave();
23            document->Translate(20.0f, 100.0f);
24            
25            //Create bitmap from translucent image file
26            {
27                System::SharedPtr<System::Drawing::Bitmap> image = System::MakeObject<System::Drawing::Bitmap>(dataDir + u"mask1.png");
28                // Clearing resources under 'using' statement
29                System::Details::DisposeGuard<1> __dispose_guard_0({ image});
30                // ------------------------------------------
31                
32                try
33                {
34                    //Add this image to document as usual opaque RGB image
35                    document->DrawImage(image, System::MakeObject<System::Drawing::Drawing2D::Matrix>(1.0f, 0.0f, 0.0f, 1.0f, 100.0f, 0.0f), System::Drawing::Color::Empty);
36                }
37                catch(...)
38                {
39                    __dispose_guard_0.SetCurrentException(std::current_exception());
40                }
41            }
42            
43            //Again create bitmap from the same image file
44            {
45                System::SharedPtr<System::Drawing::Bitmap> image = System::MakeObject<System::Drawing::Bitmap>(dataDir + u"mask1.png");
46                // Clearing resources under 'using' statement
47                System::Details::DisposeGuard<1> __dispose_guard_1({ image});
48                // ------------------------------------------
49                
50                try
51                {
52                    //Add this image to document as transparent image image
53                    document->DrawTransparentImage(image, System::MakeObject<System::Drawing::Drawing2D::Matrix>(1.0f, 0.0f, 0.0f, 1.0f, 350.0f, 0.0f), 255);
54                }
55                catch(...)
56                {
57                    __dispose_guard_1.SetCurrentException(std::current_exception());
58                }
59            }
60            
61            document->WriteGraphicsRestore();
62            
63            //Close current page
64            document->ClosePage();
65            
66            //Save the document
67            document->Save();
68        }
69        catch(...)
70        {
71            __dispose_guard_2.SetCurrentException(std::current_exception());
72        }
73    }

Vedi come lavorare con la trasparenza in un documento PS in .NET o Java.

Il risultato dell’esecuzione di questo codice è il seguente:

Aggiungi immagine trasparente

Aggiunta di un oggetto grafico vettoriale trasparente

In precedenza abbiamo scritto che la libreria Aspose.Page per C++ utilizza un algoritmo di trasparenza per forme e testo trasparenti, che abbiamo chiamato “pseudo-trasparenza”. Nell’esempio seguente mostriamo una differenza tra due forme dipinte con lo stesso colore, ma nella prima forma senza il componente Alpha e nel secondo caso con il componente Alpha.

 1    // The path to the documents directory.
 2    System::String dataDir = RunExamples::GetDataDir_WorkingWithTransparency();
 3    
 4    //Create output stream for PostScript document
 5    {
 6        System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"ShowPseudoTransparency_outPS.ps", System::IO::FileMode::Create);
 7        // Clearing resources under 'using' statement
 8        System::Details::DisposeGuard<1> __dispose_guard_0({ outPsStream});
 9        // ------------------------------------------
10        
11        try
12        {
13            //Create save options with A4 size
14            System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>();
15            
16            // Create new 1-paged PS Document
17            System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, false);
18            
19            float offsetX = 50.0f;
20            float offsetY = 100.0f;
21            float width = 200.0f;
22            float height = 100.0f;
23            
24            ///////////////////////////////// Create rectangle with opaque gradient fill /////////////////////////////////////////////////////////
25            System::SharedPtr<System::Drawing::Drawing2D::GraphicsPath> path = System::MakeObject<System::Drawing::Drawing2D::GraphicsPath>();
26            path->AddRectangle(System::Drawing::RectangleF(offsetX, offsetY, width, height));
27            
28            System::SharedPtr<System::Drawing::Drawing2D::LinearGradientBrush> opaqueBrush = System::MakeObject<System::Drawing::Drawing2D::LinearGradientBrush>(System::Drawing::RectangleF(0.0f, 0.0f, 200.0f, 100.0f), System::Drawing::Color::FromArgb(0, 0, 0), System::Drawing::Color::FromArgb(40, 128, 70), 0.f);
29            System::SharedPtr<System::Drawing::Drawing2D::Matrix> brushTransform = System::MakeObject<System::Drawing::Drawing2D::Matrix>(width, 0.0f, 0.0f, height, offsetX, offsetY);
30            opaqueBrush->set_Transform(brushTransform);
31            System::SharedPtr<GradientBrush> gradientBrush = System::MakeObject<GradientBrush>(opaqueBrush);
32            gradientBrush->set_WrapMode(System::Drawing::Drawing2D::WrapMode::Clamp);
33            
34            document->SetPaint(gradientBrush);
35            document->Fill(path);
36            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
37            
38            offsetX = 350.0f;
39            
40            ///////////////////////////////// Create rectangle with translucent gradient fill ///////////////////////////////////////////////////
41            //Create graphics path from the first rectangle
42            path = System::MakeObject<System::Drawing::Drawing2D::GraphicsPath>();
43            path->AddRectangle(System::Drawing::RectangleF(offsetX, offsetY, width, height));
44            
45            //Create linear gradient brush colors which transparency are not 255, but 150 and 50. So it are translucent.
46            System::SharedPtr<System::Drawing::Drawing2D::LinearGradientBrush> translucentBrush = System::MakeObject<System::Drawing::Drawing2D::LinearGradientBrush>(System::Drawing::RectangleF(0.0f, 0.0f, width, height), System::Drawing::Color::FromArgb(150, 0, 0, 0), System::Drawing::Color::FromArgb(50, 40, 128, 70), 0.f);
47            //Create a transform for brush.
48            brushTransform = System::MakeObject<System::Drawing::Drawing2D::Matrix>(width, 0.0f, 0.0f, height, offsetX, offsetY);
49            //Set transform
50            translucentBrush->set_Transform(brushTransform);
51            //Create GradientBrush object containing the linear gradient brush
52            gradientBrush = System::MakeObject<GradientBrush>(translucentBrush);
53            gradientBrush->set_WrapMode(System::Drawing::Drawing2D::WrapMode::Clamp);
54            //Set paint
55            document->SetPaint(gradientBrush);
56            //Fill the rectangle
57            document->Fill(path);
58            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
59            
60            //Close current page
61            document->ClosePage();
62            
63            //Save the document
64            document->Save();
65        }
66        catch(...)
67        {
68            __dispose_guard_0.SetCurrentException(std::current_exception());
69        }
70    }

Vedi come lavorare con la trasparenza nei documenti PS in .NET o Java.

Il risultato dell’esecuzione di questo codice viene visualizzato come

Mostra pseudo-trasparenza

È possibile scaricare esempi e file di dati da GitHub.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.