-
Notifications
You must be signed in to change notification settings - Fork 957
/
Image.cs
955 lines (824 loc) · 28.9 KB
/
Image.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using Windows.Win32.System.Com;
namespace System.Drawing;
/// <summary>
/// An abstract base class that provides functionality for 'Bitmap', 'Icon', 'Cursor', and 'Metafile' descended classes.
/// </summary>
[Editor($"System.Drawing.Design.ImageEditor, {AssemblyRef.SystemDrawingDesign}",
$"System.Drawing.Design.UITypeEditor, {AssemblyRef.SystemDrawing}")]
[ImmutableObject(true)]
[Serializable]
[Runtime.CompilerServices.TypeForwardedFrom(AssemblyRef.SystemDrawing)]
[TypeConverter(typeof(ImageConverter))]
public abstract unsafe class Image : MarshalByRefObject, IImage, IDisposable, ICloneable, ISerializable
{
// The signature of this delegate is incorrect. The signature of the corresponding
// native callback function is:
// extern "C" {
// typedef BOOL (CALLBACK * ImageAbort)(VOID *);
// typedef ImageAbort DrawImageAbort;
// typedef ImageAbort GetThumbnailImageAbort;
// }
// However, as this delegate is not used in both GDI 1.0 and 1.1, we choose not
// to modify it, in order to preserve compatibility.
public delegate bool GetThumbnailImageAbort();
GpImage* IPointer<GpImage>.Pointer => _nativeImage;
[NonSerialized]
private GpImage* _nativeImage;
private object? _userData;
// Used to work around lack of animated gif encoder.
private byte[]? _animatedGifRawData;
ReadOnlySpan<byte> IRawData.Data => _animatedGifRawData;
[Localizable(false)]
[DefaultValue(null)]
public object? Tag
{
get => _userData;
set => _userData = value;
}
private protected Image() { }
#pragma warning disable CA2229 // Implement serialization constructors
private protected Image(SerializationInfo info, StreamingContext context)
#pragma warning restore CA2229 // Implement serialization constructors
{
byte[] dat = (byte[])info.GetValue("Data", typeof(byte[]))!; // Do not rename (binary serialization)
try
{
SetNativeImage(InitializeFromStream(new MemoryStream(dat)));
}
catch (Exception e) when (e is ExternalException
or ArgumentException
or OutOfMemoryException
or InvalidOperationException
or NotImplementedException
or FileNotFoundException)
{
}
}
void ISerializable.GetObjectData(SerializationInfo si, StreamingContext context)
{
using MemoryStream stream = new();
this.Save(stream);
si.AddValue("Data", stream.ToArray(), typeof(byte[])); // Do not rename (binary serialization)
}
/// <summary>
/// Creates an <see cref='Image'/> from the specified file.
/// </summary>
public static Image FromFile(string filename) => FromFile(filename, false);
public static Image FromFile(string filename, bool useEmbeddedColorManagement)
{
if (!File.Exists(filename))
{
// Throw a more specific exception for invalid paths that are null or empty,
// contain invalid characters or are too long.
filename = Path.GetFullPath(filename);
throw new FileNotFoundException(filename);
}
// GDI+ will read this file multiple times. Get the fully qualified path
// so if our app changes default directory we won't get an error
filename = Path.GetFullPath(filename);
GpImage* image = null;
fixed (char* fn = filename)
{
if (useEmbeddedColorManagement)
{
PInvoke.GdipLoadImageFromFileICM(fn, &image).ThrowIfFailed();
}
else
{
PInvoke.GdipLoadImageFromFile(fn, &image).ThrowIfFailed();
}
}
ValidateImage(image);
Image img = CreateImageObject(image);
GetAnimatedGifRawData(img, filename, dataStream: null);
return img;
}
/// <summary>
/// Creates an <see cref='Image'/> from the specified data stream.
/// </summary>
public static Image FromStream(Stream stream) => FromStream(stream, useEmbeddedColorManagement: false);
public static Image FromStream(Stream stream, bool useEmbeddedColorManagement) =>
FromStream(stream, useEmbeddedColorManagement, true);
public static Image FromStream(Stream stream, bool useEmbeddedColorManagement, bool validateImageData)
{
ArgumentNullException.ThrowIfNull(stream);
GpImage* image = LoadGdipImageFromStream(stream, useEmbeddedColorManagement);
if (validateImageData)
{
ValidateImage(image);
}
Image img = CreateImageObject(image);
GetAnimatedGifRawData(img, filename: null, stream);
return img;
}
// Used for serialization
private GpImage* InitializeFromStream(Stream stream)
{
GpImage* image = LoadGdipImageFromStream(stream, useEmbeddedColorManagement: false);
ValidateImage(image);
_nativeImage = image;
GdiPlus.ImageType type = default;
PInvoke.GdipGetImageType(_nativeImage, &type).ThrowIfFailed();
GetAnimatedGifRawData(this, filename: null, stream);
return image;
}
private static GpImage* LoadGdipImageFromStream(Stream stream, bool useEmbeddedColorManagement)
{
using var iStream = stream.ToIStream(makeSeekable: true);
return LoadGdipImageFromStream(iStream, useEmbeddedColorManagement);
}
private static unsafe GpImage* LoadGdipImageFromStream(IStream* stream, bool useEmbeddedColorManagement)
{
GpImage* image;
if (useEmbeddedColorManagement)
{
PInvoke.GdipLoadImageFromStreamICM(stream, &image).ThrowIfFailed();
}
else
{
PInvoke.GdipLoadImageFromStream(stream, &image).ThrowIfFailed();
}
return image;
}
internal Image(GpImage* nativeImage) => SetNativeImage(nativeImage);
/// <summary>
/// Cleans up Windows resources for this <see cref='Image'/>.
/// </summary>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Cleans up Windows resources for this <see cref='Image'/>.
/// </summary>
~Image() => Dispose(disposing: false);
/// <summary>
/// Creates an exact copy of this <see cref='Image'/>.
/// </summary>
public object Clone()
{
GpImage* cloneImage;
PInvoke.GdipCloneImage(_nativeImage, &cloneImage).ThrowIfFailed();
ValidateImage(cloneImage);
GC.KeepAlive(this);
return CreateImageObject(cloneImage);
}
protected virtual void Dispose(bool disposing)
{
if (_nativeImage is null)
{
return;
}
Status status = !Gdip.Initialized ? Status.Ok : PInvoke.GdipDisposeImage(_nativeImage);
_nativeImage = null;
Debug.Assert(status == Status.Ok, $"GDI+ returned an error status: {status}");
}
/// <summary>
/// Saves this <see cref='Image'/> to the specified file.
/// </summary>
public void Save(string filename) => Save(filename, RawFormat);
/// <summary>
/// Saves this <see cref='Image'/> to the specified file in the specified format.
/// </summary>
public void Save(string filename, ImageFormat format)
{
ArgumentNullException.ThrowIfNull(format);
Guid encoder = format.Encoder;
if (encoder == Guid.Empty)
{
encoder = ImageCodecInfoHelper.GetEncoderClsid(PInvokeCore.ImageFormatPNG);
}
Save(filename, encoder, null);
}
/// <summary>
/// Saves this <see cref='Image'/> to the specified file in the specified format and with the specified encoder parameters.
/// </summary>
public void Save(string filename, ImageCodecInfo encoder, Imaging.EncoderParameters? encoderParams)
=> Save(filename, encoder.Clsid, encoderParams);
private void Save(string filename, Guid encoder, Imaging.EncoderParameters? encoderParams)
{
ArgumentNullException.ThrowIfNull(filename);
if (encoder == Guid.Empty)
{
throw new ArgumentNullException(nameof(encoder));
}
ThrowIfDirectoryDoesntExist(filename);
GdiPlus.EncoderParameters* nativeParameters = null;
if (encoderParams is not null)
{
_animatedGifRawData = null;
nativeParameters = encoderParams.ConvertToNative();
}
try
{
if (_animatedGifRawData is not null && RawFormat.Encoder == encoder)
{
// Special case for animated gifs. We don't have an encoder for them, so we just write the raw data.
using var fs = File.OpenWrite(filename);
fs.Write(_animatedGifRawData, 0, _animatedGifRawData.Length);
return;
}
fixed (char* fn = filename)
{
PInvoke.GdipSaveImageToFile(_nativeImage, fn, &encoder, nativeParameters).ThrowIfFailed();
}
}
finally
{
if (nativeParameters is not null)
{
Marshal.FreeHGlobal((nint)nativeParameters);
}
GC.KeepAlive(this);
GC.KeepAlive(encoderParams);
}
}
/// <summary>
/// Saves this <see cref='Image'/> to the specified stream in the specified format.
/// </summary>
public void Save(Stream stream, ImageFormat format)
{
ArgumentNullException.ThrowIfNull(format);
this.Save(stream, format.Encoder, format.Guid, encoderParameters: null);
}
/// <summary>
/// Saves this <see cref='Image'/> to the specified stream in the specified format.
/// </summary>
public void Save(Stream stream, ImageCodecInfo encoder, Imaging.EncoderParameters? encoderParams)
{
ArgumentNullException.ThrowIfNull(stream);
ArgumentNullException.ThrowIfNull(encoder);
GdiPlus.EncoderParameters* nativeParameters = null;
if (encoderParams is not null)
{
_animatedGifRawData = null;
nativeParameters = encoderParams.ConvertToNative();
}
try
{
this.Save(stream, encoder.Clsid, encoder.FormatID, nativeParameters);
}
finally
{
if (nativeParameters is not null)
{
Marshal.FreeHGlobal((nint)nativeParameters);
}
GC.KeepAlive(this);
GC.KeepAlive(encoderParams);
}
}
/// <summary>
/// Adds an <see cref='Imagin.EncoderParameters'/> to this <see cref='Image'/>.
/// </summary>
public void SaveAdd(Imaging.EncoderParameters? encoderParams)
{
GdiPlus.EncoderParameters* nativeParameters = null;
if (encoderParams is not null)
{
nativeParameters = encoderParams.ConvertToNative();
}
_animatedGifRawData = null;
try
{
PInvoke.GdipSaveAdd(_nativeImage, nativeParameters).ThrowIfFailed();
}
finally
{
if (nativeParameters is not null)
{
Marshal.FreeHGlobal((nint)nativeParameters);
}
GC.KeepAlive(this);
GC.KeepAlive(encoderParams);
}
}
/// <summary>
/// Adds an <see cref='EncoderParameters'/> to the specified <see cref='Image'/>.
/// </summary>
public void SaveAdd(Image image, Imaging.EncoderParameters? encoderParams)
{
ArgumentNullException.ThrowIfNull(image);
GdiPlus.EncoderParameters* nativeParameters = null;
if (encoderParams is not null)
{
nativeParameters = encoderParams.ConvertToNative();
}
_animatedGifRawData = null;
try
{
PInvoke.GdipSaveAddImage(_nativeImage, image._nativeImage, nativeParameters).ThrowIfFailed();
}
finally
{
if (nativeParameters is not null)
{
Marshal.FreeHGlobal((nint)nativeParameters);
}
GC.KeepAlive(this);
GC.KeepAlive(image);
GC.KeepAlive(encoderParams);
}
}
private static void ThrowIfDirectoryDoesntExist(string filename)
{
string? directoryPart = Path.GetDirectoryName(filename);
if (!string.IsNullOrEmpty(directoryPart) && !Directory.Exists(directoryPart))
{
throw new DirectoryNotFoundException(SR.Format(SR.TargetDirectoryDoesNotExist, directoryPart, filename));
}
}
/// <summary>
/// Gets the width and height of this <see cref='Image'/>.
/// </summary>
public SizeF PhysicalDimension
{
get
{
float width;
float height;
PInvoke.GdipGetImageDimension(_nativeImage, &width, &height).ThrowIfFailed();
GC.KeepAlive(this);
return new SizeF(width, height);
}
}
/// <summary>
/// Gets the width and height of this <see cref='Image'/>.
/// </summary>
public Size Size => new(Width, Height);
/// <summary>
/// Gets the width of this <see cref='Image'/>.
/// </summary>
[DefaultValue(false)]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public int Width
{
get
{
uint width;
PInvoke.GdipGetImageWidth(_nativeImage, &width).ThrowIfFailed();
GC.KeepAlive(this);
return (int)width;
}
}
/// <summary>
/// Gets the height of this <see cref='Image'/>.
/// </summary>
[DefaultValue(false)]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public int Height
{
get
{
uint height;
PInvoke.GdipGetImageHeight(_nativeImage, &height).ThrowIfFailed();
GC.KeepAlive(this);
return (int)height;
}
}
/// <summary>
/// Gets the horizontal resolution, in pixels-per-inch, of this <see cref='Image'/>.
/// </summary>
public float HorizontalResolution
{
get
{
float horzRes;
PInvoke.GdipGetImageHorizontalResolution(_nativeImage, &horzRes).ThrowIfFailed();
GC.KeepAlive(this);
return horzRes;
}
}
/// <summary>
/// Gets the vertical resolution, in pixels-per-inch, of this <see cref='Image'/>.
/// </summary>
public float VerticalResolution
{
get
{
float vertRes;
PInvoke.GdipGetImageVerticalResolution(_nativeImage, &vertRes).ThrowIfFailed();
GC.KeepAlive(this);
return vertRes;
}
}
/// <summary>
/// Gets attribute flags for this <see cref='Image'/>.
/// </summary>
[Browsable(false)]
public int Flags
{
get
{
uint flags;
PInvoke.GdipGetImageFlags(_nativeImage, &flags).ThrowIfFailed();
GC.KeepAlive(this);
return (int)flags;
}
}
/// <summary>
/// Gets the format of this <see cref='Image'/>.
/// </summary>
public ImageFormat RawFormat
{
get
{
Guid guid = default;
PInvoke.GdipGetImageRawFormat(_nativeImage, &guid).ThrowIfFailed();
GC.KeepAlive(this);
return new ImageFormat(guid);
}
}
/// <summary>
/// Gets the pixel format for this <see cref='Image'/>.
/// </summary>
public PixelFormat PixelFormat => (PixelFormat)this.GetPixelFormat();
/// <summary>
/// Gets an array of the property IDs stored in this <see cref='Image'/>.
/// </summary>
[Browsable(false)]
public int[] PropertyIdList
{
get
{
uint count;
PInvoke.GdipGetPropertyCount(_nativeImage, &count).ThrowIfFailed();
if (count == 0)
{
return [];
}
int[] propid = new int[count];
fixed (int* pPropid = propid)
{
PInvoke.GdipGetPropertyIdList(_nativeImage, count, (uint*)pPropid).ThrowIfFailed();
}
GC.KeepAlive(this);
return propid;
}
}
/// <summary>
/// Gets an array of <see cref='Imaging.PropertyItem'/> objects that describe this <see cref='Image'/>.
/// </summary>
[Browsable(false)]
public Imaging.PropertyItem[] PropertyItems
{
get
{
uint size, count;
PInvoke.GdipGetPropertySize(_nativeImage, &size, &count).ThrowIfFailed();
if (size == 0 || count == 0)
{
return [];
}
Imaging.PropertyItem[] result = new Imaging.PropertyItem[(int)count];
using BufferScope<byte> buffer = new((int)size);
fixed (byte* b = buffer)
{
GdiPlus.PropertyItem* properties = (GdiPlus.PropertyItem*)b;
PInvoke.GdipGetAllPropertyItems(_nativeImage, size, count, properties);
for (int i = 0; i < count; i++)
{
result[i] = Imaging.PropertyItem.FromNative(properties + i);
}
}
GC.KeepAlive(this);
return result;
}
}
/// <summary>
/// Gets a bounding rectangle in the specified units for this <see cref='Image'/>.
/// </summary>
public RectangleF GetBounds(ref GraphicsUnit pageUnit)
{
// The Unit is hard coded to GraphicsUnit.Pixel in GDI+.
RectangleF bounds = this.GetImageBounds();
pageUnit = GraphicsUnit.Pixel;
return bounds;
}
/// <summary>
/// Gets or sets the color palette used for this <see cref='Image'/>.
/// </summary>
[Browsable(false)]
public ColorPalette Palette
{
get
{
// "size" is total byte size:
// sizeof(ColorPalette) + (pal->Count-1)*sizeof(ARGB)
int size;
PInvoke.GdipGetImagePaletteSize(_nativeImage, &size).ThrowIfFailed();
using BufferScope<uint> buffer = new(size / sizeof(uint));
fixed (uint* b = buffer)
{
PInvoke.GdipGetImagePalette(_nativeImage, (GdiPlus.ColorPalette*)b, size).ThrowIfFailed();
GC.KeepAlive(this);
return ColorPalette.ConvertFromBuffer(buffer);
}
}
set
{
using BufferScope<uint> buffer = value.ConvertToBuffer();
fixed (uint* b = buffer)
{
PInvoke.GdipSetImagePalette(_nativeImage, (GdiPlus.ColorPalette*)b).ThrowIfFailed();
GC.KeepAlive(this);
}
}
}
// Thumbnail support
/// <summary>
/// Returns the thumbnail for this <see cref='Image'/>.
/// </summary>
public Image GetThumbnailImage(int thumbWidth, int thumbHeight, GetThumbnailImageAbort? callback, IntPtr callbackData)
{
GpImage* thumbImage;
// GDI+ had to ignore the callback as System.Drawing didn't define it correctly so it was eventually removed
// completely in Windows 7. As such, we don't need to pass it to GDI+.
PInvoke.GdipGetImageThumbnail(
this.Pointer(),
(uint)thumbWidth,
(uint)thumbHeight,
&thumbImage,
0,
null).ThrowIfFailed();
GC.KeepAlive(this);
return CreateImageObject(thumbImage);
}
internal static void ValidateImage(GpImage* image)
{
try
{
PInvoke.GdipImageForceValidation(image).ThrowIfFailed();
}
catch
{
PInvoke.GdipDisposeImage(image);
throw;
}
}
/// <summary>
/// Returns the number of frames of the given dimension.
/// </summary>
public int GetFrameCount(FrameDimension dimension)
{
Guid dimensionID = dimension.Guid;
uint count;
PInvoke.GdipImageGetFrameCount(_nativeImage, &dimensionID, &count).ThrowIfFailed();
GC.KeepAlive(this);
return (int)count;
}
/// <summary>
/// Gets the specified property item from this <see cref='Image'/>.
/// </summary>
public Imaging.PropertyItem? GetPropertyItem(int propid)
{
uint size;
PInvoke.GdipGetPropertyItemSize(_nativeImage, (uint)propid, &size).ThrowIfFailed();
if (size == 0)
{
return null;
}
using BufferScope<byte> buffer = new((int)size);
fixed (byte* b = buffer)
{
GdiPlus.PropertyItem* property = (GdiPlus.PropertyItem*)b;
PInvoke.GdipGetPropertyItem(_nativeImage, (uint)propid, size, property).ThrowIfFailed();
GC.KeepAlive(this);
return Imaging.PropertyItem.FromNative(property);
}
}
/// <summary>
/// Selects the frame specified by the given dimension and index.
/// </summary>
public int SelectActiveFrame(FrameDimension dimension, int frameIndex)
{
Guid dimensionID = dimension.Guid;
PInvoke.GdipImageSelectActiveFrame(_nativeImage, &dimensionID, (uint)frameIndex).ThrowIfFailed();
GC.KeepAlive(this);
return 0;
}
/// <summary>
/// Sets the specified property item to the specified value.
/// </summary>
public unsafe void SetPropertyItem(Imaging.PropertyItem propitem)
{
fixed (byte* propItemValue = propitem.Value)
{
GdiPlus.PropertyItem native = new()
{
id = (uint)propitem.Id,
length = (uint)propitem.Len,
type = (ushort)propitem.Type,
value = propItemValue
};
PInvoke.GdipSetPropertyItem(_nativeImage, &native).ThrowIfFailed();
GC.KeepAlive(this);
}
}
public void RotateFlip(RotateFlipType rotateFlipType)
{
PInvoke.GdipImageRotateFlip(_nativeImage, (GdiPlus.RotateFlipType)rotateFlipType).ThrowIfFailed();
GC.KeepAlive(this);
}
/// <summary>
/// Removes the specified property item from this <see cref='Image'/>.
/// </summary>
public void RemovePropertyItem(int propid)
{
PInvoke.GdipRemovePropertyItem(_nativeImage, (uint)propid).ThrowIfFailed();
GC.KeepAlive(this);
}
/// <summary>
/// Returns information about the codecs used for this <see cref='Image'/>.
/// </summary>
public unsafe Imaging.EncoderParameters? GetEncoderParameterList(Guid encoder)
{
Imaging.EncoderParameters parameters;
uint size;
PInvoke.GdipGetEncoderParameterListSize(_nativeImage, &encoder, &size).ThrowIfFailed();
if (size <= 0)
{
return null;
}
using BufferScope<byte> buffer = new((int)size);
fixed (byte* b = buffer)
{
PInvoke.GdipGetEncoderParameterList(
_nativeImage,
&encoder,
size,
(GdiPlus.EncoderParameters*)b).ThrowIfFailed();
parameters = Imaging.EncoderParameters.ConvertFromNative((GdiPlus.EncoderParameters*)b);
GC.KeepAlive(this);
}
return parameters;
}
/// <summary>
/// Creates a <see cref='Bitmap'/> from a Windows handle.
/// </summary>
public static Bitmap FromHbitmap(IntPtr hbitmap) => FromHbitmap(hbitmap, IntPtr.Zero);
/// <summary>
/// Creates a <see cref='Bitmap'/> from the specified Windows handle with the specified color palette.
/// </summary>
public static Bitmap FromHbitmap(IntPtr hbitmap, IntPtr hpalette)
{
GpBitmap* bitmap;
PInvoke.GdipCreateBitmapFromHBITMAP((HBITMAP)hbitmap, (HPALETTE)hpalette, &bitmap).ThrowIfFailed();
return new Bitmap(bitmap);
}
/// <summary>
/// Returns a value indicating whether the pixel format is extended.
/// </summary>
public static bool IsExtendedPixelFormat(PixelFormat pixfmt) => (pixfmt & PixelFormat.Extended) != 0;
/// <summary>
/// Returns a value indicating whether the pixel format is canonical.
/// </summary>
public static bool IsCanonicalPixelFormat(PixelFormat pixfmt)
{
// Canonical formats:
//
// PixelFormat32bppARGB
// PixelFormat32bppPARGB
// PixelFormat64bppARGB
// PixelFormat64bppPARGB
return (pixfmt & PixelFormat.Canonical) != 0;
}
internal void SetNativeImage(GpImage* handle)
{
if (handle is null)
throw new ArgumentException(SR.NativeHandle0, nameof(handle));
_nativeImage = handle;
}
// Multi-frame support
/// <summary>
/// Gets an array of GUIDs that represent the dimensions of frames within this <see cref='Image'/>.
/// </summary>
[Browsable(false)]
public unsafe Guid[] FrameDimensionsList
{
get
{
uint count;
PInvoke.GdipImageGetFrameDimensionsCount(_nativeImage, &count).ThrowIfFailed();
Debug.Assert(count >= 0, "FrameDimensionsList returns bad count");
if (count <= 0)
{
return [];
}
Guid[] guids = new Guid[count];
fixed (Guid* g = guids)
{
PInvoke.GdipImageGetFrameDimensionsList(_nativeImage, g, count).ThrowIfFailed();
}
GC.KeepAlive(this);
return guids;
}
}
/// <summary>
/// Returns the size of the specified pixel format.
/// </summary>
public static int GetPixelFormatSize(PixelFormat pixfmt) => ((int)pixfmt >> 8) & 0xFF;
/// <summary>
/// Returns a value indicating whether the pixel format contains alpha information.
/// </summary>
public static bool IsAlphaPixelFormat(PixelFormat pixfmt) => (pixfmt & PixelFormat.Alpha) != 0;
internal static Image CreateImageObject(GpImage* nativeImage)
{
GdiPlus.ImageType imageType = default;
PInvoke.GdipGetImageType(nativeImage, &imageType);
return imageType switch
{
GdiPlus.ImageType.ImageTypeBitmap => new Bitmap((GpBitmap*)nativeImage),
GdiPlus.ImageType.ImageTypeMetafile => new Metafile((nint)nativeImage),
_ => throw new ArgumentException(SR.InvalidImage),
};
}
/// <summary>
/// If the image is an animated GIF, loads the raw data for the image into the _rawData field so we
/// can work around the lack of an animated GIF encoder.
/// </summary>
internal static unsafe void GetAnimatedGifRawData(Image image, string? filename, Stream? dataStream)
{
if (!image.RawFormat.Equals(ImageFormat.Gif))
{
return;
}
bool animatedGif = false;
uint dimensions;
PInvoke.GdipImageGetFrameDimensionsCount(image._nativeImage, &dimensions).ThrowIfFailed();
if (dimensions <= 0)
{
return;
}
using BufferScope<Guid> guids = new(stackalloc Guid[16], (int)dimensions);
fixed (Guid* g = guids)
{
PInvoke.GdipImageGetFrameDimensionsList(image._nativeImage, g, dimensions).ThrowIfFailed();
}
Guid timeGuid = FrameDimension.Time.Guid;
for (int i = 0; i < dimensions; i++)
{
if (timeGuid == guids[i])
{
animatedGif = image.GetFrameCount(FrameDimension.Time) > 1;
break;
}
}
if (!animatedGif)
{
return;
}
try
{
Stream? created = null;
long lastPos = 0;
if (dataStream is not null)
{
lastPos = dataStream.Position;
dataStream.Position = 0;
}
try
{
if (dataStream is null)
{
created = dataStream = File.OpenRead(filename ?? throw new InvalidOperationException());
}
image._animatedGifRawData = new byte[(int)dataStream.Length];
dataStream.Read(image._animatedGifRawData, 0, (int)dataStream.Length);
}
finally
{
if (created is not null)
{
created.Close();
}
else
{
dataStream!.Position = lastPos;
}
}
}
catch (Exception e) when (e
// possible exceptions for reading the filename
is UnauthorizedAccessException
or DirectoryNotFoundException
or IOException
// possible exceptions for setting/getting the position inside dataStream
or NotSupportedException
or ObjectDisposedException
// possible exception when reading stuff into dataStream
or ArgumentException)
{
}
}
}