Draw WriteableBitmap to Pdf

Hello,

Recently I need to draw an image on a Pdf-Document. Everything works fine, as long as I load the Image from a File:
    Stream imgStream;
    imgStream = await imgFile.OpenStreamForReadAsync();
    var pdfBmp = new PdfBitmap(imgStream);
What I'm trying now is Drawing an Image, I have as WriteableBitmap.
In your Knowledgebase it's done quiet easy:
    var pdfBmp = new PdfBitmap(wb.PixelBuffer.AsStream());
But in every scenario I tried the new PdfBitmap is Size of 0

These are my tested scenarios (all of them without success)
    using(var syncStream = new MemoryStream())
        await wb.PixelBuffer.AsStream().CopyToAsync(asyncStream);
        var pdfBmp = new PdfBitmap(asyncStream);

    var randAccStream = wb.PixelBuffer.AsStream().AsRandomAccessStream();
    var wb = new WriteableBitmap(1,1);
    await wb.SetSource(randAccStream);
    var stream = wb.PixelBuffer.AsStream();
    var pdfBmp = new PdfBitmap(stream);

    using(var stream = wb.Image.PixelBuffer.AsStream())
        var pdfBmp = new PdfBitmap(stream);

    using(InMemoryRandomAccessStream memRandAccStream = new InMemoryRandomAccessStream);
        Stream pixelStream = img.Image.PixelBuffer.AsStream();
        byte[] pixels = new byte[pixelStream.Lenght];
        await pixelStream.ReadAsync(pixels, 0, pixels.Lenght);
        encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
            (uint)wb.PixelWidth, (uitn)wb.PixelHeight, 96.0, 96.0, pixels);
        await encoder.Flush();
        var wb = new WriteableBitmap(1,1);
        await wb.SetSourceAsync(memRandAccStream);
        var stream = wb.PixelBuffer.AsStream();
        var pdfBmp = new PdfBitmap(stream);

and quiet sure, some more.


2 Replies

SL Sowmiya Loganathan Syncfusion Team February 15, 2018 12:51 PM UTC

Hi Raymond, 

Thank you for the code snippet. 

We have created the sample to draw an image on a PDF document using WritableBitmap. We can generate the blank image PDF document and image file with the PDF document. 

Please find the below code snippet to generate PDF document with blank image: 

            // Create a new PDF document. 
            PdfDocument doc = new PdfDocument(); 
 
            //Add a page to the document. 
            PdfPage page = doc.Pages.Add(); 
 
            // Create An Instance of WriteableBitmap object   
            WriteableBitmap writeableBitmap = new WriteableBitmap(300, 300); 
            
            IRandomAccessStream stream = new InMemoryRandomAccessStream(); 
            BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream); 
            // Get pixels of the WriteableBitmap object  
            Stream pixelStream = writeableBitmap.PixelBuffer.AsStream(); 
            byte[] pixels = new byte[pixelStream.Length]; 
            await pixelStream.ReadAsync(pixels, 0, pixels.Length); 
 
            // Save the image file with jpg extension.  
            encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)writeableBitmap.PixelWidth, (uint)writeableBitmap.PixelHeight, 96.0, 96.0, pixels); 
            await encoder.FlushAsync(); 
 
            //Create PDF graphics for the page 
            PdfGraphics graphics = page.Graphics; 
 
            PdfBitmap image = new PdfBitmap(stream.AsStream()); 
 
            //Draw the image 
            graphics.DrawImage(image, 0, 0); 

Please find the below code snippet to generate PDF with image file: 

            // Create a new PDF document. 
            PdfDocument doc = new PdfDocument(); 
 
            //Add a page to the document. 
            PdfPage page = doc.Pages.Add(); 
 
            // Create An Instance of WriteableBitmap object   
            WriteableBitmap writeableBitmap = new WriteableBitmap(300, 300); 
            StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Autumn Leaves.jpg")); 
            using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read)) 
            { 
                // set the source for WriteableBitmap   
                await writeableBitmap.SetSourceAsync(fileStream); 
            } 
            //Save the writeableBitmap object to JPG Image file  
            FileSavePicker picker = new FileSavePicker(); 
            picker.FileTypeChoices.Add("JPG File", new List<string>() { ".jpg" }); 
            StorageFile savefile = await picker.PickSaveFileAsync(); 
            if (savefile == null) 
                return; 
            IRandomAccessStream stream = new InMemoryRandomAccessStream();// await savefile.OpenAsync(FileAccessMode.ReadWrite); 
            BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream); 
            // Get pixels of the WriteableBitmap object  
            Stream pixelStream = writeableBitmap.PixelBuffer.AsStream(); 
            byte[] pixels = new byte[pixelStream.Length]; 
            await pixelStream.ReadAsync(pixels, 0, pixels.Length); 
            // Save the image file with jpg extension.  
            encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)writeableBitmap.PixelWidth, (uint)writeableBitmap.PixelHeight, 96.0, 96.0, pixels); 
            await encoder.FlushAsync(); 
 
            //Create PDF graphics for the page 
            PdfGraphics graphics = page.Graphics; 
 
            PdfBitmap image = new PdfBitmap(stream.AsStream()); 
 
            //Draw the image 
            graphics.DrawImage(image, 0, 0); 

Please find the sample from below link : 

Please try the above sample and let us know if it solves the issue. If not It not, please let us know further details regarding your requirements. It will be helpful for us to analyze further and assist you better.  

Regards, 
Sowmiya L 



RO Raymond Osterbrink February 15, 2018 01:24 PM UTC

Hello Sowmiya,

Thank you for the response, your above example solved the problem as expected.

Regards, 
Raymond Osterbrink.

Loader.
Up arrow icon