|
// 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); |
|
// 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); |