How to convert JPEG image from PNG image and draw those JPEG image into PDF document?
At present, we do not have native support for drawing PNG image on PDF in Xamarin forms platform. So, we can convert the PNG image to JPEG and drawing the JPEG image into PDF document in Xamarin platform. We have achieved the PNG image to JPEG conversion by using the Dependency service. We have implemented the PNG to JPEG conversion in Xamarin.Android, Xamarin.IOS and Xamarin.UWP. Please find the sample and code snippet below,
Convert PNG to JPEG using Xamarin.Android:
Convert the PNG image to JPEG in Xamarin.Android by using below code snippet.
C#
public Stream ConvertPngToJpeg(Stream stream) { //Convert image stream into byte array byte[] image = new byte[stream.Length]; stream.Read(image, 0, image.Length); //Load the bitmap Bitmap resultBitmap = BitmapFactory.DecodeByteArray(image, 0, image.Length); //Create memory stream MemoryStream outStream = new MemoryStream(); //Save the image as Jpeg resultBitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, outStream); //Return the Jpeg image as stream return outStream; }
Convert PNG to JPEG using Xamarin.IOS:
Convert the PNG image to JPEG in Xamarin.IOS by using below code snippet.
C#
Stream ISave.ConvertPngToJpeg(Stream stream) { //Convert image stream into byte array byte[] image = new byte[stream.Length]; stream.Read(image, 0, image.Length); //Load the image UIKit.UIImage images = new UIImage(Foundation.NSData.FromArray(image)); //Save the image as Jpeg byte[] bytes = images.AsJPEG(100).ToArray(); //Store the byte array into memory stream Stream imgStream = new MemoryStream(bytes); //Return the Jpeg image as stream return imgStream; }
Convert PNG to JPEG using Xamarin.UWP:
Convert the PNG image to JPEG in Xamarin.UWP by using below code snippet.
C#
public async Task<Stream> ConvertPngToJpeg2(Stream s) { byte[] resultArray = null; //Convert stream into byte array byte[] image = new byte[s.Length]; s.Read(image, 0, image.Length); //Create An Instance of WriteableBitmap object WriteableBitmap resultBitmap = new WriteableBitmap(1, 1); using (IRandomAccessStream ms = new InMemoryRandomAccessStream()) { await ms.WriteAsync(image.AsBuffer()); ms.Seek(0); //Set the source for WriteableBitmap resultBitmap.SetSource(ms); } //Get the image data using (IRandomAccessStream ms = new InMemoryRandomAccessStream()) { try { byte[] bytes; // Open a stream to copy the image contents to the WriteableBitmap's pixel buffer using (Stream stream = resultBitmap.PixelBuffer.AsStream()) { bytes = new byte[(uint)stream.Length]; await stream.ReadAsync(bytes, 0, bytes.Length); } // Create an encoder with the Jpeg format BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, ms); // WriteableBitmap uses BGRA format encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)resultBitmap.PixelWidth, (uint)resultBitmap.PixelHeight, 96, 96, bytes); //Terminate the encoder bytes await encoder.FlushAsync(); resultArray = new byte[ms.AsStream().Length]; await ms.AsStream().ReadAsync(resultArray, 0, resultArray.Length); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); } } //Store the image into memory stream Stream imgStream = new MemoryStream(resultArray); //Return the Jpeg image as stream return imgStream; }
Draw the JPEG image into PDF document:
Draw the JPEG image into PDF document can be shown in the below code snippet.
C#
//Load the file as stream Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("ImageDrawing.Assets.Sample.pdf"); //Load a PDF document. PdfLoadedDocument doc = new PdfLoadedDocument(docStream); //Get first page from document PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage; //Create PDF graphics for the page PdfGraphics graphics = page.Graphics; //Load the image Stream imageStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("ImageDrawing.Assets.logo.png"); Stream stream = DependencyService.Get<ISave>().ConvertPngToJpeg(imageStream); //Load the image from the stream PdfBitmap image = new PdfBitmap(stream); //Draw the image graphics.DrawImage(image, 0, 0); MemoryStream memoryStream = new MemoryStream(); //Save the document into memory stream doc.Save(memoryStream); //close the documents doc.Close(true); //Save the stream into pdf file DependencyService.Get<ISave>().SaveAsync("sample.pdf", "application/pdf", memoryStream);
Sample link:
https://www.syncfusion.com/downloads/support/directtrac/general/ze/ImageDrawing-1680216731