-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFancyBackgroundComponent.cs
More file actions
866 lines (759 loc) · 32.3 KB
/
Copy pathFancyBackgroundComponent.cs
File metadata and controls
866 lines (759 loc) · 32.3 KB
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Xml;
using LiveSplit.Model;
using LiveSplit.UI;
namespace LiveSplit.UI.Components
{
public enum FancyBackgroundColorMode
{
Solid,
Gradient
}
public enum FancyBackgroundGradientDirection
{
Horizontal,
Vertical,
DiagonalDown,
DiagonalUp
}
public class FancyBackgroundComponent : IComponent
{
internal const float DrawSlot = 1f;
private const float ShadowRenderScale = 1f;
private readonly FancyBackgroundSettings _settings;
private bool _hasAppliedPadding;
private bool _ownsWindowPadding;
private FancyBackgroundPadding _appliedPadding;
private FancyBackgroundPadding _appliedLayoutPadding;
private LiveSplitState _lastState;
private LayoutMode _lastMode;
private Bitmap _cachedLayer;
private string _cachedLayerKey;
public FancyBackgroundComponent(LiveSplitState state)
{
_settings = new FancyBackgroundSettings(state, this);
_lastState = state;
_lastMode = GetStateLayoutMode(state);
}
internal FancyBackgroundComponent(LiveSplitState state, FancyBackgroundSettings settings)
{
_settings = settings ?? new FancyBackgroundSettings(state, this);
_settings.AttachOwner(this);
_lastState = state;
_lastMode = GetStateLayoutMode(state);
}
public string ComponentName
{
get
{
return "Fancy Background";
}
}
public float VerticalHeight
{
get
{
return _settings.IsLayerActive
? _settings.GetTotalPadding().Vertical + DrawSlot
: 0f;
}
}
public float MinimumHeight { get { return 0f; } }
public float HorizontalWidth
{
get
{
return _settings.IsLayerActive
? _settings.GetTotalPadding().Horizontal + DrawSlot
: 0f;
}
}
public float MinimumWidth { get { return 0f; } }
public float PaddingTop { get { return 0f; } }
public float PaddingBottom { get { return 0f; } }
public float PaddingLeft { get { return 0f; } }
public float PaddingRight { get { return 0f; } }
public IDictionary<string, Action> ContextMenuControls { get { return null; } }
public Control GetSettingsControl(LayoutMode mode)
{
_settings.NotifyLayoutMode(mode);
return _settings;
}
public XmlNode GetSettings(XmlDocument document)
{
return _settings.GetSettings(document);
}
public void SetSettings(XmlNode settings)
{
_settings.SetSettings(settings);
_appliedLayoutPadding = _settings.GetTotalPadding();
_appliedPadding = GetWindowResizePadding(_appliedLayoutPadding, _lastMode);
_hasAppliedPadding = true;
InvalidateLayerCache();
}
public void Update(IInvalidator invalidator, LiveSplitState state, float width, float height, LayoutMode mode)
{
_lastState = state;
_lastMode = mode;
FancyBackgroundRuntime.Publish(this, state, _settings);
FancyBackgroundRuntime.ScheduleControllerFirst(state, this);
FancyBackgroundRuntime.UpdateLayoutProxies(state, mode);
if (FancyBackgroundRuntime.IsActiveLayoutOwner(state, this))
{
ApplyWindowPadding(state, _settings.GetTotalPadding(), mode, true);
}
if (invalidator != null
&& _settings.HasVisibleLayer
&& FancyBackgroundRuntime.IsActiveLayoutOwner(state, this))
{
invalidator.Invalidate(0, 0, 1f, 1f);
}
}
public void DrawHorizontal(Graphics g, LiveSplitState state, float height, Region clipRegion)
{
DrawLayerAndShift(g, state, LayoutMode.Horizontal);
}
public void DrawVertical(Graphics g, LiveSplitState state, float width, Region clipRegion)
{
DrawLayerAndShift(g, state, LayoutMode.Vertical);
}
public void Dispose()
{
try
{
if (_ownsWindowPadding)
{
ApplyWindowPadding(_lastState, FancyBackgroundPadding.Empty, _lastMode, false);
}
}
finally
{
FancyBackgroundRuntime.Unpublish(this);
FancyBackgroundRuntime.UpdateLayoutProxies(_lastState, _lastMode);
InvalidateLayerCache();
}
}
internal void NotifySettingsChanged()
{
InvalidateLayerCache();
if (_lastState == null)
{
return;
}
FancyBackgroundRuntime.Publish(this, _lastState, _settings);
FancyBackgroundRuntime.ScheduleControllerFirst(_lastState, this);
FancyBackgroundRuntime.UpdateLayoutProxies(_lastState, _lastMode);
if (FancyBackgroundRuntime.IsActiveLayoutOwner(_lastState, this))
{
ApplyWindowPadding(_lastState, _settings.GetTotalPadding(), _lastMode, true);
}
FancyBackgroundRuntime.Invalidate(_lastState);
}
private void DrawLayerAndShift(Graphics g, LiveSplitState state, LayoutMode mode)
{
FancyBackgroundRuntime.Publish(this, state, _settings);
bool activeOwner = FancyBackgroundRuntime.IsActiveLayoutOwner(state, this);
if (activeOwner)
{
DrawBackgroundLayer(g, state);
ApplyContentTransform(g, mode);
}
}
private void DrawBackgroundLayer(Graphics g, LiveSplitState state)
{
if (!_settings.HasVisibleLayer)
{
return;
}
Form form = state != null ? state.Form : null;
if (form == null || form.IsDisposed)
{
return;
}
int width = Math.Max(1, form.ClientSize.Width > 0 ? form.ClientSize.Width : form.Width);
int height = Math.Max(1, form.ClientSize.Height > 0 ? form.ClientSize.Height : form.Height);
float layerScale = GetManualResizeScale(state, _lastMode);
string cacheKey = BuildLayerCacheKey(width, height, layerScale);
if (_cachedLayer == null || !string.Equals(_cachedLayerKey, cacheKey, StringComparison.Ordinal))
{
InvalidateLayerCache();
_cachedLayer = CreateLayerBitmap(width, height, layerScale);
_cachedLayerKey = cacheKey;
}
Matrix transform = g.Transform.Clone();
Region clip = g.Clip.Clone();
try
{
g.ResetTransform();
g.ResetClip();
g.DrawImageUnscaled(_cachedLayer, 0, 0);
}
finally
{
g.Transform = transform;
g.Clip = clip;
transform.Dispose();
clip.Dispose();
}
}
private Bitmap CreateLayerBitmap(int width, int height, float scale)
{
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppPArgb);
RectangleF rect = GetBackgroundRect(width, height, scale);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.Clear(Color.Transparent);
ApplyLayerQuality(g);
if (_settings.HasVisibleShadow)
{
DrawShadowLayer(g, width, height, rect, scale);
}
if (_settings.HasVisibleBackground)
{
DrawBackgroundFill(g, rect, scale);
}
if (_settings.HasVisibleOutline)
{
DrawOutline(g, rect, scale);
}
}
return bitmap;
}
private void DrawBackgroundFill(Graphics g, RectangleF rect, float scale)
{
using (GraphicsPath path = CreateRoundRectPath(rect,
_settings.RadiusTopLeft * scale,
_settings.RadiusTopRight * scale,
_settings.RadiusBottomRight * scale,
_settings.RadiusBottomLeft * scale))
using (Brush brush = CreateBrush(rect,
_settings.BackgroundColorMode,
_settings.BackgroundColor,
_settings.BackgroundColor2,
_settings.BackgroundColor3,
_settings.BackgroundColorCount,
_settings.BackgroundGradientDirection))
{
g.FillPath(brush, path);
}
}
private void DrawOutline(Graphics g, RectangleF rect, float scale)
{
float outlineSize = Math.Max(0f, _settings.OutlineSize * scale);
float inset = outlineSize / 2f;
RectangleF outlineRect = rect;
outlineRect.Inflate(-inset, -inset);
if (outlineRect.Width <= 0f || outlineRect.Height <= 0f)
{
return;
}
using (GraphicsPath path = CreateRoundRectPath(outlineRect,
Math.Max(0f, (_settings.RadiusTopLeft * scale) - inset),
Math.Max(0f, (_settings.RadiusTopRight * scale) - inset),
Math.Max(0f, (_settings.RadiusBottomRight * scale) - inset),
Math.Max(0f, (_settings.RadiusBottomLeft * scale) - inset)))
using (Brush brush = CreateBrush(outlineRect,
_settings.OutlineColorMode,
_settings.OutlineColor,
_settings.OutlineColor2,
_settings.OutlineColor3,
_settings.OutlineColorCount,
_settings.OutlineGradientDirection))
using (Pen pen = new Pen(brush, outlineSize))
{
pen.LineJoin = LineJoin.Round;
g.DrawPath(pen, path);
}
}
private void DrawShadowLayer(Graphics target, int width, int height, RectangleF rect, float scale)
{
int shadowWidth = Math.Max(1, (int)Math.Ceiling(width * ShadowRenderScale));
int shadowHeight = Math.Max(1, (int)Math.Ceiling(height * ShadowRenderScale));
using (Bitmap shadow = new Bitmap(shadowWidth, shadowHeight, PixelFormat.Format32bppPArgb))
{
using (Graphics g = Graphics.FromImage(shadow))
{
g.Clear(Color.Transparent);
ApplyLayerQuality(g);
g.ScaleTransform(ShadowRenderScale, ShadowRenderScale);
float spread = GetShadowSpread() * scale;
RectangleF shadowRect = rect;
shadowRect.Offset(_settings.ShadowSize * scale, _settings.ShadowSize * scale);
shadowRect.Inflate(spread, spread);
using (GraphicsPath path = CreateRoundRectPath(shadowRect,
(_settings.RadiusTopLeft * scale) + spread,
(_settings.RadiusTopRight * scale) + spread,
(_settings.RadiusBottomRight * scale) + spread,
(_settings.RadiusBottomLeft * scale) + spread))
using (Brush brush = new SolidBrush(GetShadowColor()))
{
g.FillPath(brush, path);
}
}
int blurRadius = Math.Min(100, Math.Max(0, RoundSize(_settings.ShadowBlur * scale * ShadowRenderScale)));
if (blurRadius > 0)
{
BlurBitmap(shadow, blurRadius);
}
if (_settings.ShadowOutsideEnabled)
{
using (Graphics g = Graphics.FromImage(shadow))
using (GraphicsPath path = CreateRoundRectPath(rect,
_settings.RadiusTopLeft * scale,
_settings.RadiusTopRight * scale,
_settings.RadiusBottomRight * scale,
_settings.RadiusBottomLeft * scale))
using (Brush clearBrush = new SolidBrush(Color.Transparent))
{
ApplyLayerQuality(g);
g.ScaleTransform(ShadowRenderScale, ShadowRenderScale);
CompositingMode mode = g.CompositingMode;
g.CompositingMode = CompositingMode.SourceCopy;
g.FillPath(clearBrush, path);
g.CompositingMode = mode;
}
}
target.DrawImage(shadow,
new Rectangle(0, 0, width, height),
new Rectangle(0, 0, shadowWidth, shadowHeight),
GraphicsUnit.Pixel);
}
}
private RectangleF GetBackgroundRect(int width, int height, float scale)
{
FancyBackgroundPadding padding = _settings.GetShadowPadding();
float left = Math.Min(padding.Left * scale, Math.Max(0f, width - 1f));
float top = Math.Min(padding.Top * scale, Math.Max(0f, height - 1f));
float right = Math.Min(padding.Right * scale, Math.Max(0f, width - left - 1f));
float bottom = Math.Min(padding.Bottom * scale, Math.Max(0f, height - top - 1f));
return new RectangleF(left, top,
Math.Max(1f, width - left - right),
Math.Max(1f, height - top - bottom));
}
private float GetShadowSpread()
{
return Math.Max(0f, (_settings.ShadowSizePercent - 100) / 10f);
}
private Color GetShadowColor()
{
int alpha = Math.Min(255, Math.Max(0, (int)Math.Round(_settings.ShadowColor.A * (_settings.ShadowMultiply / 100f))));
return Color.FromArgb(alpha, _settings.ShadowColor.R, _settings.ShadowColor.G, _settings.ShadowColor.B);
}
private static void ApplyLayerQuality(Graphics g)
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.CompositingQuality = CompositingQuality.GammaCorrected;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
}
private string BuildLayerCacheKey(int width, int height, float scale)
{
return string.Join("|", new object[]
{
width,
height,
Math.Round(scale, 4),
_settings.BackgroundEnabled,
_settings.BackgroundColorMode,
_settings.BackgroundColor.ToArgb(),
_settings.BackgroundColor2.ToArgb(),
_settings.BackgroundColor3.ToArgb(),
_settings.BackgroundColorCount,
_settings.BackgroundGradientDirection,
_settings.OutlineEnabled,
_settings.OutlineColorMode,
_settings.OutlineColor.ToArgb(),
_settings.OutlineColor2.ToArgb(),
_settings.OutlineColor3.ToArgb(),
_settings.OutlineColorCount,
_settings.OutlineGradientDirection,
_settings.OutlineSize,
_settings.ShadowEnabled,
_settings.ShadowOutsideEnabled,
_settings.ShadowColor.ToArgb(),
_settings.ShadowSize,
_settings.ShadowSizePercent,
_settings.ShadowBlur,
_settings.ShadowMultiply,
_settings.ShadowPaddingTop,
_settings.ShadowPaddingRight,
_settings.ShadowPaddingBottom,
_settings.ShadowPaddingLeft,
_settings.PaddingTop,
_settings.PaddingRight,
_settings.PaddingBottom,
_settings.PaddingLeft,
_settings.RadiusTopLeft,
_settings.RadiusTopRight,
_settings.RadiusBottomRight,
_settings.RadiusBottomLeft
});
}
private void InvalidateLayerCache()
{
if (_cachedLayer != null)
{
_cachedLayer.Dispose();
_cachedLayer = null;
}
_cachedLayerKey = null;
}
private void ApplyContentTransform(Graphics g, LayoutMode mode)
{
if (!_settings.IsLayerActive)
{
return;
}
FancyBackgroundPadding padding = _settings.GetTotalPadding();
if (mode == LayoutMode.Vertical)
{
g.TranslateTransform(padding.Left, -(padding.Bottom + DrawSlot));
}
else
{
g.TranslateTransform(-(padding.Right + DrawSlot), padding.Top);
}
}
internal static float GetCurrentScale(Graphics g)
{
if (g == null)
{
return 1f;
}
float[] elements = g.Transform.Elements;
float scale = elements != null && elements.Length > 0 ? Math.Abs(elements[0]) : 1f;
return scale > 0.001f ? scale : 1f;
}
private void ApplyWindowPadding(LiveSplitState state, FancyBackgroundPadding desiredLayoutPadding,
LayoutMode mode, bool markOwnership)
{
Form form = state != null ? state.Form : null;
if (form == null || form.IsDisposed)
{
return;
}
if (!_hasAppliedPadding)
{
_appliedPadding = FancyBackgroundPadding.Empty;
_appliedLayoutPadding = FancyBackgroundPadding.Empty;
_hasAppliedPadding = true;
}
FancyBackgroundPadding desiredWindowPadding = GetWindowResizePadding(desiredLayoutPadding, mode);
if (_appliedPadding.NearlyEquals(desiredWindowPadding))
{
_appliedLayoutPadding = desiredLayoutPadding;
return;
}
if (form.InvokeRequired)
{
form.BeginInvoke(new Action(() => ApplyWindowPadding(state, desiredLayoutPadding, mode, markOwnership)));
return;
}
float scale = GetManualResizeScale(state, mode);
int deltaLeft = RoundSize((desiredWindowPadding.Left - _appliedPadding.Left) * scale);
int deltaTop = RoundSize((desiredWindowPadding.Top - _appliedPadding.Top) * scale);
int deltaRight = RoundSize((desiredWindowPadding.Right - _appliedPadding.Right) * scale);
int deltaBottom = RoundSize((desiredWindowPadding.Bottom - _appliedPadding.Bottom) * scale);
if (deltaLeft != 0 || deltaTop != 0 || deltaRight != 0 || deltaBottom != 0)
{
Size oldSize = form.Size;
Point oldLocation = form.Location;
int newWidth = Math.Max(1, oldSize.Width + deltaLeft + deltaRight);
int newHeight = Math.Max(1, oldSize.Height + deltaTop + deltaBottom);
form.Location = new Point(oldLocation.X - deltaLeft, oldLocation.Y - deltaTop);
form.Size = new Size(newWidth, newHeight);
if (markOwnership)
{
_ownsWindowPadding = true;
}
}
_appliedPadding = desiredWindowPadding;
_appliedLayoutPadding = desiredLayoutPadding;
}
private static FancyBackgroundPadding GetWindowResizePadding(FancyBackgroundPadding padding, LayoutMode mode)
{
if (mode == LayoutMode.Vertical)
{
return new FancyBackgroundPadding(0f, padding.Right, 0f, padding.Left);
}
return new FancyBackgroundPadding(padding.Top, 0f, padding.Bottom, 0f);
}
private float GetManualResizeScale(LiveSplitState state, LayoutMode mode)
{
Form form = state != null ? state.Form : null;
if (form == null || form.IsDisposed)
{
return 1f;
}
float overallSize = CalculateOverallSize(state, mode, _appliedLayoutPadding);
float primarySize = mode == LayoutMode.Vertical ? form.Height : form.Width;
if (overallSize <= 0.001f || primarySize <= 0f)
{
return 1f;
}
return primarySize / overallSize;
}
private float CalculateOverallSize(LiveSplitState state, LayoutMode mode, FancyBackgroundPadding ownerPadding)
{
if (state == null || state.Layout == null || state.Layout.Components == null)
{
return 1f;
}
List<IComponent> components = new List<IComponent>();
foreach (IComponent component in state.Layout.Components)
{
if (component != null)
{
components.Add(component);
}
}
float totalSize = 0f;
for (int index = 0; index < components.Count; index++)
{
if (mode == LayoutMode.Vertical)
{
float bottomPadding = Math.Min(GetPaddingBelow(components, index, ownerPadding), components[index].PaddingBottom) / 2f;
totalSize += GetComponentPrimarySize(components[index], mode, ownerPadding) - (bottomPadding * 2f);
}
else
{
float rightPadding = Math.Min(GetPaddingToRight(components, index, ownerPadding), components[index].PaddingRight) / 2f;
totalSize += GetComponentPrimarySize(components[index], mode, ownerPadding) - (rightPadding * 2f);
}
}
return Math.Max(totalSize, 1f);
}
private float GetPaddingBelow(List<IComponent> components, int index, FancyBackgroundPadding ownerPadding)
{
while (index < components.Count - 1)
{
index++;
IComponent component = components[index];
if (GetComponentPrimarySize(component, LayoutMode.Vertical, ownerPadding) != 0f)
{
return component.PaddingTop;
}
}
return 0f;
}
private float GetPaddingToRight(List<IComponent> components, int index, FancyBackgroundPadding ownerPadding)
{
while (index < components.Count - 1)
{
index++;
IComponent component = components[index];
if (GetComponentPrimarySize(component, LayoutMode.Horizontal, ownerPadding) != 0f)
{
return component.PaddingRight;
}
}
return 0f;
}
private float GetComponentPrimarySize(IComponent component, LayoutMode mode, FancyBackgroundPadding ownerPadding)
{
IComponent unwrapped = FancyBackgroundRuntime.Unwrap(component);
if (ReferenceEquals(unwrapped, this))
{
if (!_settings.IsLayerActive)
{
return 0f;
}
return mode == LayoutMode.Vertical
? ownerPadding.Vertical + DrawSlot
: ownerPadding.Horizontal + DrawSlot;
}
return mode == LayoutMode.Vertical
? component.VerticalHeight
: component.HorizontalWidth;
}
private static int RoundSize(float value)
{
return (int)Math.Round(value, MidpointRounding.AwayFromZero);
}
private static LayoutMode GetStateLayoutMode(LiveSplitState state)
{
return state != null && state.Layout != null
? state.Layout.Mode
: LayoutMode.Vertical;
}
private static void BlurBitmap(Bitmap bitmap, int radius)
{
if (bitmap == null || radius <= 0)
{
return;
}
Rectangle bounds = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
BitmapData data = bitmap.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb);
try
{
int width = bitmap.Width;
int height = bitmap.Height;
int stride = Math.Abs(data.Stride);
byte[] raw = new byte[stride * height];
Marshal.Copy(data.Scan0, raw, 0, raw.Length);
byte[] source = new byte[width * height * 4];
for (int y = 0; y < height; y++)
{
Buffer.BlockCopy(raw, y * stride, source, y * width * 4, width * 4);
}
byte[] horizontal = new byte[source.Length];
byte[] blurred = new byte[source.Length];
int windowSize = (radius * 2) + 1;
for (int y = 0; y < height; y++)
{
int blue = 0;
int green = 0;
int red = 0;
int alpha = 0;
for (int sampleX = -radius; sampleX <= radius; sampleX++)
{
AddPixel(source, ((y * width) + Clamp(sampleX, 0, width - 1)) * 4,
ref blue, ref green, ref red, ref alpha, 1);
}
for (int x = 0; x < width; x++)
{
int output = ((y * width) + x) * 4;
horizontal[output] = (byte)(blue / windowSize);
horizontal[output + 1] = (byte)(green / windowSize);
horizontal[output + 2] = (byte)(red / windowSize);
horizontal[output + 3] = (byte)(alpha / windowSize);
AddPixel(source, ((y * width) + Clamp(x - radius, 0, width - 1)) * 4,
ref blue, ref green, ref red, ref alpha, -1);
AddPixel(source, ((y * width) + Clamp(x + radius + 1, 0, width - 1)) * 4,
ref blue, ref green, ref red, ref alpha, 1);
}
}
for (int x = 0; x < width; x++)
{
int blue = 0;
int green = 0;
int red = 0;
int alpha = 0;
for (int sampleY = -radius; sampleY <= radius; sampleY++)
{
AddPixel(horizontal, ((Clamp(sampleY, 0, height - 1) * width) + x) * 4,
ref blue, ref green, ref red, ref alpha, 1);
}
for (int y = 0; y < height; y++)
{
int output = ((y * width) + x) * 4;
blurred[output] = (byte)(blue / windowSize);
blurred[output + 1] = (byte)(green / windowSize);
blurred[output + 2] = (byte)(red / windowSize);
blurred[output + 3] = (byte)(alpha / windowSize);
AddPixel(horizontal, ((Clamp(y - radius, 0, height - 1) * width) + x) * 4,
ref blue, ref green, ref red, ref alpha, -1);
AddPixel(horizontal, ((Clamp(y + radius + 1, 0, height - 1) * width) + x) * 4,
ref blue, ref green, ref red, ref alpha, 1);
}
}
for (int y = 0; y < height; y++)
{
Buffer.BlockCopy(blurred, y * width * 4, raw, y * stride, width * 4);
}
Marshal.Copy(raw, 0, data.Scan0, raw.Length);
}
finally
{
bitmap.UnlockBits(data);
}
}
private static void AddPixel(byte[] pixels, int index, ref int blue, ref int green, ref int red, ref int alpha, int direction)
{
blue += pixels[index] * direction;
green += pixels[index + 1] * direction;
red += pixels[index + 2] * direction;
alpha += pixels[index + 3] * direction;
}
private static int Clamp(int value, int min, int max)
{
return Math.Min(Math.Max(value, min), max);
}
private static Brush CreateBrush(RectangleF rect, FancyBackgroundColorMode mode,
Color color1, Color color2, Color color3, int colorCount,
FancyBackgroundGradientDirection direction)
{
if (mode != FancyBackgroundColorMode.Gradient)
{
return new SolidBrush(color1);
}
PointF start;
PointF end;
switch (direction)
{
case FancyBackgroundGradientDirection.Horizontal:
start = new PointF(rect.Left, rect.Top);
end = new PointF(rect.Right, rect.Top);
break;
case FancyBackgroundGradientDirection.DiagonalDown:
start = new PointF(rect.Left, rect.Top);
end = new PointF(rect.Right, rect.Bottom);
break;
case FancyBackgroundGradientDirection.DiagonalUp:
start = new PointF(rect.Left, rect.Bottom);
end = new PointF(rect.Right, rect.Top);
break;
default:
start = new PointF(rect.Left, rect.Top);
end = new PointF(rect.Left, rect.Bottom);
break;
}
if (Math.Abs(start.X - end.X) < 0.01f && Math.Abs(start.Y - end.Y) < 0.01f)
{
end = new PointF(start.X + 1f, start.Y + 1f);
}
LinearGradientBrush brush = new LinearGradientBrush(start, end, color1, colorCount == 3 ? color3 : color2);
if (colorCount == 3)
{
brush.InterpolationColors = new ColorBlend
{
Positions = new[] { 0f, 0.5f, 1f },
Colors = new[] { color1, color2, color3 }
};
}
return brush;
}
internal static GraphicsPath CreateRoundRectPath(RectangleF rect, float topLeft,
float topRight, float bottomRight, float bottomLeft)
{
GraphicsPath path = new GraphicsPath();
float maxRadius = Math.Max(0f, Math.Min(rect.Width, rect.Height) / 2f);
topLeft = ClampRadius(topLeft, maxRadius);
topRight = ClampRadius(topRight, maxRadius);
bottomRight = ClampRadius(bottomRight, maxRadius);
bottomLeft = ClampRadius(bottomLeft, maxRadius);
AddCorner(path, rect.Left, rect.Top, topLeft, 180f);
path.AddLine(rect.Left + topLeft, rect.Top, rect.Right - topRight, rect.Top);
AddCorner(path, rect.Right - (topRight * 2f), rect.Top, topRight, 270f);
path.AddLine(rect.Right, rect.Top + topRight, rect.Right, rect.Bottom - bottomRight);
AddCorner(path, rect.Right - (bottomRight * 2f), rect.Bottom - (bottomRight * 2f), bottomRight, 0f);
path.AddLine(rect.Right - bottomRight, rect.Bottom, rect.Left + bottomLeft, rect.Bottom);
AddCorner(path, rect.Left, rect.Bottom - (bottomLeft * 2f), bottomLeft, 90f);
path.AddLine(rect.Left, rect.Bottom - bottomLeft, rect.Left, rect.Top + topLeft);
path.CloseFigure();
return path;
}
private static float ClampRadius(float radius, float maxRadius)
{
return Math.Min(Math.Max(0f, radius), maxRadius);
}
private static void AddCorner(GraphicsPath path, float x, float y, float radius, float startAngle)
{
if (radius <= 0f)
{
if (path.PointCount == 0)
{
path.StartFigure();
}
return;
}
path.AddArc(x, y, radius * 2f, radius * 2f, startAngle, 90f);
}
}
}