Knowledge Base - Draw bitmap with transparent background (Windows)

You can use the below code to draw a bitmap with transparent background.

            StorageFile imageFile = await ApplicationData.Current.LocalFolder.GetFileAsync("test.png");
            StorageFile pdfFile = await ApplicationData.Current.LocalFolder.GetFileAsync("Test.pdf");
            PDFDoc doc = new PDFDoc();
            WriteableBitmap bp = new WriteableBitmap(1, 1);
            using (var stream = await imageFile.OpenAsync(FileAccessMode.Read))
            {
                await bp.SetSourceAsync(stream);
            }
            
            using (var stream = await pdfFile.OpenAsync(FileAccessMode.ReadWrite))
            {
                doc.Open(stream, "");
                PDFDocImage img = doc.NewImage(bp, true);
                var page = doc.GetPage(0);
                var content = new PDFPageContent();
                content.GSSave();             
                var pgImg = page.AddResImage(img);
                if (pgImg != null)
                {
                    PDFPath path = new PDFPath();
                    path.MoveTo(0, 0);
                    path.LineTo(bp.PixelWidth, 0);
                    path.LineTo(bp.PixelWidth, bp.PixelHeight);
                    path.LineTo(0, bp.PixelHeight);
                    path.Close();


                    PDFDocGState dgs = doc.NewGState();
                    dgs.SetFillAlpha(0);
                    content.GSSet(page.AddResGState(dgs));
                    content.SetFillColor(0xFFFFFF);                  
                    content.FillPath(path, true);

                    content.GSRestore();
                    PDFMatrix m = new PDFMatrix(bp.PixelWidth, bp.PixelHeight, 0, 0);
                    content.GSSetMatrix(m);                   
                    content.DrawImage(pgImg);
                    page.AddContent(content, false);
                }
                doc.Save();
            }
Applies To

RadaeePDF SDK for Windows 10

Details

Created : 2018-02-07 09:42:34, Last Modified : 2018-02-07 09:47:01