BioCore  5.0.0
A Windows .NET microscopy library and application for viewing, editing, and annotating images.
Loading...
Searching...
No Matches
ImageView.ScrollBars.cs
Go to the documentation of this file.
1using AForge;
2
3namespace BioCore
4{
5 public partial class ImageView
6 {
7 // Normalized positions avoid integer overflow at high slide magnifications.
8 private const int ScrollRange = 1_000_000;
9 private bool updatingScrollBars;
10 private double scrollLeft, scrollTop, scrollWidth, scrollHeight;
11
12 private void UpdateScrollBars()
13 {
14 if (updatingScrollBars || hScrollBar == null || vScrollBar == null || viewport == null)
15 return;
16 BioImage image = OverviewSelectedImage;
17 if (image == null || !image.isPyramidal || image.Resolution <= 0 ||
18 !double.IsFinite(image.Resolution) || ViewWidth <= 0 || ViewHeight <= 0)
19 return;
20
21 RectangleD bounds = OverviewWorldBounds(image);
22 double left = bounds.X / image.Resolution;
23 double top = bounds.Y / image.Resolution;
24 double width = bounds.W / image.Resolution;
25 double height = bounds.H / image.Resolution;
26 if (!double.IsFinite(left) || !double.IsFinite(top) || !double.IsFinite(width) ||
27 !double.IsFinite(height) || width <= 0 || height <= 0)
28 return;
29
30 scrollLeft = left;
31 scrollTop = top;
32 scrollWidth = Math.Max(0, width - ViewWidth);
33 scrollHeight = Math.Max(0, height - ViewHeight);
34 updatingScrollBars = true;
35 try
36 {
37 ConfigureScrollBar(hScrollBar, image.PyramidalOrigin.X, left, scrollWidth, width, ViewWidth);
38 ConfigureScrollBar(vScrollBar, image.PyramidalOrigin.Y, top, scrollHeight, height, ViewHeight);
39 }
40 finally
41 {
42 updatingScrollBars = false;
43 }
44 }
45
46 private static void ConfigureScrollBar(ScrollBar bar, double position, double start,
47 double travel, double imageSize, int viewSize)
48 {
49 int page = (int)Math.Clamp(Math.Round(ScrollRange * Math.Min(1, viewSize / imageSize)), 1, ScrollRange);
50 bar.Minimum = 0;
51 bar.Maximum = ScrollRange;
52 bar.LargeChange = page;
53 bar.SmallChange = Math.Max(1, page / 10);
54 int effectiveMaximum = bar.Maximum - bar.LargeChange + 1;
55 bar.Enabled = travel > 0;
56 double fraction = travel > 0 ? Math.Clamp((position - start) / travel, 0, 1) : 0;
57 bar.Value = (int)Math.Round(fraction * effectiveMaximum);
58 }
59
60 private void vScrollBar_ValueChanged(object sender, EventArgs e)
61 {
62 BioImage image = OverviewSelectedImage;
63 if (updatingScrollBars || image == null || !image.isPyramidal)
64 return;
65 App.viewer = this;
66 PointD origin = image.PyramidalOrigin;
67 if (sender == hScrollBar && scrollWidth > 0)
68 origin.X = scrollLeft + scrollWidth * hScrollBar.Value / (hScrollBar.Maximum - hScrollBar.LargeChange + 1.0);
69 else if (sender == vScrollBar && scrollHeight > 0)
70 origin.Y = scrollTop + scrollHeight * vScrollBar.Value / (vScrollBar.Maximum - vScrollBar.LargeChange + 1.0);
71 else
72 return;
73 image.PyramidalOrigin = origin;
74 UpdateView();
75 }
76 }
77}
AForge.RectangleD RectangleD
Definition ImageView.cs:17
ImageView control for image stacks, pyramidal and whole-slide-images.