/
XImage.cs
1390 lines (1276 loc) · 45.6 KB
/
XImage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// PDFsharp - A .NET library for processing PDF
// See the LICENSE file in the solution root for more information.
#if CORE
// Nothing to import.
#endif
#if GDI
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using PdfSharp.Internal;
#endif
#if WPF
using System.IO;
using System.Text;
using System.Windows.Media.Imaging;
#endif
#if UWP
using Windows.UI.Xaml.Media.Imaging;
#endif
using PdfSharp.Drawing.Internal;
using PdfSharp.Pdf.IO;
using PdfSharp.Pdf.Advanced;
namespace PdfSharp.Drawing
{
[Flags]
enum XImageState
{
UsedInDrawingContext = 0x00000001,
StateMask = 0x0000FFFF,
}
/// <summary>
/// Defines an object used to draw image files (bmp, png, jpeg, gif) and PDF forms.
/// An abstract base class that provides functionality for the Bitmap and Metafile descended classes.
/// </summary>
public class XImage : IDisposable
{
// The hierarchy is adapted to WPF
//
// XImage <-- ImageSource
// XForm
// PdfForm
// XBitmapSource <-- BitmapSource
// XBitmapImage <-- BitmapImage
// ???
//public bool Disposed
//{
// get { return _disposed; }
// set { _disposed = value; }
//}
/// <summary>
/// Initializes a new instance of the <see cref="XImage"/> class.
/// </summary>
protected XImage()
{ }
#if GDI || CORE || WPF
/// <summary>
/// Initializes a new instance of the <see cref="XImage"/> class from an image read by ImageImporter.
/// </summary>
/// <param name="image">The image.</param>
/// <exception cref="System.ArgumentNullException">image</exception>
XImage(ImportedImage image)
{
_importedImage = image ?? throw new ArgumentNullException(nameof(image));
Initialize();
}
#endif
#if GDI
/// <summary>
/// Initializes a new instance of the <see cref="XImage"/> class from a GDI+ image.
/// </summary>
XImage(Image image)
{
_gdiImage = image;
#if WPF // Is defined in hybrid build.
_wpfImage = ImageHelper.CreateBitmapSource(image);
#endif
Initialize();
}
#endif
#if WPF
/// <summary>
/// Initializes a new instance of the <see cref="XImage"/> class from a WPF image.
/// </summary>
XImage(BitmapSource image)
{
_wpfImage = image;
Initialize();
}
#endif
#if WPF
XImage(Uri uri)
{
//var uriSource = new Uri(@"/WpfApplication1;component/Untitled.png", UriKind.Relative); foo.Source = new BitmapImage(uriSource);
_wpfImage = BitmapFromUri(uri);
//throw new NotImplementedException("XImage from Uri.");
// WPF
//Image finalImage = new Image();
//finalImage.Width = 80;
//...BitmapImage logo = new BitmapImage()
//logo.BeginInit();logo.UriSource = new Uri("pack://application:,,,/ApplicationName;component/Resources/logo.png");
//logo.EndInit();
//...finalImage.Source = logo;
}
/// <summary>
/// Creates an BitmapImage from URI. Sets BitmapCacheOption.OnLoad for WPF to prevent image file from being locked.
/// </summary>
public static BitmapImage BitmapFromUri(Uri uri)
{
#if true
// Using new BitmapImage(uri) will leave a lock on the file, leading to problems with temporary image files in server environments.
// We use BitmapCacheOption.OnLoad to prevent this lock.
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = uri;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
return bitmap;
#else
return new BitmapImage(uri);
#endif
}
#endif
#if UWP
/// <summary>
/// Initializes a new instance of the <see cref="XImage"/> class from a UWP image.
/// </summary>
XImage(BitmapSource image)
{
_wrtImage = image;
Initialize();
}
#endif
// Useful stuff here: http://stackoverflow.com/questions/350027/setting-wpf-image-source-in-code
XImage(string path)
{
#if !UWP
path = Path.GetFullPath(path);
if (!File.Exists(path))
throw new FileNotFoundException(PSSR.FileNotFound(path));
//throw new FileNotFoundException(PSSR.FileNotFound(path), path);
#endif
_path = path;
//FileStream file = new FileStream(filename, FileMode.Open);
//BitsLength = (int)file.Length;
//Bits = new byte[BitsLength];
//file.Read(Bits, 0, BitsLength);
//file.Close();
#if GDI
try
{
Lock.EnterGdiPlus();
_gdiImage = Image.FromFile(path);
}
finally { Lock.ExitGdiPlus(); }
#endif
#if WPF
_wpfImage = BitmapFromUri(new Uri(path));
#endif
#if true_
float vres = image.VerticalResolution;
float hres = image.HorizontalResolution;
SizeF size = image.PhysicalDimension;
int flags = image.Flags;
Size sz = image.Size;
GraphicsUnit units = GraphicsUnit.Millimeter;
RectangleF rect = image.GetBounds(ref units);
int width = image.Width;
#endif
Initialize();
}
XImage(Stream stream)
{
// Create a dummy unique path.
_path = "*" + Guid.NewGuid().ToString("B");
// TODO: Create a fingerprint of the bytes in the stream to identify identical images.
#if GDI
// Create a GDI+ image.
try
{
Lock.EnterGdiPlus();
_gdiImage = Image.FromStream(stream);
}
finally { Lock.ExitGdiPlus(); }
#endif
#if WPF
// Create a WPF BitmapImage.
BitmapImage bmi = new BitmapImage();
bmi.BeginInit();
bmi.StreamSource = stream;
bmi.EndInit();
_wpfImage = bmi;
#endif
#if true_
float vres = image.VerticalResolution;
float hres = image.HorizontalResolution;
SizeF size = image.PhysicalDimension;
int flags = image.Flags;
Size sz = image.Size;
GraphicsUnit units = GraphicsUnit.Millimeter;
RectangleF rect = image.GetBounds(ref units);
int width = image.Width;
#endif
// Must assign _stream before Initialize().
_stream = stream;
Initialize();
}
#if GDI
/// <summary>
/// Implicit conversion from Image to XImage.
/// </summary>
public static implicit operator XImage(Image image)
{
return new XImage(image);
}
/// <summary>
/// Conversion from Image to XImage.
/// </summary>
public static XImage FromGdiPlusImage(Image image)
{
return new XImage(image);
}
#endif
#if WPF
/// <summary>
/// Conversion from BitmapSource to XImage.
/// </summary>
public static XImage FromBitmapSource(BitmapSource image)
{
return new XImage(image);
}
#endif
#if UWP
/// <summary>
/// Conversion from BitmapSource to XImage.
/// </summary>
public static XImage FromBitmapSource(BitmapSource image)
{
return new XImage(image);
}
#endif
#if GDI || WPF
/// <summary>
/// Creates an image from the specified file.
/// </summary>
/// <param name="path">The path to a BMP, PNG, GIF, JPEG, TIFF, or PDF file.</param>
public static XImage FromFile(string path)
{
if (PdfReader.TestPdfFile(path) > 0)
return new XPdfForm(path);
return new XImage(path);
}
/// <summary>
/// Creates an image from the specified stream.<br/>
/// </summary>
/// <param name="stream">The stream containing a BMP, PNG, GIF, JPEG, TIFF, or PDF file.</param>
public static XImage FromStream(Stream stream)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
if (PdfReader.TestPdfFile(stream) > 0)
return new XPdfForm(stream);
return new XImage(stream);
}
#endif
#if CORE
/// <summary>
/// Creates an image from the specified file.
/// </summary>
/// <param name="path">The path to a BMP, PNG, JPEG, or PDF file.</param>
public static XImage FromFile(string path)
{
if (PdfReader.TestPdfFile(path) > 0)
return new XPdfForm(path);
var ii = ImageImporter.GetImageImporter();
var i = ii.ImportImage(path);
if (i == null)
throw new InvalidOperationException("Unsupported image format.");
var image = new XImage(i);
image._path = path;
return image;
}
/// <summary>
/// Creates an image from the specified stream.<br/>
/// </summary>
/// <param name="stream">The stream containing a BMP, PNG, JPEG, or PDF file.</param>
public static XImage FromStream(Stream stream)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
var ii = ImageImporter.GetImageImporter();
var i = ii.ImportImage(stream);
if (i == null)
throw new InvalidOperationException("Unsupported image format.");
XImage image = new XImage(i);
image._stream = stream;
return image;
}
#endif
#if GDI || WPF
/// <summary>
/// Creates an image from the specified file.
/// </summary>
/// <param name="path">The path to a BMP, PNG, GIF, JPEG, TIFF, or PDF file.</param>
/// <param name="platformIndependent">Uses an platform-independent implementation if set to true.
/// The platform-dependent implementation, if available, will support more image formats.</param>
public static XImage FromFile(string path, bool platformIndependent)
{
if (!platformIndependent)
return FromFile(path);
// TODO: Check PDF file.
var ii = ImageImporter.GetImageImporter();
var i = ii.ImportImage(path);
if (i == null)
throw new InvalidOperationException("Unsupported image format.");
var image = new XImage(i);
image._path = path;
return image;
}
/// <summary>
/// Creates an image from the specified stream.<br/>
/// </summary>
/// <param name="stream">The stream containing a BMP, PNG, GIF, JPEG, TIFF, or PDF file.</param>
/// <param name="platformIndependent">Uses an platform-independent implementation if set to true.
/// The platform-dependent implementation, if available, will support more image formats.</param>
public static XImage FromStream(Stream stream, bool platformIndependent)
{
if (!platformIndependent)
return FromStream(stream);
// TODO: Check PDF file.
if (stream == null)
throw new ArgumentNullException(nameof(stream));
var ii = ImageImporter.GetImageImporter();
var i = ii.ImportImage(stream);
if (i == null)
throw new InvalidOperationException("Unsupported image format.");
XImage image = new XImage(i);
image._stream = stream;
return image;
}
#endif
#if DEBUG
#if CORE || GDI || WPF
/// <summary>
/// Creates an image.
/// </summary>
/// <param name="image">The imported image.</param>
[Obsolete("THHO4THHO Internal test code.")]
internal static XImage FromImportedImage(ImportedImage image)
{
if (image == null)
throw new ArgumentNullException(nameof(image));
return new XImage(image);
}
#endif
#endif
/// <summary>
/// Tests if a file exist. Supports PDF files with page number suffix.
/// </summary>
/// <param name="path">The path to a BMP, PNG, GIF, JPEG, TIFF, or PDF file.</param>
public static bool ExistsFile(string path)
{
// Support for "base64:" pseudo protocol is a MigraDoc feature, currently completely implemented in MigraDoc files.
//if (path.StartsWith("base64:", StringComparison.Ordinal)) // The Image is stored in the string here, so the file exists.
// return true;
if (PdfReader.TestPdfFile(path) > 0)
return true;
#if !UWP
return File.Exists(path);
#else
return false;
#endif
}
internal XImageState XImageState { get; set; }
internal void Initialize()
{
#if CORE || GDI || WPF
if (_importedImage != null)
{
// In PDF there are two formats: JPEG and PDF bitmap.
if (_importedImage is ImportedImageJpeg)
_format = XImageFormat.Jpeg;
else
_format = XImageFormat.Png;
return;
}
#endif
#if GDI
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
if (_gdiImage != null)
{
// ImageFormat has no overridden Equals function.
string guid;
try
{
Lock.EnterGdiPlus();
guid = _gdiImage.RawFormat.Guid.ToString("B").ToUpper();
}
finally { Lock.ExitGdiPlus(); }
switch (guid)
{
case "{B96B3CAA-0728-11D3-9D7B-0000F81EF32E}": // memoryBMP
case "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}": // bmp
case "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}": // png
_format = XImageFormat.Png;
break;
case "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}": // jpeg
_format = XImageFormat.Jpeg;
break;
case "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}": // gif
_format = XImageFormat.Gif;
break;
case "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}": // tiff
_format = XImageFormat.Tiff;
break;
case "{B96B3CB5-0728-11D3-9D7B-0000F81EF32E}": // icon
_format = XImageFormat.Icon;
break;
case "{B96B3CAC-0728-11D3-9D7B-0000F81EF32E}": // emf
case "{B96B3CAD-0728-11D3-9D7B-0000F81EF32E}": // wmf
case "{B96B3CB2-0728-11D3-9D7B-0000F81EF32E}": // exif
case "{B96B3CB3-0728-11D3-9D7B-0000F81EF32E}": // photoCD
case "{B96B3CB4-0728-11D3-9D7B-0000F81EF32E}": // flashPIX
default:
throw new InvalidOperationException("Unsupported image format.");
}
return;
}
#endif
#if WPF
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
if (_wpfImage != null)
{
//string filename = GetImageFilename(_wpfImage);
// WPF treats all images as images.
// We give JPEG images a special treatment.
// Test if it's a JPEG.
bool isJpeg = IsJpeg; // TestJpeg(filename);
if (isJpeg)
{
_format = XImageFormat.Jpeg;
return;
}
string pixelFormat = _wpfImage.Format.ToString();
switch (pixelFormat)
{
case "Bgr32":
case "Bgra32":
case "Pbgra32":
_format = XImageFormat.Png;
break;
//case "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}": // jpeg
// format = XImageFormat.Jpeg;
// break;
//case "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}": // gif
case "BlackWhite":
case "Indexed1":
case "Indexed4":
case "Indexed8":
case "Gray8":
_format = XImageFormat.Gif;
break;
//case "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}": // tiff
// format = XImageFormat.Tiff;
// break;
//case "{B96B3CB5-0728-11D3-9D7B-0000F81EF32E}": // icon
// format = XImageFormat.Icon;
// break;
//case "{B96B3CAC-0728-11D3-9D7B-0000F81EF32E}": // emf
//case "{B96B3CAD-0728-11D3-9D7B-0000F81EF32E}": // wmf
//case "{B96B3CB2-0728-11D3-9D7B-0000F81EF32E}": // exif
//case "{B96B3CB3-0728-11D3-9D7B-0000F81EF32E}": // photoCD
//case "{B96B3CB4-0728-11D3-9D7B-0000F81EF32E}": // flashPIX
default:
Debug.Assert(false, "Unknown pixel format: " + pixelFormat);
_format = XImageFormat.Gif;
break;// throw new InvalidOperationException("Unsupported image format.");
}
}
#endif
}
#if WPF
/// <summary>
/// Gets the image filename.
/// </summary>
/// <param name="bitmapSource">The bitmap source.</param>
internal static string GetImageFilename(BitmapSource bitmapSource)
{
string filename = bitmapSource.ToString();
filename = UrlDecodeStringFromStringInternal(filename);
if (filename.StartsWith("file:///", StringComparison.Ordinal))
filename = filename.Substring(8); // Remove all 3 slashes!
else if (filename.StartsWith("file://", StringComparison.Ordinal))
filename = filename.Substring(5); // Keep 2 slashes (UNC path)
return filename;
}
static string UrlDecodeStringFromStringInternal(string s/*, Encoding e*/)
{
int length = s.Length;
StringBuilder result = new StringBuilder();
for (int i = 0; i < length; i++)
{
char ch = s[i];
if (ch == '+')
{
ch = ' ';
}
else if ((ch == '%') && (i < (length - 2)))
{
if ((s[i + 1] == 'u') && (i < (length - 5)))
{
int num3 = HexToInt(s[i + 2]);
int num4 = HexToInt(s[i + 3]);
int num5 = HexToInt(s[i + 4]);
int num6 = HexToInt(s[i + 5]);
if (((num3 < 0) || (num4 < 0)) || ((num5 < 0) || (num6 < 0)))
{
goto AddByte;
}
ch = (char)((((num3 << 12) | (num4 << 8)) | (num5 << 4)) | num6);
i += 5;
result.Append(ch);
continue;
}
int num7 = HexToInt(s[i + 1]);
int num8 = HexToInt(s[i + 2]);
if ((num7 >= 0) && (num8 >= 0))
{
byte b = (byte)((num7 << 4) | num8);
i += 2;
result.Append((char)b);
continue;
}
}
AddByte:
if ((ch & 0xff80) == 0)
{
result.Append(ch);
}
else
{
result.Append(ch);
}
}
return result.ToString();
}
static int HexToInt(char h)
{
if (h >= '0' && h <= '9')
return (h - '0');
if (h >= 'a' && h <= 'f')
return ((h - 'a') + 10);
if (h >= 'A' && h <= 'F')
return (h - 'A') + 10;
return -1;
}
#endif
#if WPF
/// <summary>
/// Tests if a file is a JPEG.
/// </summary>
/// <param name="filename">The filename.</param>
internal static bool TestJpeg(string filename)
{
// $THHO_NET6 Use ImageImporterJpeg to reduce code redundancy and detect CMYK format.
byte[]? imageBits = null;
return ReadJpegFile(filename, 16, ref imageBits);
}
/// <summary>
/// Tests if a file is a JPEG.
/// </summary>
/// <param name="stream">The filename.</param>
internal static bool TestJpeg(Stream stream)
{
byte[]? imageBits = null;
return ReadJpegFile(stream, 16, ref imageBits) == true;
}
/// <summary>
/// Reads the JPEG file.
/// </summary>
/// <param name="filename">The filename.</param>
/// <param name="maxRead">The maximum count of bytes to be read.</param>
/// <param name="imageBits">The bytes read from the file.</param>
/// <returns>False, if file could not be read or is not a JPEG file.</returns>
internal static bool ReadJpegFile(string filename, int maxRead, ref byte[]? imageBits)
{
if (File.Exists(filename))
{
FileStream? fs;
try
{
fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
catch
{
return false;
}
bool? test = ReadJpegFile(fs, maxRead, ref imageBits);
// Treat test result as definite.
if (test == false || test == true)
{
fs.Close();
return test.Value;
}
// Test result is maybe.
// Hack: store the file in PDF if extension matches ...
string str = filename.ToLower();
if (str.EndsWith(".jpg", StringComparison.Ordinal) || str.EndsWith(".jpeg", StringComparison.Ordinal))
return true;
}
return false;
}
/// <summary>
/// Reads the JPEG file.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="maxRead">The maximum count of bytes to be read.</param>
/// <param name="imageBits">The bytes read from the file.</param>
/// <returns>False, if file could not be read or is not a JPEG file.</returns>
internal static bool? ReadJpegFile(Stream stream, int maxRead, ref byte[]? imageBits)
{
if (!stream.CanSeek)
return false;
stream.Seek(0, SeekOrigin.Begin);
if (stream.Length < 16)
return false;
int len = maxRead == -1 ? (int)stream.Length : maxRead;
imageBits = new byte[len];
// ReSharper disable once UnusedVariable
var x = stream.Read(imageBits, 0, len);
if (imageBits[0] == 0xff &&
imageBits[1] == 0xd8 &&
imageBits[2] == 0xff &&
imageBits[3] == 0xe0 &&
imageBits[6] == 0x4a &&
imageBits[7] == 0x46 &&
imageBits[8] == 0x49 &&
imageBits[9] == 0x46 &&
imageBits[10] == 0x0)
{
return true;
}
// TODO: Exif: find JFIF header
if (imageBits[0] == 0xff &&
imageBits[1] == 0xd8 &&
imageBits[2] == 0xff &&
imageBits[3] == 0xe1 /*&&
imageBits[6] == 0x4a &&
imageBits[7] == 0x46 &&
imageBits[8] == 0x49 &&
imageBits[9] == 0x46 &&
imageBits[10] == 0x0*/)
{
// Hack: store the file in PDF if extension matches ...
return null;
}
return false;
}
#endif
/// <summary>
/// Under construction
/// </summary>
public void Dispose()
{
Dispose(true);
//GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes underlying GDI+ object.
/// </summary>
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
_disposed = true;
#if CORE || GDI || WPF
{
_importedImage = null;
}
#endif
#if GDI
if (_gdiImage != null!)
{
try
{
Lock.EnterGdiPlus();
_gdiImage.Dispose();
_gdiImage = null!;
}
finally { Lock.ExitGdiPlus(); }
}
#endif
#if WPF
_wpfImage = null!;
#endif
}
bool _disposed;
/// <summary>
/// Gets the width of the image.
/// </summary>
[Obsolete("Use either PixelWidth or PointWidth. Temporarily obsolete because of rearrangements for WPF. Currently same as PixelWidth, but will become PointWidth in future releases of PDFsharp.")]
public virtual double Width
{
get
{
#if CORE || GDI || WPF
if (_importedImage != null)
return _importedImage.Information.Width;
#endif
#if CORE
return 100;
#endif
#if GDI && !WPF
try
{
Lock.EnterGdiPlus();
return _gdiImage.Width;
}
finally { Lock.ExitGdiPlus(); }
#endif
#if GDI && WPF
double gdiWidth = _gdiImage.Width;
double wpfWidth = _wpfImage.PixelWidth;
Debug.Assert(gdiWidth == wpfWidth);
return wpfWidth;
#endif
//#if GDI && !WPF
// return _gdiImage.Width;
//#endif
#if WPF && !GDI
return _wpfImage.PixelWidth;
#endif
#if UWP
return 100;
#endif
}
}
/// <summary>
/// Gets the height of the image.
/// </summary>
[Obsolete("Use either PixelHeight or PointHeight. Temporarily obsolete because of rearrangements for WPF. Currently same as PixelHeight, but will become PointHeight in future releases of PDFsharp.")]
public virtual double Height
{
get
{
#if CORE || GDI || WPF
if (_importedImage != null)
return _importedImage.Information.Height;
#endif
#if CORE
return 100;
#endif
#if GDI && !WPF
try
{
Lock.EnterGdiPlus();
return _gdiImage.Height;
}
finally { Lock.ExitGdiPlus(); }
#endif
#if GDI && WPF
double gdiHeight = _gdiImage.Height;
double wpfHeight = _wpfImage.PixelHeight;
Debug.Assert(gdiHeight == wpfHeight);
return wpfHeight;
#endif
//#if GDI && !WPF
// return _gdiImage.Height;
//#endif
#if WPF && !GDI
return _wpfImage.PixelHeight;
#endif
#if UWP
return _wrtImage.PixelHeight;
#endif
}
}
#if CORE || GDI || WPF
/// <summary>
/// The factor for conversion from DPM to PointWidth or PointHeight.
/// 72 points per inch, 1000 mm per meter, 25.4 mm per inch => 72 * 1000 / 25.4.
/// </summary>
const double FactorDPM72 = 72000 / 25.4d;
/// <summary>
/// The factor for conversion from DPM to DPI.
/// 1000 mm per meter, 25.4 mm per inch => 1000 / 25.4.
/// </summary>
const double FactorDPM = 1000 / 25.4d;
#endif
/// <summary>
/// Gets the width of the image in point.
/// </summary>
public virtual double PointWidth
{
get
{
#if CORE || GDI || WPF
if (_importedImage != null)
{
if (_importedImage.Information.HorizontalDPM > 0)
return _importedImage.Information.Width * FactorDPM72 / _importedImage.Information.HorizontalDPM;
if (_importedImage.Information.HorizontalDPI > 0)
return _importedImage.Information.Width * 72 / _importedImage.Information.HorizontalDPI;
// Assume 72 DPI if information not available.
return _importedImage.Information.Width;
}
#endif
#if CORE
return 100;
#endif
#if GDI && !WPF
try
{
Lock.EnterGdiPlus();
return _gdiImage.Width * 72 / _gdiImage.HorizontalResolution;
}
finally { Lock.ExitGdiPlus(); }
#endif
#if GDI && WPF
double gdiWidth = _gdiImage.Width * 72 / _gdiImage.HorizontalResolution;
double wpfWidth = _wpfImage.Width * (72.0 / 96.0);
//Debug.Assert(gdiWidth == wpfWidth);
Debug.Assert(DoubleUtil.AreRoughlyEqual(gdiWidth, wpfWidth, 5));
return wpfWidth;
#endif
//#if GDI && !WPF
// return _gdiImage.Width * 72 / _gdiImage.HorizontalResolution;
//#endif
#if WPF && !GDI
Debug.Assert(Math.Abs(_wpfImage.PixelWidth * 72 / _wpfImage.DpiX - _wpfImage.Width * (72.0 / 96.0)) < 0.001);
return _wpfImage.Width * (72.0 / 96.0);
#endif
#if UWP
//var wb = new WriteableBitmap();
//GetImagePropertiesAsync
return 100;
#endif
}
}
/// <summary>
/// Gets the height of the image in point.
/// </summary>
public virtual double PointHeight
{
get
{
#if CORE || GDI || WPF
if (_importedImage != null)
{
if (_importedImage.Information.VerticalDPM > 0)
return _importedImage.Information.Height * FactorDPM72 / _importedImage.Information.VerticalDPM;
if (_importedImage.Information.VerticalDPI > 0)
return _importedImage.Information.Height * 72 / _importedImage.Information.VerticalDPI;
// Assume 72 DPI if information not available.
return _importedImage.Information.Width;
}
#endif
#if CORE
return 100;
#endif
#if GDI && !WPF
try
{
Lock.EnterGdiPlus();
return _gdiImage.Height * 72 / _gdiImage.HorizontalResolution;
}
finally { Lock.ExitGdiPlus(); }
#endif
#if GDI && WPF
double gdiHeight = _gdiImage.Height * 72 / _gdiImage.HorizontalResolution;
double wpfHeight = _wpfImage.Height * (72.0 / 96.0);
Debug.Assert(DoubleUtil.AreRoughlyEqual(gdiHeight, wpfHeight, 5));
return wpfHeight;
#endif
//#if GDI && !WPF
// return _gdiImage.Height * 72 / _gdiImage.HorizontalResolution;
//#endif
#if WPF && !GDI
Debug.Assert(Math.Abs(_wpfImage.PixelHeight * 72 / _wpfImage.DpiY - _wpfImage.Height * (72.0 / 96.0)) < 0.001);
return _wpfImage.Height * (72.0 / 96.0);
#endif
#if UWP
return _wrtImage.PixelHeight; //_gdi Image.Width * 72 / _gdiImage.HorizontalResolution;
#endif
}
}
/// <summary>
/// Gets the width of the image in pixels.
/// </summary>
public virtual int PixelWidth
{
get
{
#if CORE || GDI || WPF
if (_importedImage != null)
return (int)_importedImage.Information.Width;
#endif
#if CORE
return 100;
#endif
#if GDI && !WPF
try
{
Lock.EnterGdiPlus();
return _gdiImage.Width;
}
finally { Lock.ExitGdiPlus(); }
#endif
#if GDI && WPF
int gdiWidth = _gdiImage.Width;
int wpfWidth = _wpfImage.PixelWidth;
Debug.Assert(gdiWidth == wpfWidth);