Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AdvancedImageBox.axaml.cs « Controls « UVtools.WPF - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6da2638ccc8b905702f936d46e5837a34b70577d (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.CompilerServices;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using UVtools.Core.Extensions;
using UVtools.WPF.Extensions;
using Bitmap = Avalonia.Media.Imaging.Bitmap;
using Brushes = Avalonia.Media.Brushes;
using Color = Avalonia.Media.Color;
using Pen = Avalonia.Media.Pen;
using Point = Avalonia.Point;
using Size = Avalonia.Size;

namespace UVtools.WPF.Controls
{
    public class AdvancedImageBox : UserControl
    {
        public ScrollBar HorizontalScrollBar { get; }
        public ScrollBar VerticalScrollBar { get; }
        public ContentControl ViewPortControl { get; }

        public Vector Offset
        {
            get => new Vector(HorizontalScrollBar.Value, VerticalScrollBar.Value);
            set
            {
                HorizontalScrollBar.Value = value.X;
                VerticalScrollBar.Value = value.Y;
                RaisePropertyChanged();
                TriggerRender();
            }
        }

        public Size Viewport => ViewPortControl.Bounds.Size;

        #region Bindable Base
        /// <summary>
        ///     Multicast event for property change notifications.
        /// </summary>
        private PropertyChangedEventHandler _propertyChanged;
        private List<string> events = new List<string>();

        public event PropertyChangedEventHandler PropertyChanged
        {
            add { _propertyChanged += value; events.Add("added"); }
            remove { _propertyChanged -= value; events.Add("removed"); }
        }
        protected bool RaiseAndSetIfChanged<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
        {
            if (EqualityComparer<T>.Default.Equals(field, value)) return false;
            field = value;
            RaisePropertyChanged(propertyName);
            return true;
        }


        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
        }

        /// <summary>
        ///     Notifies listeners that a property value has changed.
        /// </summary>
        /// <param name="propertyName">
        ///     Name of the property used to notify listeners.  This
        ///     value is optional and can be provided automatically when invoked from compilers
        ///     that support <see cref="CallerMemberNameAttribute" />.
        /// </param>
        protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            OnPropertyChanged(e);
            _propertyChanged?.Invoke(this, e);
        }
        #endregion

        #region Sub Classes

        /// <summary>
        /// Represents available levels of zoom in an <see cref="ImageBox"/> control
        /// </summary>
        public class ZoomLevelCollection : IList<int>
        {
            #region Public Constructors

            /// <summary>
            /// Initializes a new instance of the <see cref="ZoomLevelCollection"/> class.
            /// </summary>
            public ZoomLevelCollection()
            {
                List = new SortedList<int, int>();
            }

            /// <summary>
            /// Initializes a new instance of the <see cref="ZoomLevelCollection"/> class.
            /// </summary>
            /// <param name="collection">The default values to populate the collection with.</param>
            /// <exception cref="System.ArgumentNullException">Thrown if the <c>collection</c> parameter is null</exception>
            public ZoomLevelCollection(IEnumerable<int> collection)
              : this()
            {
                if (collection == null)
                {
                    throw new ArgumentNullException(nameof(collection));
                }

                AddRange(collection);
            }

            #endregion

            #region Public Class Properties

            /// <summary>
            /// Returns the default zoom levels
            /// </summary>
            public static ZoomLevelCollection Default
            {
                get
                {
                    return new ZoomLevelCollection(new[]
                                                   {
                                         7, 10, 15, 20, 25, 30, 50, 70, 100, 150, 200, 300, 400, 500, 600, 700, 800, 1200, 1600, 3200
                                       });
                }
            }

            #endregion

            #region Public Properties

            /// <summary>
            /// Gets the number of elements contained in the <see cref="ZoomLevelCollection" />.
            /// </summary>
            /// <returns>
            /// The number of elements contained in the <see cref="ZoomLevelCollection" />.
            /// </returns>
            public int Count => List.Count;

            /// <summary>
            /// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1" /> is read-only.
            /// </summary>
            /// <value><c>true</c> if this instance is read only; otherwise, <c>false</c>.</value>
            /// <returns>true if the <see cref="T:System.Collections.Generic.ICollection`1" /> is read-only; otherwise, false.
            /// </returns>
            public bool IsReadOnly => false;

            /// <summary>
            /// Gets or sets the zoom level at the specified index.
            /// </summary>
            /// <param name="index">The index.</param>
            public int this[int index]
            {
                get => List.Values[index];
                set
                {
                    List.RemoveAt(index);
                    Add(value);
                }
            }

            #endregion

            #region Protected Properties

            /// <summary>
            /// Gets or sets the backing list.
            /// </summary>
            protected SortedList<int, int> List { get; set; }

            #endregion

            #region Public Members

            /// <summary>
            /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1" />.
            /// </summary>
            /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1" />.</param>
            public void Add(int item)
            {
                List.Add(item, item);
            }

            /// <summary>
            /// Adds a range of items to the <see cref="ZoomLevelCollection"/>.
            /// </summary>
            /// <param name="collection">The items to add to the collection.</param>
            /// <exception cref="System.ArgumentNullException">Thrown if the <c>collection</c> parameter is null.</exception>
            public void AddRange(IEnumerable<int> collection)
            {
                if (collection == null)
                {
                    throw new ArgumentNullException(nameof(collection));
                }

                foreach (int value in collection)
                {
                    Add(value);
                }
            }

            /// <summary>
            /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1" />.
            /// </summary>
            public void Clear()
            {
                List.Clear();
            }

            /// <summary>
            /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1" /> contains a specific value.
            /// </summary>
            /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1" />.</param>
            /// <returns>true if <paramref name="item" /> is found in the <see cref="T:System.Collections.Generic.ICollection`1" />; otherwise, false.</returns>
            public bool Contains(int item)
            {
                return List.ContainsKey(item);
            }

            /// <summary>
            /// Copies a range of elements this collection into a destination <see cref="Array"/>.
            /// </summary>
            /// <param name="array">The <see cref="Array"/> that receives the data.</param>
            /// <param name="arrayIndex">A 64-bit integer that represents the index in the <see cref="Array"/> at which storing begins.</param>
            public void CopyTo(int[] array, int arrayIndex)
            {
                for (int i = 0; i < this.Count; i++)
                {
                    array[arrayIndex + i] = this.List.Values[i];
                }
            }

            /// <summary>
            /// Finds the index of a zoom level matching or nearest to the specified value.
            /// </summary>
            /// <param name="zoomLevel">The zoom level.</param>
            public int FindNearest(int zoomLevel)
            {
                int nearestValue = this.List.Values[0];
                int nearestDifference = Math.Abs(nearestValue - zoomLevel);
                for (int i = 1; i < Count; i++)
                {
                    int value = List.Values[i];
                    int difference = Math.Abs(value - zoomLevel);
                    if (difference < nearestDifference)
                    {
                        nearestValue = value;
                        nearestDifference = difference;
                    }
                }
                return nearestValue;
            }

            /// <summary>
            /// Returns an enumerator that iterates through the collection.
            /// </summary>
            /// <returns>A <see cref="T:System.Collections.Generic.IEnumerator`1" /> that can be used to iterate through the collection.</returns>
            public IEnumerator<int> GetEnumerator()
            {
                return List.Values.GetEnumerator();
            }

            /// <summary>
            /// Determines the index of a specific item in the <see cref="T:System.Collections.Generic.IList`1" />.
            /// </summary>
            /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.IList`1" />.</param>
            /// <returns>The index of <paramref name="item" /> if found in the list; otherwise, -1.</returns>
            public int IndexOf(int item)
            {
                return List.IndexOfKey(item);
            }

            /// <summary>
            /// Not implemented.
            /// </summary>
            /// <param name="index">The index.</param>
            /// <param name="item">The item.</param>
            /// <exception cref="System.NotImplementedException">Not implemented</exception>
            public void Insert(int index, int item)
            {
                throw new NotImplementedException();
            }

            /// <summary>
            /// Returns the next increased zoom level for the given current zoom.
            /// </summary>
            /// <param name="zoomLevel">The current zoom level.</param>
            /// <returns>The next matching increased zoom level for the given current zoom if applicable, otherwise the nearest zoom.</returns>
            public int NextZoom(int zoomLevel)
            {
                var index = IndexOf(this.FindNearest(zoomLevel));
                if (index < this.Count - 1)
                {
                    index++;
                }

                return this[index];
            }

            /// <summary>
            /// Returns the next decreased zoom level for the given current zoom.
            /// </summary>
            /// <param name="zoomLevel">The current zoom level.</param>
            /// <returns>The next matching decreased zoom level for the given current zoom if applicable, otherwise the nearest zoom.</returns>
            public int PreviousZoom(int zoomLevel)
            {
                var index = IndexOf(FindNearest(zoomLevel));
                if (index > 0)
                {
                    index--;
                }

                return this[index];
            }

            /// <summary>
            /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1" />.
            /// </summary>
            /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1" />.</param>
            /// <returns>true if <paramref name="item" /> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1" />; otherwise, false. This method also returns false if <paramref name="item" /> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1" />.</returns>
            public bool Remove(int item)
            {
                return List.Remove(item);
            }

            /// <summary>
            /// Removes the element at the specified index of the <see cref="ZoomLevelCollection"/>.
            /// </summary>
            /// <param name="index">The zero-based index of the element to remove.</param>
            public void RemoveAt(int index)
            {
                List.RemoveAt(index);
            }

            /// <summary>
            /// Copies the elements of the <see cref="ZoomLevelCollection"/> to a new array.
            /// </summary>
            /// <returns>An array containing copies of the elements of the <see cref="ZoomLevelCollection"/>.</returns>
            public int[] ToArray()
            {
                int[] results;

                results = new int[Count];
                CopyTo(results, 0);

                return results;
            }

            #endregion

            #region IList<int> Members

            /// <summary>
            /// Returns an enumerator that iterates through a collection.
            /// </summary>
            /// <returns>An <see cref="ZoomLevelCollection" /> object that can be used to iterate through the collection.</returns>
            IEnumerator IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }

            #endregion
        }

        #endregion

        #region Enums

        /// <summary>
        /// Determines the sizing mode of an image hosted in an <see cref="UVtools.WPF.Controls.AdvancedImageBox" /> control.
        /// </summary>
        public enum SizeModes : byte
        {
            /// <summary>
            /// The image is disiplayed according to current zoom and scroll properties.
            /// </summary>
            Normal,

            /// <summary>
            /// The image is stretched to fill the client area of the control.
            /// </summary>
            Stretch,

            /// <summary>
            /// The image is stretched to fill as much of the client area of the control as possible, whilst retaining the same aspect ratio for the width and height.
            /// </summary>
            //Fit
        }

        [Flags]
        public enum MouseButtons : byte
        {
            None = 0,
            LeftButton = 1,
            MiddleButton = 2,
            RightButton = 4
        }

        /// <summary>
        /// Describes the zoom action occuring
        /// </summary>
        [Flags]
        public enum ZoomActions : byte
        {
            /// <summary>
            /// No action.
            /// </summary>
            None = 0,

            /// <summary>
            /// The control is increasing the zoom.
            /// </summary>
            ZoomIn = 1,

            /// <summary>
            /// The control is decreasing the zoom.
            /// </summary>
            ZoomOut = 2,

            /// <summary>
            /// The control zoom was reset.
            /// </summary>
            ActualSize = 4
        }

        public enum SelectionModes
        {
            /// <summary>
            ///   No selection.
            /// </summary>
            None,

            /// <summary>
            ///   Rectangle selection.
            /// </summary>
            Rectangle,

            /// <summary>
            ///   Zoom selection.
            /// </summary>
            Zoom
        }

        #endregion

        #region Constants
        public static readonly int MinZoom = 10;
        public static readonly int MaxZoom = 3500;
        #endregion

        public bool CanRender
        {
            get => _canRender;
            set
            {
                if (!RaiseAndSetIfChanged(ref _canRender, value)) return;
                if (_canRender) TriggerRender();
            }
        }

        /// <summary>
        /// Gets or sets the basic cell size
        /// </summary>
        public byte GridCellSize
        {
            get => _gridCellSize;
            set => RaiseAndSetIfChanged(ref _gridCellSize, value);
        }

        /// <summary>
        /// Gets or sets the color used to create the checkerboard style background
        /// </summary>
        public ISolidColorBrush GridColor
        {
            get => _gridColor;
            set => RaiseAndSetIfChanged(ref _gridColor, value);
        }

        /// <summary>
        /// Gets or sets the color used to create the checkerboard style background
        /// </summary>
        public ISolidColorBrush GridColorAlternate
        {
            get => _gridColorAlternate;
            set => RaiseAndSetIfChanged(ref _gridColorAlternate, value);
        }

        /// <summary>
        /// Gets or sets the image to be displayed
        /// </summary>
        public Bitmap Image
        {
            get => _image;
            set
            {
                if (!RaiseAndSetIfChanged(ref _image, value)) return;

                if (Image is null)
                {
                    SelectNone();
                }

                UpdateViewPort();
                TriggerRender();
            }
        }

        /// <summary>
        /// Gets or sets an image to follow the mouse pointer
        /// </summary>
        public Bitmap TrackerImage
        {
            get => _trackerImage;
            set
            {
                if (!RaiseAndSetIfChanged(ref _trackerImage, value)) return;
                RaisePropertyChanged(nameof(HaveTrackerImage));
                TriggerRender();
            }
        }

        public bool HaveTrackerImage => !(_trackerImage is null);

        /// <summary>
        /// Gets or sets if the tracker image will be scaled to the current zoom
        /// </summary>
        public bool TrackerImageAutoZoom
        {
            get => _trackerImageAutoZoom;
            set => RaiseAndSetIfChanged(ref _trackerImageAutoZoom, value);
        }

        public bool IsHorizontalBarVisible
        {
            get
            {
                if (Image is null) return false;
                return ScaledImageWidth > Viewport.Width;
            }
        }

        public bool IsVerticalBarVisible
        {
            get
            {
                if (Image is null) return false;
                return ScaledImageHeight > Viewport.Height;
            }
        }

        public static readonly DirectProperty<AdvancedImageBox, bool> ShowGridProperty =
            AvaloniaProperty.RegisterDirect<AdvancedImageBox, bool>(nameof(ShowGrid),
                c => c.ShowGrid, (c, v) => c.ShowGrid = v);
        /// <summary>
        /// Gets or sets if the checkerboard background should be displayed
        /// </summary>
        public bool ShowGrid
        {
            get => _showGrid;
            set => SetAndRaise(ShowGridProperty, ref _showGrid, value);
        }

        public Point PointerPosition
        {
            get => _pointerPosition;
            private set => RaiseAndSetIfChanged(ref _pointerPosition, value);
        }

        public bool IsPanning
        {
            get => _isPanning;
            protected set
            {
                if (!RaiseAndSetIfChanged(ref _isPanning, value)) return;
                _startScrollPosition = Offset;

                if (value)
                {
                    Cursor = new Cursor(StandardCursorType.SizeAll);
                    //this.OnPanStart(EventArgs.Empty);
                }
                else
                {
                    Cursor = Cursor.Default;
                    //this.OnPanEnd(EventArgs.Empty);
                }
            }
        }

        public bool IsSelecting
        {
            get => _isSelecting;
            protected set => RaiseAndSetIfChanged(ref _isSelecting, value);
        }

        public Point CenterPoint
        {
            get
            {
                var viewport = GetImageViewPort();
                return new Point(viewport.Width / 2, viewport.Height / 2);
            }
        }

        public bool AutoPan
        {
            get => _autoPan;
            set => RaiseAndSetIfChanged(ref _autoPan, value);
        }

        public MouseButtons PanWithMouseButtons
        {
            get => _panWithMouseButtons;
            set => RaiseAndSetIfChanged(ref _panWithMouseButtons, value);
        }

        public bool PanWithArrows
        {
            get => _panWithArrows;
            set => RaiseAndSetIfChanged(ref _panWithArrows, value);
        }

        public MouseButtons SelectWithMouseButtons
        {
            get => _selectWithMouseButtons;
            set => RaiseAndSetIfChanged(ref _selectWithMouseButtons, value);
        }

        public bool InvertMouse
        {
            get => _invertMouse;
            set => RaiseAndSetIfChanged(ref _invertMouse, value);
        }

        public bool AutoCenter
        {
            get => _autoCenter;
            set => RaiseAndSetIfChanged(ref _autoCenter, value);
        }

        public SizeModes SizeMode
        {
            get => _sizeMode;
            set => RaiseAndSetIfChanged(ref _sizeMode, value);
        }

        private bool _allowZoom = true;
        public virtual bool AllowZoom
        {
            get => _allowZoom;
            set => RaiseAndSetIfChanged(ref _allowZoom, value);
        }

        ZoomLevelCollection _zoomLevels = ZoomLevelCollection.Default;
        /// <summary>
        ///   Gets or sets the zoom levels.
        /// </summary>
        /// <value>The zoom levels.</value>
        public virtual ZoomLevelCollection ZoomLevels
        {
            get => _zoomLevels;
            set => RaiseAndSetIfChanged(ref _zoomLevels, value);
        }

        private int _oldZoom = 100;
        private int _zoom = 100;

        /// <summary>
        ///   Gets or sets the zoom.
        /// </summary>
        /// <value>The zoom.</value>
        public virtual int OldZoom
        {
            get => _oldZoom;
            set => RaiseAndSetIfChanged(ref _oldZoom, value);
        }

        /// <summary>
        ///   Gets or sets the zoom.
        /// </summary>
        /// <value>The zoom.</value>
        public virtual int Zoom
        {
            get => _zoom;
            set
            {
                var newZoom = value.Clamp(MinZoom, MaxZoom);

                if (_zoom == newZoom) return;
                var previousZoom = _zoom;
                _zoom = newZoom;
                UpdateViewPort();
                TriggerRender();

                OldZoom = previousZoom;
                RaisePropertyChanged(nameof(Zoom));
                //this.OnZoomChanged(EventArgs.Empty);
                //this.OnZoomed(new ImageBoxZoomEventArgs(actions, source, previousZoom, this.Zoom));
                //SetZoom(value, value > Zoom ? ImageZoomActions.ZoomIn : ImageZoomActions.ZoomOut);
            }
        }

        public virtual bool IsActualSize => Zoom == 100;

        private ISolidColorBrush _pixelGridColor = Brushes.DimGray;
        /// <summary>
        /// Gets or sets the color of the pixel grid.
        /// </summary>
        /// <value>The color of the pixel grid.</value>
        public virtual ISolidColorBrush PixelGridColor
        {
            get => _pixelGridColor;
            set => RaiseAndSetIfChanged(ref _pixelGridColor, value);
        }

        private int _pixelGridThreshold = 5;
        /// <summary>
        /// Gets or sets the minimum size of zoomed pixel's before the pixel grid will be drawn
        /// </summary>
        /// <value>The pixel grid threshold.</value>

        public virtual int PixelGridThreshold
        {
            get => _pixelGridThreshold;
            set => RaiseAndSetIfChanged(ref _pixelGridThreshold, value);
        }

        public SelectionModes SelectionMode
        {
            get => _selectionMode;
            set => RaiseAndSetIfChanged(ref _selectionMode, value);
        }

        public ISolidColorBrush SelectionColor
        {
            get => _selectionColor;
            set => RaiseAndSetIfChanged(ref _selectionColor, value);
        }

        public Rect SelectionRegion
        {
            get => _selectionRegion;
            set
            {
                if (!RaiseAndSetIfChanged(ref _selectionRegion, value)) return;
                TriggerRender();
                RaisePropertyChanged(nameof(HaveSelection));
            }
        }

        public bool HaveSelection => !SelectionRegion.IsEmpty;


        //Our render target we compile everything to and present to the user
        private Point _startMousePosition;
        private Vector _startScrollPosition;
        private bool _isPanning;
        private bool _isSelecting;
        private Bitmap _image;
        private Bitmap _trackerImage;
        private bool _trackerImageAutoZoom = true;
        private byte _gridCellSize;
        private ISolidColorBrush _gridColor = Brushes.Gainsboro;
        private ISolidColorBrush _gridColorAlternate = Brushes.White;
        private bool _showGrid = true;
        private bool _autoPan = true;
        private MouseButtons _panWithMouseButtons = MouseButtons.LeftButton | MouseButtons.MiddleButton | MouseButtons.RightButton;
        private bool _panWithArrows = true;
        private MouseButtons _selectWithMouseButtons = MouseButtons.LeftButton | MouseButtons.RightButton;
        private bool _invertMouse = false;
        private bool _autoCenter = true;
        private SizeModes _sizeMode = SizeModes.Normal;
        private ISolidColorBrush _selectionColor = new SolidColorBrush(new Color(127, 0, 128, 255));
        private Rect _selectionRegion = Rect.Empty;
        private SelectionModes _selectionMode = SelectionModes.None;
        private bool _canRender = true;
        private Point _pointerPosition;
        


        public AdvancedImageBox()
        {
            InitializeComponent();

            FocusableProperty.OverrideDefaultValue(typeof(AdvancedImageBox), true);
            AffectsRender<AdvancedImageBox>(ShowGridProperty);

            HorizontalScrollBar = this.FindControl<ScrollBar>("HorizontalScrollBar");
            VerticalScrollBar = this.FindControl<ScrollBar>("VerticalScrollBar");
            ViewPortControl = this.FindControl<ContentControl>("ViewPort");

            HorizontalScrollBar.Scroll += ScrollBarOnScroll;
            VerticalScrollBar.Scroll += ScrollBarOnScroll;
            ViewPortControl.PointerWheelChanged += FillContainerOnPointerWheelChanged;
        }

        private void ScrollBarOnScroll(object? sender, ScrollEventArgs e)
        {
            TriggerRender();
        }

        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);
        }

        /*protected override void OnScrollChanged(ScrollChangedEventArgs e)
        {
            Debug.WriteLine($"ViewportDelta: {e.ViewportDelta} | OffsetDelta: {e.OffsetDelta} | ExtentDelta: {e.ExtentDelta}");
            if (!e.ViewportDelta.IsDefault)
            {
                UpdateViewPort();
            }

            TriggerRender();

            base.OnScrollChanged(e);
        }*/

        private void FillContainerOnPointerWheelChanged(object? sender, PointerWheelEventArgs e)
        {
            Debug.WriteLine("mouse whell");
            e.Handled = true;
            if (Image is null) return;
            if (AllowZoom && SizeMode == SizeModes.Normal)
            {
                // The MouseWheel event can contain multiple "spins" of the wheel so we need to adjust accordingly
                //double spins = Math.Abs(e.Delta.Y);
                //Debug.WriteLine(e.GetPosition(this));
                // TODO: Really should update the source method to handle multiple increments rather than calling it multiple times
                /*for (int i = 0; i < spins; i++)
                {*/
                ProcessMouseZoom(e.Delta.Y > 0, e.GetPosition(ViewPortControl));
                //}
            }
        }

        public void TriggerRender(bool renderOnlyCursorTracker = false)
        {
            if (!_canRender) return;
            if (renderOnlyCursorTracker && _trackerImage is null) return;
                
            InvalidateVisual();
        }

        private void ProcessMouseZoom(bool isZoomIn, Point cursorPosition)
         => PerformZoom(isZoomIn ? ZoomActions.ZoomIn : ZoomActions.ZoomOut, true, cursorPosition);

        /// <summary>
        /// Returns an appropriate zoom level based on the specified action, relative to the current zoom level.
        /// </summary>
        /// <param name="action">The action to determine the zoom level.</param>
        /// <exception cref="System.ArgumentOutOfRangeException">Thrown if an unsupported action is specified.</exception>
        private int GetZoomLevel(ZoomActions action)
        {
            int result;

            switch (action)
            {
                case ZoomActions.None:
                    result = Zoom;
                    break;
                case ZoomActions.ZoomIn:
                    result = ZoomLevels.NextZoom(Zoom);
                    break;
                case ZoomActions.ZoomOut:
                    result = ZoomLevels.PreviousZoom(Zoom);
                    break;
                case ZoomActions.ActualSize:
                    result = 100;
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(action));
            }

            return result;
        }

        /// <summary>
        /// Resets the <see cref="SizeModes"/> property whilsts retaining the original <see cref="Zoom"/>.
        /// </summary>
        protected void RestoreSizeMode()
        {
            if (SizeMode != SizeModes.Normal)
            {
                var previousZoom = Zoom;
                SizeMode = SizeModes.Normal;
                Zoom = previousZoom; // Stop the zoom getting reset to 100% before calculating the new zoom
            }
        }

        private void PerformZoom(ZoomActions action, bool preservePosition)
            => PerformZoom(action, preservePosition, CenterPoint);

        private void PerformZoom(ZoomActions action, bool preservePosition, Point relativePoint)
        {
            Point currentPixel = PointToImage(relativePoint);
            int currentZoom = Zoom;
            int newZoom = GetZoomLevel(action);

            if (preservePosition && Zoom != currentZoom)
                CanRender = false;

            RestoreSizeMode();
            Zoom = newZoom;

            if (preservePosition && Zoom != currentZoom)
            {
                ScrollTo(currentPixel, relativePoint);
            }
        }

        /// <summary>
        ///   Determines whether the specified point is located within the image view port
        /// </summary>
        /// <param name="point">The point.</param>
        /// <returns>
        ///   <c>true</c> if the specified point is located within the image view port; otherwise, <c>false</c>.
        /// </returns>
        public virtual bool IsPointInImage(Point point)
            => GetImageViewPort().Contains(point);

        /// <summary>
        ///   Determines whether the specified point is located within the image view port
        /// </summary>
        /// <param name="x">The X co-ordinate of the point to check.</param>
        /// <param name="y">The Y co-ordinate of the point to check.</param>
        /// <returns>
        ///   <c>true</c> if the specified point is located within the image view port; otherwise, <c>false</c>.
        /// </returns>
        public bool IsPointInImage(int x, int y)
            => IsPointInImage(new Point(x, y));

        /// <summary>
        ///   Determines whether the specified point is located within the image view port
        /// </summary>
        /// <param name="x">The X co-ordinate of the point to check.</param>
        /// <param name="y">The Y co-ordinate of the point to check.</param>
        /// <returns>
        ///   <c>true</c> if the specified point is located within the image view port; otherwise, <c>false</c>.
        /// </returns>
        public bool IsPointInImage(double x, double y)
            => IsPointInImage(new Point(x, y));

        /// <summary>
        ///   Converts the given client size point to represent a coordinate on the source image.
        /// </summary>
        /// <param name="point">The source point.</param>
        /// <returns><c>Point.Empty</c> if the point could not be matched to the source image, otherwise the new translated point</returns>
        public Point PointToImage(Point point)
            => PointToImage(point, false);

        /// <summary>
        ///   Converts the given client size point to represent a coordinate on the source image.
        /// </summary>
        /// <param name="x">The X co-ordinate of the point to convert.</param>
        /// <param name="y">The Y co-ordinate of the point to convert.</param>
        /// <param name="fitToBounds">
        ///   if set to <c>true</c> and the point is outside the bounds of the source image, it will be mapped to the nearest edge.
        /// </param>
        /// <returns><c>Point.Empty</c> if the point could not be matched to the source image, otherwise the new translated point</returns>
        public Point PointToImage(double x, double y, bool fitToBounds = false)
            => PointToImage(x, y, fitToBounds);

        /// <summary>
        ///   Converts the given client size point to represent a coordinate on the source image.
        /// </summary>
        /// <param name="x">The X co-ordinate of the point to convert.</param>
        /// <param name="y">The Y co-ordinate of the point to convert.</param>
        /// <param name="fitToBounds">
        ///   if set to <c>true</c> and the point is outside the bounds of the source image, it will be mapped to the nearest edge.
        /// </param>
        /// <returns><c>Point.Empty</c> if the point could not be matched to the source image, otherwise the new translated point</returns>
        public Point PointToImage(int x, int y, bool fitToBounds = false)
        {
            return PointToImage(x, y, fitToBounds);
        }

        /// <summary>
        ///   Converts the given client size point to represent a coordinate on the source image.
        /// </summary>
        /// <param name="point">The source point.</param>
        /// <param name="fitToBounds">
        ///   if set to <c>true</c> and the point is outside the bounds of the source image, it will be mapped to the nearest edge.
        /// </param>
        /// <returns><c>Point.Empty</c> if the point could not be matched to the source image, otherwise the new translated point</returns>
        public virtual Point PointToImage(Point point, bool fitToBounds)
        {
            double x;
            double y;

            var viewport = GetImageViewPort();

            if (!fitToBounds || viewport.Contains(point))
            {
                x = ((point.X + Offset.X - viewport.X) / ZoomFactor);
                y = ((point.Y + Offset.Y - viewport.Y) / ZoomFactor);

                if (fitToBounds)
                {
                    x = x.Clamp(0, Image.Size.Width);
                    y = y.Clamp(0, Image.Size.Height);
                }
            }
            else
            {
                x = 0; // Return Point.Empty if we couldn't match
                y = 0;
            }

            return new Point(x, y);
        }

        /// <summary>
        ///   Scrolls the control to the given point in the image, offset at the specified display point
        /// </summary>
        /// <param name="x">The X co-ordinate of the point to scroll to.</param>
        /// <param name="y">The Y co-ordinate of the point to scroll to.</param>
        /// <param name="relativeX">The X co-ordinate relative to the <c>x</c> parameter.</param>
        /// <param name="relativeY">The Y co-ordinate relative to the <c>y</c> parameter.</param>
        public void ScrollTo(double x, double y, double relativeX, double relativeY)
            => ScrollTo(new Point(x, y), new Point(relativeX, relativeY));

        /// <summary>
        ///   Scrolls the control to the given point in the image, offset at the specified display point
        /// </summary>
        /// <param name="x">The X co-ordinate of the point to scroll to.</param>
        /// <param name="y">The Y co-ordinate of the point to scroll to.</param>
        /// <param name="relativeX">The X co-ordinate relative to the <c>x</c> parameter.</param>
        /// <param name="relativeY">The Y co-ordinate relative to the <c>y</c> parameter.</param>
        public void ScrollTo(int x, int y, int relativeX, int relativeY)
            => ScrollTo(new Point(x, y), new Point(relativeX, relativeY));

        /// <summary>
        ///   Scrolls the control to the given point in the image, offset at the specified display point
        /// </summary>
        /// <param name="imageLocation">The point of the image to attempt to scroll to.</param>
        /// <param name="relativeDisplayPoint">The relative display point to offset scrolling by.</param>
        public virtual void ScrollTo(Point imageLocation, Point relativeDisplayPoint)
        {
            //CanRender = false;
            var x = imageLocation.X * ZoomFactor - relativeDisplayPoint.X;
            var y = imageLocation.Y * ZoomFactor - relativeDisplayPoint.Y;


            _canRender = true;
            Offset = new Vector(x, y);

            /*Debug.WriteLine(
                $"X/Y: {x},{y} | \n" +
                $"Offset: {Offset} | \n" +
                $"ZoomFactor: {ZoomFactor} | \n" +
                $"Image Location: {imageLocation}\n" +
                $"MAX: {HorizontalScrollBar.Maximum},{VerticalScrollBar.Maximum} \n" +
                $"ViewPort: {Viewport.Width},{Viewport.Height} \n" +
                $"Container: {HorizontalScrollBar.ViewportSize},{VerticalScrollBar.ViewportSize} \n" +
                $"Relative: {relativeDisplayPoint}");*/
        }

        /// <summary>
        ///   Zooms into the image
        /// </summary>
        public virtual void ZoomIn()
            => ZoomIn(true);

        /// <summary>
        ///   Zooms into the image
        /// </summary>
        /// <param name="preservePosition"><c>true</c> if the current scrolling position should be preserved relative to the new zoom level, <c>false</c> to reset.</param>
        public virtual void ZoomIn(bool preservePosition)
        {
            PerformZoom(ZoomActions.ZoomIn, preservePosition);
        }

        /// <summary>
        ///   Zooms out of the image
        /// </summary>
        public virtual void ZoomOut()
         => ZoomOut(true);

        /// <summary>
        ///   Zooms out of the image
        /// </summary>
        /// <param name="preservePosition"><c>true</c> if the current scrolling position should be preserved relative to the new zoom level, <c>false</c> to reset.</param>
        public virtual void ZoomOut(bool preservePosition)
        {
            PerformZoom(ZoomActions.ZoomOut, preservePosition);
        }

        /// <summary>
        ///   Zooms to the maximum size for displaying the entire image within the bounds of the control.
        /// </summary>
        public virtual void ZoomToFit()
        {
            if (Image is null) return;

            double zoom;
            double aspectRatio;

            if (Image.Size.Width > Image.Size.Height)
            {
                aspectRatio = Viewport.Width / Image.Size.Width;
                zoom = aspectRatio * 100.0;

                if (Viewport.Height < Image.Size.Height * zoom / 100.0)
                {
                    aspectRatio = Viewport.Height / Image.Size.Height;
                    zoom = aspectRatio * 100.0;
                }
            }
            else
            {
                aspectRatio = Viewport.Height / Image.Size.Height;
                zoom = aspectRatio * 100.0;

                if (Viewport.Width < Image.Size.Width * zoom / 100.0)
                {
                    aspectRatio = Viewport.Width / Image.Size.Width;
                    zoom = aspectRatio * 100.0;
                }
            }

            Zoom = (int)Math.Round(Math.Floor(zoom));
        }

        /// <summary>
        ///   Adjusts the view port to fit the given region
        /// </summary>
        /// <param name="x">The X co-ordinate of the selection region.</param>
        /// <param name="y">The Y co-ordinate of the selection region.</param>
        /// <param name="width">The width of the selection region.</param>
        /// <param name="height">The height of the selection region.</param>
        /// <param name="margin">Give a margin to rectangle by a value to zoom-out that pixel value</param>
        public void ZoomToRegion(double x, double y, double width, double height, double margin = 0)
        {
            ZoomToRegion(new Rect(x, y, width, height), margin);
        }

        /// <summary>
        ///   Adjusts the view port to fit the given region
        /// </summary>
        /// <param name="x">The X co-ordinate of the selection region.</param>
        /// <param name="y">The Y co-ordinate of the selection region.</param>
        /// <param name="width">The width of the selection region.</param>
        /// <param name="height">The height of the selection region.</param>
        /// <param name="margin">Give a margin to rectangle by a value to zoom-out that pixel value</param>
        public void ZoomToRegion(int x, int y, int width, int height, double margin = 0)
        {
            ZoomToRegion(new Rect(x, y, width, height), margin);
        }

        /// <summary>
        ///   Adjusts the view port to fit the given region
        /// </summary>
        /// <param name="rectangle">The rectangle to fit the view port to.</param>
        /// <param name="margin">Give a margin to rectangle by a value to zoom-out that pixel value</param>
        public virtual void ZoomToRegion(Rectangle rectangle, double margin = 0) => ZoomToRegion(rectangle.ToAvalonia(), margin);

        /// <summary>
        ///   Adjusts the view port to fit the given region
        /// </summary>
        /// <param name="rectangle">The rectangle to fit the view port to.</param>
        /// <param name="margin">Give a margin to rectangle by a value to zoom-out that pixel value</param>
        public virtual void ZoomToRegion(Rect rectangle, double margin = 0)
        {
            if (margin > 0) rectangle = rectangle.Inflate(margin);
            var ratioX = Viewport.Width / rectangle.Width;
            var ratioY = Viewport.Height / rectangle.Height;
            var zoomFactor = Math.Min(ratioX, ratioY);
            var cx = rectangle.X + rectangle.Width / 2;
            var cy = rectangle.Y + rectangle.Height / 2;

            CanRender = false;
            Zoom = (int)(zoomFactor * 100); // This function sets the zoom so viewport will change
            CenterAt(new Point(cx, cy)); // If i call this here, it will move to the wrong position due wrong viewport
        }

        /// <summary>
        /// Zooms to current selection region
        /// </summary>
        public void ZoomToSelectionRegion(double margin = 0)
        {
            if (!HaveSelection) return;
            ZoomToRegion(SelectionRegion, margin);
        }

        /// <summary>
        ///   Centers the given point in the image in the center of the control
        /// </summary>
        /// <param name="imageLocation">The point of the image to attempt to center.</param>
        public virtual void CenterAt(System.Drawing.Point imageLocation)
            => ScrollTo(new Point(imageLocation.X, imageLocation.Y), new Point(Viewport.Width / 2, Viewport.Height / 2));

        /// <summary>
        ///   Centers the given point in the image in the center of the control
        /// </summary>
        /// <param name="imageLocation">The point of the image to attempt to center.</param>
        public virtual void CenterAt(Point imageLocation)
         => ScrollTo(imageLocation, new Point(Viewport.Width / 2, Viewport.Height / 2));

        /// <summary>
        ///   Centers the given point in the image in the center of the control
        /// </summary>
        /// <param name="x">The X co-ordinate of the point to center.</param>
        /// <param name="y">The Y co-ordinate of the point to center.</param>
        public void CenterAt(int x, int y)
         => CenterAt(new Point(x, y));

        /// <summary>
        ///   Centers the given point in the image in the center of the control
        /// </summary>
        /// <param name="x">The X co-ordinate of the point to center.</param>
        /// <param name="y">The Y co-ordinate of the point to center.</param>
        public void CenterAt(double x, double y)
         => CenterAt(new Point(x, y));

        /// <summary>
        /// Resets the viewport to show the center of the image.
        /// </summary>
        public virtual void CenterToImage()
        {
            Offset = new Vector(HorizontalScrollBar.Maximum / 2, VerticalScrollBar.Maximum / 2);
        }

        private bool UpdateViewPort()
        {
            if (Image is null)
            {
                HorizontalScrollBar.Maximum = 0;
                VerticalScrollBar.Maximum = 0;
                return true;
            }

            var scaledImageWidth = ScaledImageWidth;
            var scaledImageHeight = ScaledImageHeight;
            var width = scaledImageWidth - HorizontalScrollBar.ViewportSize;
            var height = scaledImageHeight - VerticalScrollBar.ViewportSize;
            //var width = scaledImageWidth <= Viewport.Width ? Viewport.Width : scaledImageWidth;
            //var height = scaledImageHeight <= Viewport.Height ? Viewport.Height : scaledImageHeight;

            bool changed = false;
            if (HorizontalScrollBar.Maximum != width)
            {
                HorizontalScrollBar.Maximum = width;
                changed = true;
            }

            if (VerticalScrollBar.Maximum != scaledImageHeight)
            {
                VerticalScrollBar.Maximum = height;
                changed = true;
            }

            /*if (changed)
            {
                var newContainer = new ContentControl
                {
                    Width = width,
                    Height = height
                };
                FillContainer.Content = SizedContainer = newContainer;
                Debug.WriteLine($"Updated ViewPort: {DateTime.Now.Ticks}");
                //TriggerRender();
            }*/

            return changed;
        }

        /// <summary>
        /// Resets the zoom to 100%.
        /// </summary>
        /// <param name="source">The source that initiated the action.</param>
        private void PerformActualSize()
        {
            SizeMode = SizeModes.Normal;
            //SetZoom(100, ImageZoomActions.ActualSize | (Zoom < 100 ? ImageZoomActions.ZoomIn : ImageZoomActions.ZoomOut));
            Zoom = 100;
        }

        #region Overrides


        #endregion

        #region Methods

        #region Selection

        /// <summary>
        ///   Returns the source <see cref="T:System.Drawing.Point" /> scaled according to the current zoom level
        /// </summary>
        /// <param name="x">The X co-ordinate of the point to scale.</param>
        /// <param name="y">The Y co-ordinate of the point to scale.</param>
        /// <returns>A <see cref="Point"/> which has been scaled to match the current zoom level</returns>
        public Point GetScaledPoint(int x, int y)
        {
            return GetScaledPoint(new Point(x, y));
        }

        /// <summary>
        ///   Returns the source <see cref="T:System.Drawing.Point" /> scaled according to the current zoom level
        /// </summary>
        /// <param name="x">The X co-ordinate of the point to scale.</param>
        /// <param name="y">The Y co-ordinate of the point to scale.</param>
        /// <returns>A <see cref="Point"/> which has been scaled to match the current zoom level</returns>
        public PointF GetScaledPoint(float x, float y)
        {
            return GetScaledPoint(new PointF(x, y));
        }

        /// <summary>
        ///   Returns the source <see cref="T:System.Drawing.Point" /> scaled according to the current zoom level
        /// </summary>
        /// <param name="source">The source <see cref="Point"/> to scale.</param>
        /// <returns>A <see cref="Point"/> which has been scaled to match the current zoom level</returns>
        public virtual Point GetScaledPoint(Point source)
        {
            return new Point(source.X * ZoomFactor, source.Y * ZoomFactor);
        }

        /// <summary>
        ///   Returns the source <see cref="T:System.Drawing.PointF" /> scaled according to the current zoom level
        /// </summary>
        /// <param name="source">The source <see cref="PointF"/> to scale.</param>
        /// <returns>A <see cref="PointF"/> which has been scaled to match the current zoom level</returns>
        public virtual PointF GetScaledPoint(PointF source)
        {
            return new PointF((float)(source.X * this.ZoomFactor), (float)(source.Y * this.ZoomFactor));
        }

        /// <summary>
        ///   Returns the source rectangle scaled according to the current zoom level
        /// </summary>
        /// <param name="x">The X co-ordinate of the source rectangle.</param>
        /// <param name="y">The Y co-ordinate of the source rectangle.</param>
        /// <param name="width">The width of the rectangle.</param>
        /// <param name="height">The height of the rectangle.</param>
        /// <returns>A <see cref="Rectangle"/> which has been scaled to match the current zoom level</returns>
        public Rect GetScaledRectangle(int x, int y, int width, int height)
        {
            return GetScaledRectangle(new Rect(x, y, width, height));
        }

        /// <summary>
        ///   Returns the source rectangle scaled according to the current zoom level
        /// </summary>
        /// <param name="x">The X co-ordinate of the source rectangle.</param>
        /// <param name="y">The Y co-ordinate of the source rectangle.</param>
        /// <param name="width">The width of the rectangle.</param>
        /// <param name="height">The height of the rectangle.</param>
        /// <returns>A <see cref="RectangleF"/> which has been scaled to match the current zoom level</returns>
        public RectangleF GetScaledRectangle(float x, float y, float width, float height)
        {
            return GetScaledRectangle(new RectangleF(x, y, width, height));
        }

        /// <summary>
        ///   Returns the source rectangle scaled according to the current zoom level
        /// </summary>
        /// <param name="location">The location of the source rectangle.</param>
        /// <param name="size">The size of the source rectangle.</param>
        /// <returns>A <see cref="Rectangle"/> which has been scaled to match the current zoom level</returns>
        public Rect GetScaledRectangle(Point location, Size size)
        {
            return GetScaledRectangle(new Rect(location, size));
        }

        /// <summary>
        ///   Returns the source rectangle scaled according to the current zoom level
        /// </summary>
        /// <param name="location">The location of the source rectangle.</param>
        /// <param name="size">The size of the source rectangle.</param>
        /// <returns>A <see cref="Rectangle"/> which has been scaled to match the current zoom level</returns>
        public RectangleF GetScaledRectangle(PointF location, SizeF size)
        {
            return GetScaledRectangle(new RectangleF(location, size));
        }

        /// <summary>
        ///   Returns the source <see cref="T:System.Drawing.Rectangle" /> scaled according to the current zoom level
        /// </summary>
        /// <param name="source">The source <see cref="Rectangle"/> to scale.</param>
        /// <returns>A <see cref="Rectangle"/> which has been scaled to match the current zoom level</returns>
        public virtual Rect GetScaledRectangle(Rect source)
        {
            return new Rect(source.Left * ZoomFactor, source.Top * ZoomFactor, source.Width * ZoomFactor, source.Height * ZoomFactor);
        }

        /// <summary>
        ///   Returns the source <see cref="T:System.Drawing.RectangleF" /> scaled according to the current zoom level
        /// </summary>
        /// <param name="source">The source <see cref="RectangleF"/> to scale.</param>
        /// <returns>A <see cref="RectangleF"/> which has been scaled to match the current zoom level</returns>
        public virtual RectangleF GetScaledRectangle(RectangleF source)
        {
            return new RectangleF((float)(source.Left * ZoomFactor), (float)(source.Top * ZoomFactor), (float)(source.Width * ZoomFactor), (float)(source.Height * ZoomFactor));
        }

        /// <summary>
        ///   Returns the source size scaled according to the current zoom level
        /// </summary>
        /// <param name="width">The width of the size to scale.</param>
        /// <param name="height">The height of the size to scale.</param>
        /// <returns>A <see cref="SizeF"/> which has been resized to match the current zoom level</returns>
        public SizeF GetScaledSize(float width, float height)
        {
            return this.GetScaledSize(new SizeF(width, height));
        }

        /// <summary>
        ///   Returns the source size scaled according to the current zoom level
        /// </summary>
        /// <param name="width">The width of the size to scale.</param>
        /// <param name="height">The height of the size to scale.</param>
        /// <returns>A <see cref="Size"/> which has been resized to match the current zoom level</returns>
        public Size GetScaledSize(int width, int height)
        {
            return this.GetScaledSize(new Size(width, height));
        }

        /// <summary>
        ///   Returns the source <see cref="T:System.Drawing.SizeF" /> scaled according to the current zoom level
        /// </summary>
        /// <param name="source">The source <see cref="SizeF"/> to scale.</param>
        /// <returns>A <see cref="SizeF"/> which has been resized to match the current zoom level</returns>
        public virtual SizeF GetScaledSize(SizeF source)
        {
            return new SizeF((float)(source.Width * this.ZoomFactor), (float)(source.Height * this.ZoomFactor));
        }

        /// <summary>
        ///   Returns the source <see cref="T:System.Drawing.Size" /> scaled according to the current zoom level
        /// </summary>
        /// <param name="source">The source <see cref="Size"/> to scale.</param>
        /// <returns>A <see cref="Size"/> which has been resized to match the current zoom level</returns>
        public virtual Size GetScaledSize(Size source)
        {
            return new Size(source.Width * ZoomFactor, source.Height * ZoomFactor);
        }

        /// <summary>
        ///   Creates a selection region which encompasses the entire image
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if no image is currently set</exception>
        public virtual void SelectAll()
        {
            if (Image is null) return;
            SelectionRegion = new Rect(0, 0, Image.Size.Width, Image.Size.Height);
        }

        /// <summary>
        ///   Clears any existing selection region
        /// </summary>
        public virtual void SelectNone()
        {
            SelectionRegion = Rect.Empty;
        }

        #endregion

        public void LoadImage(string path)
        {
            Image = new Bitmap(path);
        }

        public override void Render(DrawingContext context)
        {
            Debug.WriteLine($"Render: {DateTime.Now.Ticks}");
            base.Render(context);

            // Draw Grid
            if (ShowGrid)
            {
                // draw the background
                var currentColor = GridColor;
                for (int y = 0; y < Viewport.Height; y += GridCellSize)
                {
                    var firstRowColor = currentColor;
                    for (int x = 0; x < Viewport.Width; x += GridCellSize)
                    {
                        context.FillRectangle(currentColor, new Rect(x, y, GridCellSize, GridCellSize));
                        currentColor = ReferenceEquals(currentColor, GridColor) ? GridColorAlternate : GridColor;
                    }

                    if (firstRowColor == currentColor)
                        currentColor = ReferenceEquals(currentColor, GridColor) ? GridColorAlternate : GridColor;
                }

            }
            /*else
            {
                context.FillRectangle(Background, new Rect(0, 0, Viewport.Width, Viewport.Height));
            }*/

            if (Image is null) return;
            // Draw iamge
            context.DrawImage(_image,
                GetSourceImageRegion(),
                GetImageViewPort()
            );

            if (HaveTrackerImage && _pointerPosition.X >= 0 && _pointerPosition.Y >= 0)
            {
                var destSize = TrackerImageAutoZoom 
                    ? new Size(_trackerImage.Size.Width * ZoomFactor, _trackerImage.Size.Height * ZoomFactor)
                    : _image.Size;

                var destPos = new Point(
                    _pointerPosition.X - destSize.Width / 2,
                    _pointerPosition.Y - destSize.Height / 2
                    );
                context.DrawImage(_trackerImage,
                    new Rect(destPos, destSize)
                );
            }

            //SkiaContext.SkCanvas.dr
            // Draw pixel grid
            var pixelSize = ZoomFactor;
            if (pixelSize > PixelGridThreshold)
            {
                var viewport = GetImageViewPort();
                var offsetX = Offset.X % pixelSize;
                var offsetY = Offset.Y % pixelSize;

                Pen pen = new Pen(PixelGridColor);
                for (double x = viewport.X + pixelSize - offsetX; x < viewport.Right; x += pixelSize)
                {
                    context.DrawLine(pen, new Avalonia.Point(x, viewport.X), new Avalonia.Point(x, viewport.Bottom));
                }

                for (double y = viewport.Y + pixelSize - offsetY; y < viewport.Bottom; y += pixelSize)
                {
                    context.DrawLine(pen, new Avalonia.Point(viewport.Y, y), new Avalonia.Point(viewport.Right, y));
                }

                context.DrawRectangle(pen, viewport);
            }

            if (!SelectionRegion.IsEmpty)
            {
                var rect = GetOffsetRectangle(SelectionRegion);
                context.FillRectangle(SelectionColor, rect);
                Color solidColor = Color.FromArgb(255, SelectionColor.Color.R, SelectionColor.Color.G, SelectionColor.Color.B);
                context.DrawRectangle(new Pen(solidColor.ToUint32()), rect);
            }
        }

        /// <summary>
        ///   Returns the source <see cref="T:System.Drawing.Point" /> repositioned to include the current image offset and scaled by the current zoom level
        /// </summary>
        /// <param name="source">The source <see cref="Point"/> to offset.</param>
        /// <returns>A <see cref="Point"/> which has been repositioned to match the current zoom level and image offset</returns>
        public virtual Point GetOffsetPoint(System.Drawing.Point source)
        {
            var offset = GetOffsetPoint(new Point (source.X, source.Y));

            return new Point((int)offset.X, (int)offset.Y);
        }

        /// <summary>
        ///   Returns the source co-ordinates repositioned to include the current image offset and scaled by the current zoom level
        /// </summary>
        /// <param name="x">The source X co-ordinate.</param>
        /// <param name="y">The source Y co-ordinate.</param>
        /// <returns>A <see cref="Point"/> which has been repositioned to match the current zoom level and image offset</returns>
        public Point GetOffsetPoint(int x, int y)
        {
            return GetOffsetPoint(new System.Drawing.Point(x, y));
        }

        /// <summary>
        ///   Returns the source co-ordinates repositioned to include the current image offset and scaled by the current zoom level
        /// </summary>
        /// <param name="x">The source X co-ordinate.</param>
        /// <param name="y">The source Y co-ordinate.</param>
        /// <returns>A <see cref="Point"/> which has been repositioned to match the current zoom level and image offset</returns>
        public Point GetOffsetPoint(double x, double y)
        {
            return GetOffsetPoint(new Point(x, y));
        }

        /// <summary>
        ///   Returns the source <see cref="T:System.Drawing.PointF" /> repositioned to include the current image offset and scaled by the current zoom level
        /// </summary>
        /// <param name="source">The source <see cref="PointF"/> to offset.</param>
        /// <returns>A <see cref="PointF"/> which has been repositioned to match the current zoom level and image offset</returns>
        public virtual Point GetOffsetPoint(Point source)
        {
            Rect viewport = GetImageViewPort();
            var scaled = GetScaledPoint(source);
            var offsetX = viewport.Left + Offset.X;
            var offsetY = viewport.Top + Offset.Y;

            return new Point(scaled.X + offsetX, scaled.Y + offsetY);
        }

        /// <summary>
        ///   Returns the source <see cref="T:System.Drawing.RectangleF" /> scaled according to the current zoom level and repositioned to include the current image offset
        /// </summary>
        /// <param name="source">The source <see cref="RectangleF"/> to offset.</param>
        /// <returns>A <see cref="RectangleF"/> which has been resized and repositioned to match the current zoom level and image offset</returns>
        public virtual Rect GetOffsetRectangle(Rect source)
        {
            var viewport = GetImageViewPort();
            var scaled = GetScaledRectangle(source);
            var offsetX = viewport.Left - Offset.X;
            var offsetY = viewport.Top - Offset.Y;

            return new Rect(new Point(scaled.Left + offsetX, scaled.Top + offsetY), scaled.Size);
        }

        /// <summary>
        ///   Returns the source rectangle scaled according to the current zoom level and repositioned to include the current image offset
        /// </summary>
        /// <param name="x">The X co-ordinate of the source rectangle.</param>
        /// <param name="y">The Y co-ordinate of the source rectangle.</param>
        /// <param name="width">The width of the rectangle.</param>
        /// <param name="height">The height of the rectangle.</param>
        /// <returns>A <see cref="Rectangle"/> which has been resized and repositioned to match the current zoom level and image offset</returns>
        public Rectangle GetOffsetRectangle(int x, int y, int width, int height)
        {
            return this.GetOffsetRectangle(new Rectangle(x, y, width, height));
        }

        /// <summary>
        ///   Returns the source rectangle scaled according to the current zoom level and repositioned to include the current image offset
        /// </summary>
        /// <param name="x">The X co-ordinate of the source rectangle.</param>
        /// <param name="y">The Y co-ordinate of the source rectangle.</param>
        /// <param name="width">The width of the rectangle.</param>
        /// <param name="height">The height of the rectangle.</param>
        /// <returns>A <see cref="RectangleF"/> which has been resized and repositioned to match the current zoom level and image offset</returns>
        public Rect GetOffsetRectangle(double x, double y, double width, double height)
        {
            return GetOffsetRectangle(new Rect(x, y, width, height));
        }

        /// <summary>
        ///   Returns the source <see cref="T:System.Drawing.Rectangle" /> scaled according to the current zoom level and repositioned to include the current image offset
        /// </summary>
        /// <param name="source">The source <see cref="Rectangle"/> to offset.</param>
        /// <returns>A <see cref="Rectangle"/> which has been resized and repositioned to match the current zoom level and image offset</returns>
        public virtual Rectangle GetOffsetRectangle(Rectangle source)
        {
            var viewport = GetImageViewPort();
            var scaled = GetScaledRectangle(source);
            var offsetX = viewport.Left + Offset.X;
            var offsetY = viewport.Top + Offset.Y;

            return new Rectangle(new System.Drawing.Point((int)(scaled.Left + offsetX), (int)(scaled.Top + offsetY)),
                new System.Drawing.Size((int)scaled.Size.Width, (int)scaled.Size.Height));
        }

        /// <summary>
        ///   Fits a given <see cref="T:System.Drawing.Rectangle" /> to match image boundaries
        /// </summary>
        /// <param name="rectangle">The rectangle.</param>
        /// <returns>
        ///   A <see cref="T:System.Drawing.Rectangle" /> structure remapped to fit the image boundaries
        /// </returns>
        public Rectangle FitRectangle(Rectangle rectangle)
        {
            if (Image is null) return Rectangle.Empty;
            var x = rectangle.X;
            var y = rectangle.Y;
            var w = rectangle.Width;
            var h = rectangle.Height;

            if (x < 0)
            {
                x = 0;
            }

            if (y < 0)
            {
                y = 0;
            }

            if (x + w > Image.Size.Width)
            {
                w = (int)(Image.Size.Width - x);
            }

            if (y + h > Image.Size.Height)
            {
                h = (int)(Image.Size.Height - y);
            }

            return new Rectangle(x, y, w, h);
        }

        /// <summary>
        ///   Fits a given <see cref="T:System.Drawing.RectangleF" /> to match image boundaries
        /// </summary>
        /// <param name="rectangle">The rectangle.</param>
        /// <returns>
        ///   A <see cref="T:System.Drawing.RectangleF" /> structure remapped to fit the image boundaries
        /// </returns>
        public Rect FitRectangle(Rect rectangle)
        {
            if (Image is null) return Rect.Empty;
            var x = rectangle.X;
            var y = rectangle.Y;
            var w = rectangle.Width;
            var h = rectangle.Height;

            if (x < 0)
            {
                w -= -x;
                x = 0;
            }

            if (y < 0)
            {
                h -= -y;
                y = 0;
            }

            if (x + w > Image.Size.Width)
            {
                w = Image.Size.Width - x;
            }

            if (y + h > Image.Size.Height)
            {
                h = Image.Size.Height - y;
            }

            return new Rect(x, y, w, h);
        }

        /// <summary>
        ///   Gets the source image region.
        /// </summary>
        /// <returns></returns>
        public virtual Rect GetSourceImageRegion()
        {
            if (Image is null) return Rect.Empty;

            if (SizeMode != SizeModes.Stretch)
            {
                var viewPort = GetImageViewPort();
                double sourceLeft = (Offset.X / ZoomFactor);
                double sourceTop = (Offset.Y / ZoomFactor);
                double sourceWidth = (viewPort.Width / ZoomFactor);
                double sourceHeight = (viewPort.Height / ZoomFactor);

                return new Rect(sourceLeft, sourceTop, sourceWidth, sourceHeight);
            }

            return new Rect(0, 0, Image.Size.Width, Image.Size.Height);
        }

        /// <summary>
        /// Gets the image view port.
        /// </summary>
        /// <returns></returns>
        public virtual Rect GetImageViewPort()
        {
            if (Viewport.Width == 0 && Viewport.Height == 0) return Rect.Empty;

            double xOffset = 0;
            double yOffset = 0;
            double width;
            double height;

            if (SizeMode != SizeModes.Stretch)
            {
                if (AutoCenter)
                {
                    xOffset = (!IsHorizontalBarVisible ? (Viewport.Width - ScaledImageWidth) / 2 : 0);
                    yOffset = (!IsVerticalBarVisible ? (Viewport.Height - ScaledImageHeight) / 2 : 0);
                }

                width = Math.Min(ScaledImageWidth - Math.Abs(Offset.X), Viewport.Width);
                height = Math.Min(ScaledImageHeight - Math.Abs(Offset.Y), Viewport.Height);
            }
            else
            {
                width = Viewport.Width;
                height = Viewport.Height;
            }

            return new Rect(xOffset, yOffset, width, height);
        }

        /// <summary>
        ///   Gets the width of the scaled image.
        /// </summary>
        /// <value>The width of the scaled image.</value>
        protected virtual double ScaledImageWidth => Image.Size.Width * ZoomFactor;

        /// <summary>
        ///   Gets the height of the scaled image.
        /// </summary>
        /// <value>The height of the scaled image.</value>
        protected virtual double ScaledImageHeight => Image.Size.Height * ZoomFactor;

        public double ZoomFactor => _zoom / 100.0;

        protected override void OnPointerPressed(PointerPressedEventArgs e)
        {
            base.OnPointerPressed(e);
            if (e.Handled
                || IsPanning
                || IsSelecting
                || Image is null) return;

            var pointer = e.GetCurrentPoint(this);

            if (SelectionMode != SelectionModes.None)
            {
                if (!(
                        pointer.Properties.IsLeftButtonPressed && (SelectWithMouseButtons & MouseButtons.LeftButton) != 0 ||
                        pointer.Properties.IsMiddleButtonPressed && (SelectWithMouseButtons & MouseButtons.MiddleButton) != 0 ||
                        pointer.Properties.IsRightButtonPressed && (SelectWithMouseButtons & MouseButtons.RightButton) != 0
                    )
                ) return;
                IsSelecting = true;
            }
            else
            {
                if (!(
                        pointer.Properties.IsLeftButtonPressed && (PanWithMouseButtons & MouseButtons.LeftButton) != 0 ||
                        pointer.Properties.IsMiddleButtonPressed && (PanWithMouseButtons & MouseButtons.MiddleButton) != 0 ||
                        pointer.Properties.IsRightButtonPressed && (PanWithMouseButtons & MouseButtons.RightButton) != 0
                    )
                    || !AutoPan

                ) return;

                IsPanning = true;
            }

            var location = pointer.Position;

            if (location.X > Viewport.Width) return;
            if (location.Y > Viewport.Height) return;
            _startMousePosition = location;
        }

        protected override void OnPointerReleased(PointerReleasedEventArgs e)
        {
            base.OnPointerReleased(e);
            if (e.Handled) return;

            IsPanning = false;
            IsSelecting = false;
        }

        protected override void OnPointerLeave(PointerEventArgs e)
        {
            base.OnPointerLeave(e);
            PointerPosition = new Point(-1,-1);
            TriggerRender(true);
            e.Handled = true;
        }

        protected override void OnPointerMoved(PointerEventArgs e)
        {
            base.OnPointerMoved(e);
            if (e.Handled) return;

            var pointer = e.GetCurrentPoint(this);
            PointerPosition = pointer.Position;

            if (!IsPanning && !IsSelecting)
            {
                TriggerRender(true);
                return;
            }

            if (IsPanning)
            {
                double x;
                double y;

                if (!InvertMouse)
                {
                    x = _startScrollPosition.X + (_startMousePosition.X - _pointerPosition.X);
                    y = _startScrollPosition.Y + (_startMousePosition.Y - _pointerPosition.Y);
                }
                else
                {
                    x = (_startScrollPosition.X - (_startMousePosition.X - _pointerPosition.X));
                    y = (_startScrollPosition.Y - (_startMousePosition.Y - _pointerPosition.Y));
                }

                Offset = new Vector(x, y);
            }
            else if (IsSelecting)
            {
                double x;
                double y;
                double w;
                double h;

                var imageOffset = GetImageViewPort().Position;

                if (_pointerPosition.X < _startMousePosition.X)
                {
                    x = _pointerPosition.X;
                    w = _startMousePosition.X - _pointerPosition.X;
                }
                else
                {
                    x = _startMousePosition.X;
                    w = _pointerPosition.X - _startMousePosition.X;
                }

                if (_pointerPosition.Y < _startMousePosition.Y)
                {
                    y = _pointerPosition.Y;
                    h = _startMousePosition.Y - _pointerPosition.Y;
                }
                else
                {
                    y = _startMousePosition.Y;
                    h = _pointerPosition.Y - _startMousePosition.Y;
                }

                x -= imageOffset.X - Offset.X;
                y -= imageOffset.Y - Offset.Y;

                x /= ZoomFactor;
                y /= ZoomFactor;
                w /= ZoomFactor;
                h /= ZoomFactor;

                if (w != 0 && h != 0)
                {
                    SelectionRegion = FitRectangle(new Rect(x, y, w, h));
                }
            }

            e.Handled = true;
        }
        #endregion
    }
}