/
ToolboxBitmapAttribute.cs
332 lines (285 loc) · 11.5 KB
/
ToolboxBitmapAttribute.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing.Imaging;
using System.IO;
using DpiHelper = System.Windows.Forms.DpiHelper;
using Gdip = System.Drawing.SafeNativeMethods.Gdip;
namespace System.Drawing
{
/// <summary>
/// ToolboxBitmapAttribute defines the images associated with a specified component.
/// The component can offer a small and large image (large is optional).
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class ToolboxBitmapAttribute : Attribute
{
private Image? _smallImage;
private Image? _largeImage;
private readonly string? _imageFile;
private readonly Type? _imageType;
private readonly string? _imageName;
private static readonly Size s_largeSize = new Size(32, 32);
private static readonly Size s_smallSize = new Size(16, 16);
public ToolboxBitmapAttribute(string imageFile) : this(GetImageFromFile(imageFile, false), GetImageFromFile(imageFile, true))
{
_imageFile = imageFile;
}
public ToolboxBitmapAttribute(Type t) : this(GetImageFromResource(t, null, false), GetImageFromResource(t, null, true))
{
_imageType = t;
}
public ToolboxBitmapAttribute(Type t, string name)
: this(GetImageFromResource(t, name, false), GetImageFromResource(t, name, true))
{
_imageType = t;
_imageName = name;
}
private ToolboxBitmapAttribute(Image? smallImage, Image? largeImage)
{
_smallImage = smallImage;
_largeImage = largeImage;
}
public override bool Equals([NotNullWhen(true)] object? value)
{
if (value == this)
{
return true;
}
if (value is ToolboxBitmapAttribute attr)
{
return attr._smallImage == _smallImage && attr._largeImage == _largeImage;
}
return false;
}
public override int GetHashCode() => base.GetHashCode();
public Image? GetImage(object? component) => GetImage(component, true);
public Image? GetImage(object? component, bool large)
{
if (component != null)
{
return GetImage(component.GetType(), large);
}
return null;
}
public Image? GetImage(Type type) => GetImage(type, false);
public Image? GetImage(Type type, bool large) => GetImage(type, null, large);
public Image? GetImage(Type type, string? imgName, bool large)
{
if ((large && _largeImage == null) || (!large && _smallImage == null))
{
Image? img = large ? _largeImage : _smallImage;
img ??= GetImageFromResource(type, imgName, large);
// last resort for large images.
if (large && _largeImage == null && _smallImage != null)
{
img = new Bitmap((Bitmap)_smallImage, s_largeSize.Width, s_largeSize.Height);
}
if (img is Bitmap b)
{
MakeBackgroundAlphaZero(b);
}
if (img == null)
{
img = s_defaultComponent.GetImage(type, large);
// We don't want to hand out the static shared image
// because otherwise it might get disposed.
if (img != null)
{
img = (Image)img.Clone();
}
}
if (large)
{
_largeImage = img;
}
else
{
_smallImage = img;
}
}
Image? toReturn = (large) ? _largeImage : _smallImage;
if (Equals(Default))
{
_largeImage = null;
_smallImage = null;
}
return toReturn;
}
// Helper to get the right icon from the given stream that represents an icon.
private static Image? GetIconFromStream(Stream? stream, bool large, bool scaled)
{
if (stream == null)
{
return null;
}
Icon ico = new Icon(stream);
Icon sizedico = new Icon(ico, large ? s_largeSize : s_smallSize);
Bitmap? b = sizedico.ToBitmap();
if (DpiHelper.IsScalingRequired && scaled)
{
DpiHelper.ScaleBitmapLogicalToDevice(ref b);
}
return b;
}
// Just forwards to Image.FromFile eating any non-critical exceptions that may result.
private static Image? GetImageFromFile(string? imageFile, bool large, bool scaled = true)
{
Image? image = null;
try
{
if (imageFile != null)
{
string? ext = Path.GetExtension(imageFile);
if (ext != null && string.Equals(ext, ".ico", StringComparison.OrdinalIgnoreCase))
{
//ico files support both large and small, so we respect the large flag here.
using (FileStream reader = File.OpenRead(imageFile!))
{
image = GetIconFromStream(reader, large, scaled);
}
}
else if (!large)
{
//we only read small from non-ico files.
image = Image.FromFile(imageFile!);
Bitmap? b = image as Bitmap;
if (DpiHelper.IsScalingRequired && scaled)
{
DpiHelper.ScaleBitmapLogicalToDevice(ref b);
}
}
}
}
catch (Exception e) when (!ClientUtils.IsCriticalException(e))
{
}
return image;
}
private static Image? GetBitmapFromResource(Type t, string? bitmapname, bool large, bool scaled)
{
if (bitmapname == null)
{
return null;
}
Image? img = null;
// Load the image from the manifest resources.
Stream? stream = BitmapSelector.GetResourceStream(t, bitmapname);
if (stream != null)
{
Bitmap? b = new Bitmap(stream);
img = b;
MakeBackgroundAlphaZero(b);
if (large)
{
img = new Bitmap(b, s_largeSize.Width, s_largeSize.Height);
}
if (DpiHelper.IsScalingRequired && scaled)
{
b = (Bitmap)img;
DpiHelper.ScaleBitmapLogicalToDevice(ref b);
img = b;
}
}
return img;
}
private static Image? GetIconFromResource(Type t, string? bitmapname, bool large, bool scaled)
{
if (bitmapname == null)
{
return null;
}
return GetIconFromStream(BitmapSelector.GetResourceStream(t, bitmapname), large, scaled);
}
public static Image? GetImageFromResource(Type t, string? imageName, bool large)
{
return GetImageFromResource(t, imageName, large, scaled: true);
}
internal static Image? GetImageFromResource(Type t, string? imageName, bool large, bool scaled)
{
Image? img = null;
try
{
string? name = imageName;
string? iconname = null;
string? bmpname = null;
string? rawbmpname = null;
// If we didn't get a name, use the class name
if (name == null)
{
name = t.FullName!;
int indexDot = name.LastIndexOf('.');
if (indexDot != -1)
{
name = name.Substring(indexDot + 1);
}
// All bitmap images from winforms runtime are changed to Icons
// and logical names, now, does not contain any extension.
rawbmpname = name;
iconname = name + ".ico";
bmpname = name + ".bmp";
}
else
{
if (string.Equals(Path.GetExtension(imageName), ".ico", StringComparison.CurrentCultureIgnoreCase))
{
iconname = name;
}
else if (string.Equals(Path.GetExtension(imageName), ".bmp", StringComparison.CurrentCultureIgnoreCase))
{
bmpname = name;
}
else
{
// We don't recognize the name as either bmp or ico. we need to try three things.
// 1. the name as a bitmap (back compat)
// 2. name+.bmp
// 3. name+.ico
rawbmpname = name;
bmpname = name + ".bmp";
iconname = name + ".ico";
}
}
if (rawbmpname != null)
{
img = GetBitmapFromResource(t, rawbmpname, large, scaled);
}
if (img == null && bmpname != null)
{
img = GetBitmapFromResource(t, bmpname, large, scaled);
}
if (img == null && iconname != null)
{
img = GetIconFromResource(t, iconname, large, scaled);
}
}
catch (Exception) { }
return img;
}
private static void MakeBackgroundAlphaZero(Bitmap img)
{
// Bitmap derived from Icon is already transparent.
if (img.RawFormat.Guid == ImageFormat.Icon.Guid)
return;
Color bottomLeft = img.GetPixel(0, img.Height - 1);
img.MakeTransparent();
Color newBottomLeft = Color.FromArgb(0, bottomLeft);
img.SetPixel(0, img.Height - 1, newBottomLeft);
}
public static readonly ToolboxBitmapAttribute Default = new ToolboxBitmapAttribute(null, (Image?)null);
private static readonly ToolboxBitmapAttribute s_defaultComponent;
#pragma warning disable CA1810 // DummyFunction apparently needs to be invoked prior to the rest of the initialization
static ToolboxBitmapAttribute()
{
// When we call Gdip.DummyFunction, JIT will make sure Gdip..cctor will be called.
Gdip.DummyFunction();
Stream? stream = BitmapSelector.GetResourceStream(typeof(ToolboxBitmapAttribute), "DefaultComponent.bmp");
Debug.Assert(stream != null, "DefaultComponent.bmp must be present as an embedded resource.");
var bitmap = new Bitmap(stream);
MakeBackgroundAlphaZero(bitmap);
s_defaultComponent = new ToolboxBitmapAttribute(bitmap, null);
}
#pragma warning restore CA1810
}
}