private async void DisplayImage(int pageIndex, double dpi) { if (MyPdfDocument == null) { return; } MyDpi = dpi; using (Windows.Data.Pdf.PdfPage page = MyPdfDocument.GetPage((uint)pageIndex)) { double h = page.Size.Height; var options = new Windows.Data.Pdf.PdfPageRenderOptions(); options.DestinationHeight = (uint)Math.Round(page.Size.Height * (dpi / 96.0), MidpointRounding.AwayFromZero); tbDpi.Text = $"dpi : {dpi.ToString()}"; tbHeight.Text = $"縦ピクセル : {options.DestinationHeight.ToString()}"; using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) { await page.RenderToStreamAsync(stream, options); BitmapImage image = new BitmapImage(); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; image.StreamSource = stream.AsStream();//using System.IOがないとエラーになる image.EndInit(); MyImage.Source = image; MyImage.Width = image.PixelWidth; MyImage.Height = image.PixelHeight; } } }
private async void ButtonOpenScale_Click(object sender, RoutedEventArgs e) { var file = await Windows.Storage.StorageFile.GetFileFromPathAsync(@"D:\ブログ用\1708_04.pdf"); try { PdfDocument = await Windows.Data.Pdf.PdfDocument.LoadFromFileAsync(file); } catch (Exception) { } if (PdfDocument != null) { using (Windows.Data.Pdf.PdfPage page = PdfDocument.GetPage(0)) { BitmapImage image = new BitmapImage(); using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) { var options = new Windows.Data.Pdf.PdfPageRenderOptions(); options.DestinationHeight = 1000; await page.RenderToStreamAsync(stream, options); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; image.StreamSource = stream.AsStream(); image.EndInit(); } ImagePDF.Source = image; } } }
/// <summary> /// jpeg画像で保存 /// </summary> /// <param name="dpi">PDFファイルを読み込む時のDPI</param> /// <param name="directory">保存フォルダ</param> /// <param name="fileName">保存名</param> /// <param name="pageIndex">保存するPDFのページ</param> /// <param name="quality">jpegの品質min0、max100</param> /// <param name="keta">保存名につける連番0埋めの桁数</param> /// <returns></returns> private async Task SaveSub2(double dpi, string directory, string fileName, int pageIndex, int quality, int keta) { using (Windows.Data.Pdf.PdfPage page = MyPdfDocument.GetPage((uint)pageIndex)) { //指定されたdpiを元に画像サイズ指定、四捨五入 var options = new Windows.Data.Pdf.PdfPageRenderOptions(); options.DestinationHeight = (uint)Math.Round(page.Size.Height * (dpi / 96.0), MidpointRounding.AwayFromZero); using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) { await page.RenderToStreamAsync(stream, options); BitmapImage image = new BitmapImage(); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; image.StreamSource = stream.AsStream(); image.EndInit(); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.QualityLevel = quality; encoder.Frames.Add(BitmapFrame.Create(image)); pageIndex++; string renban = pageIndex.ToString("d" + keta); using (var fileStream = new FileStream( System.IO.Path.Combine(directory, fileName) + "_" + renban + ".jpg", FileMode.Create, FileAccess.Write)) { encoder.Save(fileStream); } } } }
/// <summary> /// 表示 /// </summary> /// <param name="pageNumber">指定ページ</param> /// <param name="displayDpi">指定dpi、通常なら96</param> private async void PageLoad(uint pageNumber, double displayDpi) { if (PdfDocument == null) { return; } BitmapImage image = new BitmapImage(); var rate = displayDpi / 96.0;//表示倍率みたいなもの using (Windows.Data.Pdf.PdfPage page = PdfDocument.GetPage(pageNumber)) { //指定されたdpiを元にレンダーサイズ指定、四捨五入 double h = page.Size.Height; var options = new Windows.Data.Pdf.PdfPageRenderOptions(); options.DestinationHeight = (uint)Math.Round(h * rate, MidpointRounding.AwayFromZero); using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) { await page.RenderToStreamAsync(stream, options); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; image.StreamSource = stream.AsStream(); image.EndInit(); } } MyImage.Source = image; MyImage.Width = image.PixelWidth; MyImage.Height = image.PixelHeight; }
private async Task <int> SaveSubTask(double rate, string filePath, uint i) { using (Windows.Data.Pdf.PdfPage page = PdfDocument.GetPage((uint)i)) { //指定されたdpiを元にレンダーサイズ指定、四捨五入 double h = page.Size.Height; var options = new Windows.Data.Pdf.PdfPageRenderOptions(); options.DestinationHeight = (uint)Math.Round(h * rate, MidpointRounding.AwayFromZero); using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) { await page.RenderToStreamAsync(stream, options); BitmapImage image = new BitmapImage(); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; image.StreamSource = stream.AsStream(); image.EndInit(); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.QualityLevel = 85; encoder.Frames.Add(BitmapFrame.Create(image)); using (var fileStream = new FileStream(filePath + "_" + i + ".jpg", FileMode.Create, FileAccess.Write)) { encoder.Save(fileStream); } } } return((int)i); }
private async void MakeImageAndSave(double displayDpi) { if (PdfDocument == null) { return; } string filePath = @"D:\ブログ用\1708_04"; var rate = displayDpi / 96.0;//表示倍率みたいなもの for (uint i = 0; i < PdfDocument.PageCount; i++) { using (Windows.Data.Pdf.PdfPage page = PdfDocument.GetPage(i)) { //指定されたdpiを元にレンダーサイズ指定、四捨五入 double h = page.Size.Height; var options = new Windows.Data.Pdf.PdfPageRenderOptions(); options.DestinationHeight = (uint)Math.Round(h * rate, MidpointRounding.AwayFromZero); using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) { await page.RenderToStreamAsync(stream, options); BitmapImage image = new BitmapImage(); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; image.StreamSource = stream.AsStream(); image.EndInit(); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.QualityLevel = 85; encoder.Frames.Add(BitmapFrame.Create(image)); using (var fileStream = new FileStream(filePath + "_" + i + ".jpg", FileMode.Create, FileAccess.Write)) { encoder.Save(fileStream); } } } } }
private BitmapImage RenderPage(double dpi, int pageIndex) { BitmapImage image = new BitmapImage(); using (Windows.Data.Pdf.PdfPage page = MyPdfDocument.GetPage((uint)pageIndex)) { //指定されたdpiを元に画像サイズ指定、四捨五入 var options = new Windows.Data.Pdf.PdfPageRenderOptions(); options.DestinationHeight = (uint)Math.Round(page.Size.Height * (dpi / 96.0), MidpointRounding.AwayFromZero); using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) { page.RenderToStreamAsync(stream, options); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; image.StreamSource = stream.AsStream(); image.EndInit(); } } return(image); }