-
Notifications
You must be signed in to change notification settings - Fork 957
/
Icon.cs
912 lines (782 loc) · 32.5 KB
/
Icon.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using Windows.Win32.System.Ole;
namespace System.Drawing;
[Editor($"System.Drawing.Design.IconEditor, {AssemblyRef.SystemDrawingDesign}",
$"System.Drawing.Design.UITypeEditor, {AssemblyRef.SystemDrawing}")]
[TypeConverter(typeof(IconConverter))]
[Serializable]
[TypeForwardedFrom(AssemblyRef.SystemDrawing)]
public sealed unsafe partial class Icon : MarshalByRefObject, ICloneable, IDisposable, ISerializable, IIcon
{
private static int s_bitDepth;
// The PNG signature is specified at http://www.w3.org/TR/PNG/#5PNG-file-signature
private const int PNGSignature1 = 137 + ('P' << 8) + ('N' << 16) + ('G' << 24);
private const int PNGSignature2 = 13 + (10 << 8) + (26 << 16) + (10 << 24);
// Icon data
private readonly byte[]? _iconData;
private uint _bestImageOffset;
private uint _bestBitDepth;
private uint _bestBytesInRes;
private bool? _isBestImagePng;
private Size _iconSize = Size.Empty;
[NonSerialized]
private HICON _handle;
private readonly bool _ownHandle = true;
private Icon() { }
internal Icon(HICON handle) : this(handle, false)
{
}
internal Icon(HICON handle, bool takeOwnership)
{
if (handle.IsNull)
{
throw new ArgumentException(SR.Format(SR.InvalidGDIHandle, nameof(Icon)));
}
_handle = handle;
_ownHandle = takeOwnership;
}
public Icon(string fileName) : this(fileName, 0, 0)
{
}
public Icon(string fileName, Size size) : this(fileName, size.Width, size.Height)
{
}
public Icon(string fileName, int width, int height) : this()
{
using (FileStream f = new(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
Debug.Assert(f is not null, "File.OpenRead returned null instead of throwing an exception");
_iconData = new byte[(int)f.Length];
f.Read(_iconData, 0, _iconData.Length);
}
Initialize(width, height);
}
public Icon(Icon original, Size size) : this(original, size.Width, size.Height)
{
}
public Icon(Icon original, int width, int height) : this()
{
ArgumentNullException.ThrowIfNull(original);
_iconData = original._iconData;
if (_iconData is null)
{
_iconSize = original.Size;
_handle = PInvokeCore.CopyIcon(original, _iconSize.Width, _iconSize.Height);
}
else
{
Initialize(width, height);
}
}
public Icon(Type type, string resource) : this()
{
ArgumentNullException.ThrowIfNull(resource);
Stream? stream = type.Module.Assembly.GetManifestResourceStream(type, resource)
?? throw new ArgumentException(SR.Format(SR.ResourceNotFound, type, resource));
_iconData = new byte[(int)stream.Length];
stream.Read(_iconData, 0, _iconData.Length);
Initialize(0, 0);
}
public Icon(Stream stream) : this(stream, 0, 0)
{
}
public Icon(Stream stream, Size size) : this(stream, size.Width, size.Height)
{
}
public Icon(Stream stream, int width, int height) : this()
{
ArgumentNullException.ThrowIfNull(stream);
_iconData = new byte[(int)stream.Length];
stream.ReadExactly(_iconData);
Initialize(width, height);
}
private Icon(SerializationInfo info, StreamingContext context)
{
// Do not rename value names or change types (binary serialization)
_iconData = (byte[])info.GetValue("IconData", typeof(byte[]))!;
_iconSize = (Size)info.GetValue("IconSize", typeof(Size))!;
Initialize(_iconSize.Width, _iconSize.Height);
}
void ISerializable.GetObjectData(SerializationInfo si, StreamingContext context)
{
// Do not rename value names or change types (binary serialization)
if (_iconData is not null)
{
si.AddValue("IconData", _iconData, typeof(byte[]));
}
else
{
MemoryStream stream = new();
Save(stream);
si.AddValue("IconData", stream.ToArray(), typeof(byte[]));
}
si.AddValue("IconSize", _iconSize, typeof(Size));
}
public static Icon? ExtractAssociatedIcon(string filePath) => ExtractAssociatedIcon(filePath, 0);
private static Icon? ExtractAssociatedIcon(string filePath, int index)
{
ArgumentNullException.ThrowIfNull(filePath);
if (string.IsNullOrEmpty(filePath))
throw new ArgumentException(SR.NullOrEmptyPath, nameof(filePath));
filePath = Path.GetFullPath(filePath);
if (!File.Exists(filePath))
{
throw new FileNotFoundException(message: null, fileName: filePath);
}
// ExtractAssociatedIcon copies the loaded path into the buffer that it is passed.
// It isn't clear what the exact semantics are for copying back the path, a quick
// look at the code it might be hard coded to 128 chars for some cases. Leaving the
// historical MAX_PATH as a minimum to be safe.
char[] buffer = ArrayPool<char>.Shared.Rent(Math.Max((int)PInvokeCore.MAX_PATH, filePath.Length));
filePath.CopyTo(0, buffer, 0, filePath.Length);
buffer[filePath.Length] = '\0';
HICON hIcon;
fixed (char* b = buffer)
{
ushort piIcon = (ushort)index;
hIcon = PInvoke.ExtractAssociatedIcon(HINSTANCE.Null, b, &piIcon);
}
ArrayPool<char>.Shared.Return(buffer);
return !hIcon.IsNull ? new Icon(hIcon, takeOwnership: true) : null;
}
[Browsable(false)]
public IntPtr Handle => _handle.IsNull ? throw new ObjectDisposedException(GetType().Name) : (nint)_handle;
HICON IHandle<HICON>.Handle => (HICON)Handle;
[Browsable(false)]
public int Height => Size.Height;
public unsafe Size Size
{
get
{
if (!_iconSize.IsEmpty)
{
return _iconSize;
}
ICONINFO info = PInvokeCore.GetIconInfo(this);
BITMAP bitmap = default;
if (!info.hbmColor.IsNull)
{
PInvokeCore.GetObject(
info.hbmColor,
sizeof(BITMAP),
&bitmap);
PInvokeCore.DeleteObject(info.hbmColor);
_iconSize = new Size(bitmap.bmWidth, bitmap.bmHeight);
}
else if (!info.hbmMask.IsNull)
{
PInvokeCore.GetObject(
info.hbmMask,
sizeof(BITMAP),
&bitmap);
_iconSize = new Size(bitmap.bmWidth, bitmap.bmHeight / 2);
}
if (!info.hbmMask.IsNull)
{
PInvokeCore.DeleteObject(info.hbmMask);
}
return _iconSize;
}
}
[Browsable(false)]
public int Width => Size.Width;
public object Clone() => new Icon(this, Size.Width, Size.Height);
// Called when this object is going to destroy it's Win32 handle. You
// may override this if there is something special you need to do to
// destroy the handle. This will be called even if the handle is not
// owned by this object, which is handy if you want to create a
// derived class that has it's own create/destroy semantics.
//
// The default implementation will call the appropriate Win32
// call to destroy the handle if this object currently owns the
// handle. It will do nothing if the object does not currently
// own the handle.
internal void DestroyHandle()
{
if (_ownHandle)
{
PInvokeCore.DestroyIcon(_handle);
_handle = HICON.Null;
GC.KeepAlive(this);
}
}
public void Dispose()
{
if (!_handle.IsNull)
{
DestroyHandle();
}
GC.SuppressFinalize(this);
}
// Draws this image to a graphics object. The drawing command originates on the graphics
// object, but a graphics object generally has no idea how to render a given image. So,
// it passes the call to the actual image. This version crops the image to the given
// dimensions and allows the user to specify a rectangle within the image to draw.
private void DrawIcon(HDC hdc, Rectangle imageRect, Rectangle targetRect, bool stretch)
{
int imageX = 0;
int imageY = 0;
int imageWidth;
int imageHeight;
int targetX = 0;
int targetY = 0;
int targetWidth;
int targetHeight;
Size cursorSize = Size;
// Compute the dimensions of the icon if needed.
if (!imageRect.IsEmpty)
{
imageX = imageRect.X;
imageY = imageRect.Y;
imageWidth = imageRect.Width;
imageHeight = imageRect.Height;
}
else
{
imageWidth = cursorSize.Width;
imageHeight = cursorSize.Height;
}
if (!targetRect.IsEmpty)
{
targetX = targetRect.X;
targetY = targetRect.Y;
targetWidth = targetRect.Width;
targetHeight = targetRect.Height;
}
else
{
targetWidth = cursorSize.Width;
targetHeight = cursorSize.Height;
}
int drawWidth, drawHeight;
int clipWidth, clipHeight;
if (stretch)
{
drawWidth = cursorSize.Width * targetWidth / imageWidth;
drawHeight = cursorSize.Height * targetHeight / imageHeight;
clipWidth = targetWidth;
clipHeight = targetHeight;
}
else
{
drawWidth = cursorSize.Width;
drawHeight = cursorSize.Height;
clipWidth = targetWidth < imageWidth ? targetWidth : imageWidth;
clipHeight = targetHeight < imageHeight ? targetHeight : imageHeight;
}
// The ROP is SRCCOPY, so we can be simple here and take
// advantage of clipping regions. Drawing the cursor
// is merely a matter of offsetting and clipping.
using RegionScope clippingRegion = new(hdc);
try
{
PInvokeCore.IntersectClipRect(hdc, targetX, targetY, targetX + clipWidth, targetY + clipHeight);
PInvokeCore.DrawIconEx(
hdc,
targetX - imageX,
targetY - imageY,
this,
drawWidth,
drawHeight);
}
finally
{
// We need to delete the region handle after restoring the region as GDI+ uses a copy of the handle.
PInvokeCore.SelectClipRgn(hdc, clippingRegion);
}
}
internal void Draw(Graphics graphics, int x, int y)
{
Size size = Size;
Draw(graphics, new Rectangle(x, y, size.Width, size.Height));
}
// Draws this image to a graphics object. The drawing command originates on the graphics
// object, but a graphics object generally has no idea how to render a given image. So,
// it passes the call to the actual image. This version stretches the image to the given
// dimensions and allows the user to specify a rectangle within the image to draw.
internal void Draw(Graphics graphics, Rectangle targetRect)
{
Rectangle copy = targetRect;
using Matrix transform = graphics.Transform;
PointF offset = transform.Offset;
copy.X += (int)offset.X;
copy.Y += (int)offset.Y;
using DeviceContextHdcScope hdc = new(graphics, ApplyGraphicsProperties.Clipping);
DrawIcon(hdc, Rectangle.Empty, copy, true);
}
// Draws this image to a graphics object. The drawing command originates on the graphics
// object, but a graphics object generally has no idea how to render a given image. So,
// it passes the call to the actual image. This version crops the image to the given
// dimensions and allows the user to specify a rectangle within the image to draw.
internal void DrawUnstretched(Graphics graphics, Rectangle targetRect)
{
Rectangle copy = targetRect;
using Matrix transform = graphics.Transform;
PointF offset = transform.Offset;
copy.X += (int)offset.X;
copy.Y += (int)offset.Y;
using DeviceContextHdcScope hdc = new(graphics, ApplyGraphicsProperties.Clipping);
DrawIcon(hdc, Rectangle.Empty, copy, false);
}
~Icon() => Dispose();
public static Icon FromHandle(IntPtr handle)
{
if (handle == IntPtr.Zero)
throw new ArgumentException(null, nameof(handle));
return new Icon((HICON)handle);
}
// Initializes this Image object. This is identical to calling the image's
// constructor with picture, but this allows non-constructor initialization,
// which may be necessary in some instances.
private void Initialize(int width, int height)
{
if (_iconData is null || !_handle.IsNull)
{
throw new InvalidOperationException(SR.Format(SR.IllegalState, GetType().Name));
}
SpanReader<byte> reader = new(_iconData);
if (!reader.TryRead(out ICONDIR dir)
|| dir.idReserved != 0
|| dir.idType != 1
|| dir.idCount == 0)
{
throw new ArgumentException(SR.Format(SR.InvalidPictureType, "picture", nameof(Icon)));
}
// Get the correct width and height.
if (width == 0)
{
width = PInvokeCore.GetSystemMetrics(SYSTEM_METRICS_INDEX.SM_CXICON);
}
if (height == 0)
{
height = PInvokeCore.GetSystemMetrics(SYSTEM_METRICS_INDEX.SM_CYICON);
}
if (s_bitDepth == 0)
{
using var hdc = GetDcScope.ScreenDC;
s_bitDepth = PInvokeCore.GetDeviceCaps(hdc, GET_DEVICE_CAPS_INDEX.BITSPIXEL);
s_bitDepth *= PInvokeCore.GetDeviceCaps(hdc, GET_DEVICE_CAPS_INDEX.PLANES);
// If the bit depth is 8, make it 4 because windows does not
// choose a 256 color icon if the display is running in 256 color mode
// due to palette flicker.
if (s_bitDepth == 8)
{
s_bitDepth = 4;
}
}
byte bestWidth = 0;
byte bestHeight = 0;
if (!reader.TryRead(dir.idCount, out ReadOnlySpan<ICONDIRENTRY> entries))
{
throw new ArgumentException(SR.Format(SR.InvalidPictureType, "picture", nameof(Icon)));
}
foreach (ICONDIRENTRY entry in entries)
{
bool fUpdateBestFit = false;
uint iconBitDepth;
if (entry.bColorCount != 0)
{
iconBitDepth = 4;
if (entry.bColorCount < 0x10)
{
iconBitDepth = 1;
}
}
else
{
iconBitDepth = entry.wBitCount;
}
// If it looks like if nothing is specified at this point then set the bits per pixel to 8.
if (iconBitDepth == 0)
{
iconBitDepth = 8;
}
// Windows rules for specifying an icon:
//
// 1. The icon with the closest size match.
// 2. For matching sizes, the image with the closest bit depth.
// 3. If there is no color depth match, the icon with the closest color depth that does not exceed the display.
// 4. If all icon color depth > display, lowest color depth is chosen.
// 5. color depth of > 8bpp are all equal.
// 6. Never choose an 8bpp icon on an 8bpp system.
if (_bestBytesInRes == 0)
{
fUpdateBestFit = true;
}
else
{
int bestDelta = Math.Abs(bestWidth - width) + Math.Abs(bestHeight - height);
int thisDelta = Math.Abs(entry.bWidth - width) + Math.Abs(entry.bHeight - height);
if ((thisDelta < bestDelta)
|| (thisDelta == bestDelta
&& ((iconBitDepth <= s_bitDepth && iconBitDepth > _bestBitDepth)
|| (_bestBitDepth > s_bitDepth && iconBitDepth < _bestBitDepth))))
{
fUpdateBestFit = true;
}
}
if (fUpdateBestFit)
{
bestWidth = entry.bWidth;
bestHeight = entry.bHeight;
_bestImageOffset = entry.dwImageOffset;
_bestBytesInRes = entry.dwBytesInRes;
_bestBitDepth = iconBitDepth;
}
}
if (_bestImageOffset > int.MaxValue)
{
throw new ArgumentException(SR.Format(SR.InvalidPictureType, "picture", nameof(Icon)));
}
if (_bestBytesInRes > int.MaxValue)
{
throw new Win32Exception((int)WIN32_ERROR.ERROR_INVALID_PARAMETER);
}
uint endOffset;
try
{
endOffset = checked(_bestImageOffset + _bestBytesInRes);
}
catch (OverflowException)
{
throw new Win32Exception((int)WIN32_ERROR.ERROR_INVALID_PARAMETER);
}
if (endOffset > _iconData.Length)
{
throw new ArgumentException(SR.Format(SR.InvalidPictureType, "picture", nameof(Icon)));
}
// Copy the bytes into an aligned buffer if needed.
ReadOnlySpan<byte> bestImage = reader.Span.Slice((int)_bestImageOffset, (int)_bestBytesInRes);
if ((_bestImageOffset % sizeof(nint)) != 0)
{
// Beginning of icon's content is misaligned.
using BufferScope<byte> alignedBuffer = new((int)_bestBytesInRes);
bestImage.CopyTo(alignedBuffer.AsSpan());
fixed (byte* b = alignedBuffer)
{
_handle = PInvoke.CreateIconFromResourceEx(b, (uint)bestImage.Length, fIcon: true, 0x00030000, 0, 0, 0);
}
}
else
{
fixed (byte* b = bestImage)
{
_handle = PInvoke.CreateIconFromResourceEx(b, (uint)bestImage.Length, fIcon: true, 0x00030000, 0, 0, 0);
}
}
if (_handle.IsNull)
{
throw new Win32Exception();
}
}
private void CopyBitmapData(BitmapData sourceData, BitmapData targetData)
{
byte* srcPtr = (byte*)sourceData.Scan0;
byte* destPtr = (byte*)targetData.Scan0;
Debug.Assert(sourceData.Height == targetData.Height, "Unexpected height. How did this happen?");
int height = Math.Min(sourceData.Height, targetData.Height);
long bytesToCopyEachIter = Math.Abs(targetData.Stride);
for (int i = 0; i < height; i++)
{
Buffer.MemoryCopy(srcPtr, destPtr, bytesToCopyEachIter, bytesToCopyEachIter);
srcPtr += sourceData.Stride;
destPtr += targetData.Stride;
}
GC.KeepAlive(this); // finalizer mustn't deallocate data blobs while this method is running
}
private static bool BitmapHasAlpha(BitmapData bmpData)
{
bool hasAlpha = false;
for (int i = 0; i < bmpData.Height; i++)
{
for (int j = 3; j < Math.Abs(bmpData.Stride); j += 4)
{
// Stride here is fine since we know we're doing this on the whole image.
byte* candidate = ((byte*)bmpData.Scan0.ToPointer()) + (i * bmpData.Stride) + j;
if (*candidate != 0)
{
hasAlpha = true;
return hasAlpha;
}
}
}
return false;
}
public Bitmap ToBitmap()
{
// DontSupportPngFramesInIcons is true when the application is targeting framework version below 4.6
// and false when the application is targeting 4.6 and above. Downlevel application can also set the following switch
// to false in the .config file's runtime section in order to opt-in into the new behavior:
// <AppContextSwitchOverrides value="Switch.System.Drawing.DontSupportPngFramesInIcons=false" />
if (HasPngSignature() && !LocalAppContextSwitches.DontSupportPngFramesInIcons)
{
return PngFrame();
}
return BmpFrame();
}
private Bitmap BmpFrame()
{
Bitmap? bitmap = null;
if (_iconData is not null && _bestBitDepth == 32)
{
// GDI+ doesn't handle 32 bpp icons with alpha properly
// we load the icon ourself from the byte table
bitmap = new Bitmap(Size.Width, Size.Height, PixelFormat.Format32bppArgb);
Debug.Assert(_bestImageOffset >= 0 && (_bestImageOffset + _bestBytesInRes) <= _iconData.Length, "Illegal offset/length for the Icon data");
BitmapData bmpdata = bitmap.LockBits(new Rectangle(0, 0, Size.Width, Size.Height),
ImageLockMode.WriteOnly,
PixelFormat.Format32bppArgb);
try
{
uint* pixelPtr = (uint*)bmpdata.Scan0.ToPointer();
// jumping the image header
int newOffset = (int)(_bestImageOffset + sizeof(BITMAPINFOHEADER));
// there is no color table that we need to skip since we're 32bpp
int lineLength = Size.Width * 4;
int width = Size.Width;
for (int j = (Size.Height - 1) * 4; j >= 0; j -= 4)
{
Marshal.Copy(_iconData, newOffset + j * width, (IntPtr)pixelPtr, lineLength);
pixelPtr += width;
}
// note: we ignore the mask that's available after the pixel table
}
finally
{
bitmap.UnlockBits(bmpdata);
}
}
else if (_bestBitDepth is 0 or 32)
{
// This may be a 32bpp icon or an icon without any data.
ICONINFO info = PInvokeCore.GetIconInfo(this);
BITMAP bmp = default;
try
{
if (!info.hbmColor.IsNull)
{
PInvokeCore.GetObject(info.hbmColor, sizeof(BITMAP), &bmp);
if (bmp.bmBitsPixel == 32)
{
Bitmap? tmpBitmap = null;
BitmapData? bmpData = null;
BitmapData? targetData = null;
try
{
tmpBitmap = Image.FromHbitmap(info.hbmColor);
// In GDI+ the bits are there but the bitmap was created with no alpha channel
// so copy the bits by hand to a new bitmap
// we also need to go around a limitation in the way the ICON is stored (ie if it's another bpp
// but stored in 32bpp all pixels are transparent and not opaque)
// (Here you mostly need to remain calm....)
bmpData = tmpBitmap.LockBits(new Rectangle(0, 0, tmpBitmap.Width, tmpBitmap.Height), ImageLockMode.ReadOnly, tmpBitmap.PixelFormat);
// we need do the following if the image has alpha because otherwise the image is fully transparent even though it has data
if (BitmapHasAlpha(bmpData))
{
bitmap = new Bitmap(bmpData.Width, bmpData.Height, PixelFormat.Format32bppArgb);
targetData = bitmap.LockBits(new Rectangle(0, 0, bmpData.Width, bmpData.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
CopyBitmapData(bmpData, targetData);
}
}
finally
{
if (tmpBitmap is not null && bmpData is not null)
{
tmpBitmap.UnlockBits(bmpData);
}
if (bitmap is not null && targetData is not null)
{
bitmap.UnlockBits(targetData);
}
}
tmpBitmap.Dispose();
}
}
}
finally
{
if (!info.hbmColor.IsNull)
{
PInvokeCore.DeleteObject(info.hbmColor);
}
if (!info.hbmMask.IsNull)
{
PInvokeCore.DeleteObject(info.hbmMask);
}
}
}
if (bitmap is null)
{
// last chance... all the other cases (ie non 32 bpp icons coming from a handle or from the bitmapData)
// we have to do this rather than just return Bitmap.FromHIcon because
// the bitmap returned from that, even though it's 32bpp, just paints where the mask allows it
// seems like another GDI+ weirdness. might be interesting to investigate further. In the meantime
// this looks like the right thing to do and is not more expansive that what was present before.
Size size = Size;
bitmap = new Bitmap(size.Width, size.Height); // initialized to transparent
Graphics? graphics;
using (graphics = Graphics.FromImage(bitmap))
{
try
{
using var tmpBitmap = Bitmap.FromHicon(Handle);
graphics.DrawImage(tmpBitmap, new Rectangle(0, 0, size.Width, size.Height));
}
catch (ArgumentException)
{
// Sometimes FromHicon will crash with no real reason.
// The backup plan is to just draw the image like we used to.
// NOTE: FromHIcon is also where we have the buffer overrun
// if width and height are mismatched.
Draw(graphics, new Rectangle(0, 0, size.Width, size.Height));
}
}
// GDI+ fills the surface with a sentinel color for GetDC, but does
// not correctly clean it up again, so we have to do it.
Color fakeTransparencyColor = Color.FromArgb(0x0d, 0x0b, 0x0c);
bitmap.MakeTransparent(fakeTransparencyColor);
}
Debug.Assert(bitmap is not null, "Bitmap cannot be null");
return bitmap;
}
private Bitmap PngFrame()
{
Debug.Assert(_iconData is not null);
using MemoryStream stream = new();
stream.Write(_iconData, (int)_bestImageOffset, (int)_bestBytesInRes);
return new Bitmap(stream);
}
private bool HasPngSignature()
{
if (!_isBestImagePng.HasValue)
{
if (_iconData is not null && _iconData.Length >= _bestImageOffset + 8)
{
int iconSignature1 = BitConverter.ToInt32(_iconData, (int)_bestImageOffset);
int iconSignature2 = BitConverter.ToInt32(_iconData, (int)_bestImageOffset + 4);
_isBestImagePng = (iconSignature1 == PNGSignature1) && (iconSignature2 == PNGSignature2);
}
else
{
_isBestImagePng = false;
}
}
return _isBestImagePng.Value;
}
public override string ToString() => SR.toStringIcon;
/// <summary>
/// Saves this <see cref="Icon"/> to the specified output <see cref="Stream"/>.
/// </summary>
/// <param name="outputStream">The <see cref="Stream"/> to save to.</param>
/// <exception cref="ArgumentNullException"><paramref name="outputStream"/> was <see langword="null"/>.</exception>
public unsafe void Save(Stream outputStream)
{
ArgumentNullException.ThrowIfNull(outputStream);
if (_iconData is not null)
{
outputStream.Write(_iconData, 0, _iconData.Length);
}
else
{
// Ideally, we would pick apart the icon using GetIconInfo, and then pull the individual bitmaps out,
// converting them to DIBS and saving them into the file. But, in the interest of simplicity, we just
// call to OLE to do it for us.
using var iPicture = IPicture.CreateFromIcon(this, copy: false);
using var iStream = outputStream.ToIStream(makeSeekable: true);
iPicture.Value->SaveAsFile(iStream, (BOOL)(-1), pCbSize: null).ThrowOnFailure();
}
}
#if NET8_0_OR_GREATER
/// <summary>
/// Extracts a specified icon from the given <paramref name="filePath"/>.
/// </summary>
/// <remarks>
/// <para>
/// Unlike the <see cref="Icon(string)">constructors that take a path</see> this method and the
/// <see cref="ExtractAssociatedIcon(string)"/> methods do not retain all of the resource data or modify the
/// original data (outside of resizing if necessary). As such, the <see cref="Icon"/> only uses as much
/// memory as is needed for the requested size (mostly native memory).
/// </para>
/// <para>
/// Without the original source data the <see cref="Icon(Icon, Size)">copy constructors</see> have to resample
/// the current icon's bitmap to change sizes. For best image quality, if different sizes for an <see cref="Icon"/>
/// are desired you should create separate instances with this method and avoid the copy constructors.
/// </para>
/// </remarks>
/// <param name="filePath">Path to an icon or PE (.dll, .exe) file.</param>
/// <param name="id">
/// Positive numbers refer to an icon index in the given file. Negative numbers refer to a specific native resource
/// identifier in a PE (.dll, .exe) file.
/// </param>
/// <param name="size">
/// The desired size. If the specified size does not exist, an existing size will be resampled to give the
/// requested size.
/// </param>
/// <returns>
/// An <see cref="Icon"/>, or <see langword="null"/> if an icon can't be found with the specified
/// <paramref name="id"/>.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="size"/> is negative or larger than <see cref="ushort.MaxValue"/>.
/// </exception>
/// <exception cref="IOException">
/// There given <paramref name="filePath"/> could not be accessed.
/// </exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="filePath"/> is null.
/// </exception>
public static Icon? ExtractIcon(string filePath, int id, int size)
=> size is <= 0 or > ushort.MaxValue
? throw new ArgumentOutOfRangeException(nameof(size))
: ExtractIcon(filePath, id, size, smallIcon: false);
/// <param name="smallIcon">
/// If <see langword="true"/>, gets the <see cref="Icon"/> at the current system small icon size setting. If
/// <see langword="false"/>, gets the <see cref="Icon"/> at the current system large icon size setting. Default is
/// <see langword="false"/>.
/// </param>
/// <inheritdoc cref="ExtractIcon(string, int, int)" />
public static Icon? ExtractIcon(string filePath, int id, bool smallIcon = false)
=> ExtractIcon(filePath, id, 0, smallIcon);
private static Icon? ExtractIcon(string filePath, int id, int size, bool smallIcon = false)
{
ArgumentNullException.ThrowIfNull(filePath);
Debug.Assert(size is >= 0 and <= ushort.MaxValue);
HICON hicon = HICON.Null;
HRESULT result;
fixed (char* c = filePath)
{
result = PInvoke.SHDefExtractIcon(
c,
id,
0,
smallIcon ? null : &hicon,
smallIcon ? &hicon : null,
(uint)(ushort)size << 16 | (ushort)size);
}
if (result == HRESULT.S_FALSE)
{
// Icon wasn't found
return null;
}
// This only throws if there is an error.
try
{
Marshal.ThrowExceptionForHR((int)result);
}
catch (COMException ex)
{
// This API is only documented to return E_FAIL, which surfaces as COMException. Wrap in a "nicer"
// ArgumentException.
throw new IOException(SR.IconCouldNotBeExtracted, ex);
}
return new Icon(hicon, takeOwnership: true);
}
#endif
}