Frequently Asked Question
Draw bitmap with transparent background (Windows)
最后更新 11 个月前
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(); }