|
@@ -24,6 +24,8 @@ using System.Linq;
|
|
|
using Syncfusion.Pdf.Graphics;
|
|
|
using Syncfusion.Pdf;
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
+using System.Text;
|
|
|
+using Encoder = System.Drawing.Imaging.Encoder;
|
|
|
|
|
|
namespace InABox.WPF
|
|
|
{
|
|
@@ -240,21 +242,22 @@ namespace InABox.WPF
|
|
|
{
|
|
|
return BitmapImageFromBytes(Convert.FromBase64String(base64));
|
|
|
}
|
|
|
- public static BitmapImage? BitmapImageFromBytes(byte[] data)
|
|
|
+ public static BitmapImage? BitmapImageFromBytes(byte[]? data)
|
|
|
{
|
|
|
+
|
|
|
+ if (data?.Any() != true)
|
|
|
+ return null;
|
|
|
+
|
|
|
var imageSource = new BitmapImage();
|
|
|
- if(data.Length > 0)
|
|
|
+ using (var ms = new MemoryStream(data))
|
|
|
{
|
|
|
- using (var ms = new MemoryStream(data))
|
|
|
- {
|
|
|
- imageSource.BeginInit();
|
|
|
- imageSource.StreamSource = ms;
|
|
|
- imageSource.CacheOption = BitmapCacheOption.OnLoad;
|
|
|
- imageSource.EndInit();
|
|
|
- }
|
|
|
- return imageSource;
|
|
|
+ imageSource.BeginInit();
|
|
|
+ imageSource.StreamSource = ms;
|
|
|
+ imageSource.CacheOption = BitmapCacheOption.OnLoad;
|
|
|
+ imageSource.EndInit();
|
|
|
}
|
|
|
- return null;
|
|
|
+ return imageSource;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
public static BitmapImage? BitmapImageFromStream(Stream data)
|
|
@@ -830,6 +833,12 @@ namespace InABox.WPF
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
+ public static bool IsPdf(byte[] data)
|
|
|
+ {
|
|
|
+ var pdfBytes = Encoding.ASCII.GetBytes("%PDF-");
|
|
|
+ return data.Take(pdfBytes.Length).SequenceEqual(pdfBytes);
|
|
|
+ }
|
|
|
|
|
|
public static byte[] GetPDFThumbnail(byte[] pdfData, int width, int height)
|
|
|
{
|
|
@@ -839,6 +848,15 @@ namespace InABox.WPF
|
|
|
image.Save(stream, ImageFormat.Jpeg);
|
|
|
return stream.ToArray();
|
|
|
}
|
|
|
+
|
|
|
+ public static byte[] PDFToBitmap(byte[] pdfData, int page)
|
|
|
+ {
|
|
|
+ PdfLoadedDocument loadeddoc = new PdfLoadedDocument(pdfData);
|
|
|
+ Bitmap image = loadeddoc.ExportAsImage(page, new ImageExportSettings() { KeepAspectRatio = true });
|
|
|
+ MemoryStream stream = new MemoryStream();
|
|
|
+ image.Save(stream, ImageFormat.Jpeg);
|
|
|
+ return stream.ToArray();
|
|
|
+ }
|
|
|
|
|
|
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
|
|
|
public static extern bool DeleteObject(IntPtr hObject);
|