Browse Source

Samples - PDFToImage用magick读取byte[]

weixiangjie 10 months ago
parent
commit
ba6726f8db

+ 16 - 17
Demo/Examples/Samples/PDFToImage/CS/PDFToImage.cs

@@ -2,14 +2,15 @@
 using ComPDFKit.PDFPage;
 using System;
 using System.Collections.Generic;
+using System.Drawing;
 using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows;
-using System.Windows.Media;
-using System.Windows.Media.Imaging;
+
 using ComPDFKit.Import;
+using ImageMagick;
 
 namespace PDFToImageTest
 {
@@ -44,16 +45,6 @@ namespace PDFToImageTest
             document.PdfToImage("1", outputPath);
         }
 
-        static private void SaveWriteableBitmapAsPng(WriteableBitmap writeableBitmap, string fileName)
-        {
-            using (FileStream stream = new FileStream(fileName, FileMode.Create))
-            {
-                PngBitmapEncoder encoder = new PngBitmapEncoder();
-                encoder.Frames.Add(BitmapFrame.Create(writeableBitmap));
-                encoder.Save(stream);
-            }
-        }
-
         /// <summary>
         /// Convert PDF to image
         /// </summary>
@@ -63,15 +54,23 @@ namespace PDFToImageTest
             {
                 CPDFPage pdfPage = document.PageAtIndex(i, true);
                 CSize cSize = document.GetPageSize(0);
-                Size pageSize = new Size(cSize.width, cSize.height);
+                System.Drawing.Size pageSize = new System.Drawing.Size((int)cSize.width, (int)cSize.height);
                 CRect pageRect = new CRect(0, (int)(pageSize.Height / 72.0 * 96), (int)(pageSize.Width / 72.0 * 96), 0);
                 byte[] bmpData = new byte[(int)(pageRect.width() * pageRect.height() * (96 / 72.0) * (96 / 72.0) * 4)];
                 pdfPage.RenderPageBitmapWithMatrix((float)(96 / 72.0), pageRect, 0xFFFFFFFF, bmpData, 0, true);
-                WriteableBitmap writeableBitmap = new WriteableBitmap((int)pageRect.width(), (int)pageRect.height(), 96, 96, PixelFormats.Bgra32, null);
-
-                writeableBitmap.WritePixels(new Int32Rect(0, 0, (int)pageRect.width(), (int)pageRect.height()), bmpData, writeableBitmap.BackBufferStride, 0);
                 var path = outputPath + @"\PDFToImageTest" + i + ".png";
-                SaveWriteableBitmapAsPng(writeableBitmap, path);
+
+                var settings = new MagickReadSettings()
+                {
+                    Format = MagickFormat.Bgra,
+                    Width = (int)pageRect.width(),
+                    Height = (int)pageRect.height()
+                };
+                using (var image = new MagickImage(bmpData,settings))
+                {
+                    image.Format = MagickFormat.Png;
+                    image.Write(path);
+                }
                 Console.WriteLine("Png image saved in {0}", path);
             }
             return false;

+ 3 - 0
Demo/Examples/Samples/PDFToImage/CS/PDFToImageTest.csproj

@@ -95,5 +95,8 @@
       <Name>ComPDFKit.Desk</Name>
     </ProjectReference>
   </ItemGroup>
+  <ItemGroup>
+    <PackageReference Include="Magick.NET-Q8-AnyCPU" Version="13.6.0" />
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>